I have a spinner that loads data from string-array and I would like to customize it from the XML layout and not from Java code (as I saw in many examples).
Is it possible to change the text color and the spinner style from XML?
Yes you can, here is how you add a spinner layout
item_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="#dimen/small_margin"
android:background="#color/white"
android:ellipsize="marquee"
android:paddingLeft="#dimen/margin_small"
android:singleLine="true"
android:text="--Select--"
android:textColor="#color/text_title"
/>
and use in your adapter like this
ArrayAdapter<String> itemsAdapter =
new ArrayAdapter<String>(this,R.layout.item_spinner, items);
and you are done. customize but keep the textView id same that is how adapter will set text on it.
Related
the many other answers do tell me that android:textColor is the one that does the trick. However no matter what attribute I change in my styles, the app still shows default Black text.In fact, I cannot even see an attribute called textColor or the likes of it in the layout editor - attribs tab.
See here: Spinner with custom text font and color
Here you can make a custom xml file in layout folder where you can add this:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#333333"
android:padding="10dp"
android:textStyle="bold" />
And then in your code mention it like this:
val adapter = ArrayAdapter.createFromResource(this, R.array.array_name, R.layout.custom_spinner) // where array_name consists of the items to show in Spinner
adapter.setDropDownViewResource(R.layout.custom_spinner) // where custom-spinner is
mycustom xml file.
And then set the adapter.
I hope my solution would work,
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#ff0000" />
And then change your declaration of the spinner to use the R.layout.spinner_item:
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, R.layout.spinner_item);
spinner.setAdapter(adapter);
I'm able to change text color and size of popup list shown after clicking the spinner. after selecting an item , the item sets on spinner , i've to change this item text size on spinner. please help...
Use the below adapter constructor for customizing spinner in the way
you required.
spinnerAdapter = new ArrayAdapter(this, R.layout.row_spinner
/* The resource ID for a layout file containing a layout to use when
instantiating views. */ ,android.R.id.text1, array);
And you can set the layout resource defining the drop down views using
spinnerAdapter.setDropDownViewResource(R.layout.layout_spinner);
Sample layouts
row_spinner
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:gravity="center"
android:singleLine="true"
android:textColor="#color/colorBlack"
android:textColorHint="#color/colorBlack"
android:textSize="#dimen/text_size_l"/>
layout_spinner
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="#dimen/margin_padding_width_height_6"
android:ellipsize="end"
android:gravity="center"
android:singleLine="true"
android:textColor="#color/colorBlackLight"
android:textSize="#dimen/text_size_l"/>
in my project I have this custom layout inflated in the actionbar:
Link to image
But i would like to have the spinner text of white color.
I've seen a lot of topics on the web about this, but I can't fix the problem yet.
How can I fix this problem? How can I change the text color of my spinner bysetting it to white color?
EDIT:
the xml code of spinner is this:
<Spinner
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#ffffff"
android:id="#+id/spinner"></Spinner>
It's very simple, I tried many things but nothing..
In your activity you should have something like this:
(Basically you are getting the Spinner and setting it an adapter. This adapter is the responsible for filling it)
Spinner spinner = (Spinner) findViewById(R.id.your_spinner_id);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.your_selected_spinner_item, myArrayList<String));
adapter.setDropDownViewResource(R.layout.your_dropdown_item);
spinner.setAdapter(adapter);
And you can have this XMLs: (put them on res/layout)
your_selected_spinner_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myLayoutID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="20sp"/>
And also
your_dropdown_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myDropdownLayoutID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="20sp"/>
Create a Layout file like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinner_ref_id"
android:layout_width="match_parent"
android:padding="20dp"
android:textColor="#fff"
android:textSize="20sp"
android:text="Hello"
android:layout_height="wrap_content">
</TextView>
Assuming that you are using ArrayAdapter as your Spinner Adapter.
Hope it helps!!!
I am trying to experiment with a custom Spinner / ArrayAdapter. Following is my code:
myspinner=(Spinner)findViewById(R.id.myspinner);
myadapter=new ArrayAdapter<String>(this,R.layout.mytextview,R.id.mytv,sample_data);
myadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
myspinner.setOnItemSelectedListener(this);
myspinner.setAdapter(myadapter);
Following is the custom layout mytextview that contains mytv:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="#+id/mylayout" android:layout_width="match_parent" android:layout_height="wrap_content">
<View android:layout_width="match_parent" android:layout_height="15dip" android:id="#+id/myview1" android:background="#color/bordertop"></View>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="30dip" android:id="#+id/mytv"></TextView>
<View android:layout_width="match_parent" android:layout_height="15dip" android:id="#+id/myview2" android:background="#color/borderbottom"></View>
</LinearLayout>
When i'm press the spinner to bring the popup, Android crashes. The came logic works perfectly fine, when I comment out the setDropDownViewResource method. I still don't understand what view setDropDownViewResource actually sets. Can anyone show me how it looks like by default. And, if one wants to override it with a custom layout - does the layout need to have a specific structure?
here is what i have typically used for spinners:
Spinner distancespinner = (Spinner) findViewById(R.id.distance);
ArrayAdapter<CharSequence> distanceadapter = ArrayAdapter.createFromResource(
this, R.array.distance, android.R.layout.simple_spinner_item);
distanceadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
distancespinner.setAdapter(distanceadapter);
distancespinner.setOnItemSelectedListener(new distanceSelector());
(obviously replace distance with whatever you want)
<Spinner
android:id="#+id/distance"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:prompt="#string/app_name"
/>
to me it looks like your problem is in this line :
myadapter=new ArrayAdapter<String>(this,R.layout.mytextview,R.id.mytv,sample_data);
but then i dont know, your ArrayAdapter is based on instead of as i have and as per Spinner "the android.R.layout.simple_spinner_item ID references a layout for the standard spinner appearance, defined by the platform. Then setDropDownViewResource(int) is called to define the appearance for each item when the widget is opened (simple_spinner_dropdown_item is another standard layout defined by the platform)."
i think if you want to customize your spinner, .setDropDownViewResource(); is where to do it.
I'm try to change text color and align item in spinner to center of it how can I do this
here is my code
String[] li={"1","2","3"};
final Spinner combo = (Spinner)findViewById(R.id.widget30);
ArrayAdapter<String> a = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, li);
combo.setAdapter(a);
Thanks
Use a custom view for that, and specify by calling:
a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Note that you have to use your own view here: R.id.my_simple_spinner_dropdown_item
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />