Android simple spinner item
Asked Answered
N

5

24

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?

Nightgown answered 26/9, 2014 at 9:52 Comment(1)
Use android:layout_height="wrap_content". You Cannot use dropdownListPreferredItemHeight as its not public.Lully
V
43

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"/>
Verniavernice answered 23/5, 2015 at 14:24 Comment(3)
This doesn't answer the question.Unilateral
How does it not answer the question? It literally directly answers the question....Verniavernice
@DanielWilson android:layout_height="?attr/dropdownListPreferredItemHeight" is marked as private in appcompat-v7Wyatt
B
4

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);

    }
Balbo answered 26/9, 2014 at 11:0 Comment(3)
I know, but this will also set the alignment to left. I want the default view with alignment right.Nightgown
sorry I meant, this will wrap the content so the listitem is smalller than the dropdownListPreferredItemHeight. If I know how high that would be, I could set the heigt... not a beautiful solution thoughNightgown
@Nightgown It's 64dip.Mcintosh
C
4

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"

Canonist answered 7/12, 2017 at 10:43 Comment(0)
V
2
android:layout_width="match_parent"
android:layout_height="48dp"

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

Vidal answered 3/3, 2017 at 17:57 Comment(0)
C
1

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.

Cabanatuan answered 26/9, 2014 at 10:0 Comment(2)
I already tried "wrap content" but this will minimize the item height radically so its to small. I juut want to stay with the default size.Nightgown
Wow, everyone keeps giving the same terrible suggestion, which removes the space between each item. Why would anybody be content with that?Muddlehead

© 2022 - 2024 — McMap. All rights reserved.