I have a selection for some items using a spinner widget. At the moment the spinner will load a simple Textview and show the name of the item.
It is possible to define an own view to show inside the spinner row? I would suspect it being similar to a custom List row.
I simply want to show an individual icon left from the spinner text for each item in the list.
Yes, you can do that - as you may know, Spinner is a type of AdapterView, which means that you can adapt your own data to show in such View.
The approach is pretty similar to the one with ListView.
Excerpt from ApiDemos:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, INTERPOLATORS);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
You can define your own layout and pass it to the SpinnerAdapter subclass that you use.
Yes, it is very easy.
The main thing is that, define TextView as the root element in the layout file.
Do as below:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, INTERPOLATORS);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
simple_spinner_dropdown_item.xml file :
<?xml version="1.0" encoding="utf-8"?>
<TextView>
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50px"
android:textColor="#000000"
android:textSize="20px"
android:gravity="center_vertical" >
</TextView>
Related
for now radio button image on shown on right side of my spinnerand when I click on it, it open option list in popup box
what I want is to show arrow on right side of spinner and options list should be drop down instead of popup box with white background.
see image
How Can I do this, Do I need to create a custom spinner?
Here is the code
XML
<Spinner
android:id="#+id/type_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/text_color"/>
Java
type_Spinner = (Spinner) findViewById(R.id.type_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, new String[]{"Buy","Sale","Rent","Let"});
type_Spinner.setAdapter(adapter);
1st approach
Change Spinner in xml like this
<Spinner
android:id="#+id/type_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_dropdown" />
Change theme of that Activity to
android:Theme.Holo
In java class
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, new String[]{"Buy","Sale","Rent","Let"});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
type_Spinner.setAdapter(adapter);
2nd approach
Instead of Spinner use a Button set background using android:background="#android:drawable/btn_dropdown" set gravity (not layout_gravity) to left|center_vertical open a PopupWindow on clicking of that Button set that Button as anchor of that PopUpWindow. In that PopUpWindow place a ListView and in OnItemClick change text with selected value in that Button using setText(java.lang.CharSequence)
Full code snippet for 2nd approach
If you use approach 1 then it will work on Post Gingerbread
version. Approach 2 will work on any version of android(not tested in
pre froyo).
In layout use the android:spinnerMode
android:spinnerMode="dropdown"
In activity use like this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, new String[]{"Buy","Sale","Rent","Let"});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
type_Spinner.setAdapter(adapter);
I have ListView that shows data:
<ListView
android:id="#+android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
The headlines is an array:
List headlines;
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, headlines);
setListAdapter(adapter);
How to change the text color, style and size? There isn't any android:textColor on the ListView.
Find the file simple_list_item_1.xml from your sdk installation, copy it to your project's layout folder, rename it and make whatever color/text changes you want in there. Then replace the layout call in your program with your new layout name (don't forget that since you will be using your own list row layout you need to remove the android. from in front of the layout call).
It should look something like this:
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.your_row_layout, headlines);
Hope this helps!
This is the code for my AutoCompleteTextView :
String[] countries = getResources().getStringArray(R.array.countries_array);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item_list ,countries);
textView = (AutoCompleteTextView) dialog.findViewById(R.id.autoCompleteTextView1);
adapter.setNotifyOnChange(true);
textView.setAdapter(adapter);
This is item_list :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#000"
android:drawableRight="#drawable/arrow">
and this is the output :
I want to change this pop-up kind of suggestion box , and want to show the suggestion list a little bit below from the AutoCompleteTextView ..similar to this : (please disregard the apple design, it’s just about where the results appear)
How can i do this ..Please suggest .
Thanks.!!
Use the android:dropDownAnchor, android:dropDownHorizontalOffset, android:dropDownVerticalOffset and maybe others on the AutoCompleteTextView in your layout. See AutocompleteTextView.
I used a ListView below the AutoCompleteTextView. I added my custom adapter to both the ListView and the AutoCompleteTextView. I the set the height and width of the AutoCompleteTextView dropdown to 0:
myListView.setAdapter(myCustomAdapter);
myAutoComplete.setAdapter(myCustomAdapter);
myAutoComplete.setDropDownHeight(0);
myAutoComplete.setDropDownWidth(0);
Since the listview is also attached to the same adapter it'll get updated when you update the autocomplete.
u need QSB (Quick search box).
refer to this http://developer.android.com/resources/articles/qsb.html
It needs a ContentProvider to fill the QSB data.
Hi I have a spinner, which contains radio button elements. My spinner loads the elements, but everything is invisible. I think you can understand my situation from the image. an I get any suggestions.
This is the code I am using,
String[] reminder={"5 minutes","10 minutes","15 minutes","20 minutes","25 minutes","30 minutes","35 minutes","45 minutes"};
spinnerAdapter = new ArrayAdapter<String>(mContext,
android.R.layout.simple_spinner_item, reminder);
spinnerAdapter
.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
reminder_spinner.setPrompt("Reminders");
reminder_spinner.setAdapter(spinnerAdapter);
reminder_spinner.setOnItemSelectedListener(new ItemSelect());
The fact that the item text shows up when it is selected leads me to believe it could be a styling issue, ie white font on white background. Here is the layout for simple_spinner_dropdown_item:
<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="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"/>
I would define another xml layout using the above as a template, and pass that to setDropDownViewResource instead.
add this
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);`
in your code and remove
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);`
How do I change the text size of a spinner in android.
I have read that if you get your items from a resource (like R.array.items) you use ArrayAdapter.createFromResource(context, R.array.items, R.layout.textview), but what if a get my items from a list that changes, how would I then change text size of the spinner.
You need to redefine 2 resource xml's
Adapter dropdown view resource
Spinner item resource
Spinner spinner=new Spinner(context);
ArrayAdapter adapter = new ArrayAdapter(context, R.layout.my_spinner_item);
adapter.setDropDownViewResource(R.layout.my_spinner_dropdown_item);
E.g. my_spinner_dropdown_item.xml resource can be defined as:
<MyCheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/simpleSpinnerDropDownItem"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight" />
where MyCheckedTextView class has to be defined by you (extending CheckedTextView) - so it's a place where you can define your text size/style - whatever