My spinner items are invisble - android

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

Related

Change spinner icon and list open style

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

Customized the Suggestion List of AutoComplete Text View

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.

How to wrap lengthy text in a spinner? [duplicate]

This question already has answers here:
Spinner does not wrap text -- is this an Android bug?
(15 answers)
Closed 3 years ago.
I have two spinner and EditText controls within a table layout view on a separate row. The spinners are populated with data. My problem is the data (texts) that are populated into the spinners are too lengthy to fit the screen size. Therefore, the spinners are forced to stretch unnecessarily stretching other controls on another row.
It's a must for me to show the texts in the spinner. Hence using ellipses is not an option. If it's possible how can I wrap the lengthy text on the spinners?
Step 1. TextView with wrapped text
The first thing to do is to to force simple TextView to wrap text. Its easy:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="very long text that will be wrapped to next line" />
Note the singleLine attribute here.
Step 2. Custom layout
Now we should somehow set singleLine attribute to false in TextView used by Spinner to show the item in the list.
In your code you probably have place where you create adapter to use it with Spinner:
this.mAdapter = ArrayAdapter.createFromResource(this, R.array.Planets,
android.R.layout.simple_spinner_dropdown_item);
The idea is to copy the android.R.layout.simple_spinner_dropdown_item layout to your project. Then modify it by setting singleLine attribute to false in CheckedTextView:
For this, add file to res/layout folder named multiline_spinner_dropdown_item.xml with next code:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="false"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee" />
Note that this file is identical to android.R.layout.simple_spinner_dropdown_item layout, except it has singleLine set to false now.
Step 3. Creating Adapter with custom layout
Modify your adapter creating code to:
this.mAdapter = ArrayAdapter.createFromResource(this, R.array.Planets,
R.layout.multiline_spinner_dropdown_item);
Here is screenshot from modified SpinnerActivity example from Android SDK:
Define a custom layout and use it with the spinner and adapter.

How to change appearance of ListView?

I have three main things I want to change with the appearance of a ListView.
1) get rid of horizontal dividers that print between items
2) change font size of test displayed in listview
3) change the padding around each item displayed
It looks like after reading for a while that 2) and 3) should be controlled by changing these properties for the TextView used by the ListView, but I don't yet understand how to do that. Can someone answer these with a little more detail?
1) Set divider height to 0 --- setDividerHeight(0) and set divider color to transparent --- setDivider(new ColorDrawable(0x00FFFFFF))
2) If you're using a list of text views then you can continue using a simple adapter like ArrayAdapter but you will need to create a custom text view. You can add something like this to res/layout
test_text.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize=<SIZE IN SP>
android:textColor="#FFF" />
3) Add padding to your textview above
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text"
android:padding="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize=<SIZE IN SP>
android:textColor="#FFF" />
Use your new layout with your ArrayAdapter
eg.
ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, R.layout.test_text, R.id.text, myData);
All of those can be controlled via styles. The docs on styles explain everything you need to do.
In short, you create a style resource - derived from whatever you want to use as your base - where you override all the relevant items, like the divider, the padding the font size.
Then, you either apply that style to your ListView in the XML file, or you create a theme to it and call setTheme() BEFORE you call setContentView().
EDIT: I should mention the obvious answer that kcoppock pointed out - if this is specific to just one element in your layout, just modify that particular element in your layout file.

How to show own view in Spinner Widget instead of Text View?

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>

Categories

Resources