Zuop
Zuop

Reputation: 624

Android simple spinner item

this question relates to this one: android.R.simple_spinner_item

Since I can't comment because of low reputation, I have an additional question:

If I copy&paste the android.R.simple_spinner_item layout, I get an error on

android:layout_height="?android:attr/dropdownListPreferredItemHeight"

saying "error: Error: Attribute is not public. (at 'layout_height' with value '?android:attr/dropdownListPreferredItemHeight')."

I just added android:gravity="right" to get the spinner_item alignment to the right side.

How can I solve this error?

Upvotes: 24

Views: 20822

Answers (5)

Anthony Cannon
Anthony Cannon

Reputation: 1273

This resource is private, so only the library which that attribute comes from can use it. So you need to get the size of this attribute and create it within your app. From the source code: https://android.googlesource.com/platform/frameworks/support/+/50fe5ec/appcompat/res/values/themes.xml

we can see at lines 50 and/or 84 the attribute there. So in your dimens.xml file you can write:

<dimen name="dropdownListPreferredItemHeight">64dip</dimen>

and then reference it like you would a normal resource:

android:layout_height="@dimen/dropdownListPreferredItemHeight"

Upvotes: 4

Omkar Amberkar
Omkar Amberkar

Reputation: 2452

android:layout_width="match_parent"
android:layout_height="48dp"

Create a custom layout with these attributes. You might want to theme it later.

Upvotes: 2

Daniel Wilson
Daniel Wilson

Reputation: 19824

Seems to work for me if you don't prefix it with android, like so:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@android:id/text1"
             style="?android:attr/spinnerDropDownItemStyle"
             android:singleLine="true"
             android:layout_width="match_parent"
             android:layout_height="?attr/dropdownListPreferredItemHeight"
             android:ellipsize="marquee"/>

Upvotes: 43

Jossy Paul
Jossy Paul

Reputation: 1287

then you will have to design it.

layout/my_spinner_textview.xml

 <?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/spinnerItemStyle"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:gravity="right" />

and this is how i set the adapter

 private String[] state= {"Andra Pradesh","Arunachal Pradesh","Assam","Bihar","Haryana","Himachal Pradesh", "Jammu and Kashmir", "Jharkhand","Karnataka", "Kerala","Tamil Nadu"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,  R.layout.my_spinner_textview, state);
        adapter_state.setDropDownViewResource(R.layout.my_spinner_textview);
        Spinner spinner=(Spinner)findViewById(R.id.spinner1);
        spinner.setAdapter(adapter_state);

    }

Upvotes: 4

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

You can only use the android resources (themes or attributes) that are defined as public by System.

As attr "?android:attr/dropdownListPreferredItemHeight" is not public, you cannot use that.

Instead, you can use

android:layout_height="wrap_content"

for Spinner item layout.

Another workaround may be of copying the resources from SDK to your project and then use them in your project.

Upvotes: 1

Related Questions