Android spinner dont draw selected item - android

I have a spinner with three items in it
<string-array name="menuitems">
<item>one</item>
<item>two</item>
<item>three</item>
</string-array>
Then I set up my spinner like this
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.menuitems, R.layout.spinnerstyle);
adapter.setDropDownViewResource(R.layout.spinnerdropdownstyle);
spinner.setAdapter(adapter);
The spinner displays like this
One
One
Two
Three
My problem is for this spinner I don't want to display the selected item I want this
One
Two
Three
And obviously if two were selected only one and three would appear in the dropdown. I did search for this but couldn't figure out a way to get that affect I tried changing the menuitems array on selection but if I removed the item from there it wouldn't draw it in the selection. I assume there is a fairly easy way to get this affect.
thanks for any help.

Write a custom SpinnerAdapter which returns the items from the menu excluding the selected one.

You can try to set the promt in the xml to "One" like this:
<Spinner android:id="#+id/spinnerTest"
android:layout_marginLeft="50px"
android:layout_width="fill_parent"
android:drawSelectorOnTop="true"
android:layout_marginTop="5dip"
android:prompt="#string/SelectOne"
android:layout_marginRight="30px"
android:layout_height="35px"
/>
Then you should override the onItemClick methode and set the text of your variable in
"On nothing selected".

Related

Android "custom" Spinner

What I need to achieve is a Spinner with these properties:
The Spinner allows the user to select from a fixed set of strings. Those strings are localized, so an English user has different choices than a French user.
When I try to retrieve the value of the selected item of the Spinner, I don't want it to return the localized value of that selected item, but rather the "non-localized" one (in my case, the English value of that string).
How could I achieve this kind of thing? Would a custom Adapter suffice? I've been stucking here for a while.
In your string.xml put
<string-array name="language">
<item>English</item>
<item>Franch</item>
</string-array>
In Your XML where you use spinner
<Spinner
android:id="#+id/language"
android:layout_width="60dp"
android:layout_height="30dp"
android:entries="#array/language"></Spinner>
and finally on java file
Spinner language1= (Spinner) findViewById(R.id.language);
language.getSelectedView().toString())
it work for me..
Ok, a custom Adapter was sufficient to do the trick. I've also defined a layout resource (for elements in the spinner) containing two TextViews: one with visibility="Visible" and one with visibility="Gone". The former holds the localized value, while the latter holds the english value.

How to change TextSize and Background Color of a spinner

I want to change my spinner background color and text size of each item in the Spinner, i already have gone through few answers for questions similar to mine but did not get the answer weather we can change the textSize / Background Color of a spinner or not? If anyone have a simple answer, i will be thankful to their reply.
Thanks in advance.
In the strings.xml file create a <string-array name="spinner"> tag.
create <item> tags inside with each entry.
Wrap the text in each item with <![CDATA[ ...raw html... ]]> tag. Put in the formatting information in the raw html part ofcourse.
In the activity,
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.spinner, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Check the top answer from this post :
Set TextView text from html-formatted string resource in XML
Though this refers to a TextView, it can be modified to be used in a Spinner too.
You'll have to create a custom xml list item for your spinner, something like this list_item_spinner.xml
<?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:textSize="20sp"
android:background="#000000"
android:textColor="#FB26A9"
android:padding="10dip"/>
After that use that file in your code as follows
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item_spinner, mListData);

spinner text color is white?

I use a Spinner inside a layout.
I use this code:
...
<Spinner
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/spin_prod_promo" />
...
Now, when I assign it's adapter I use this code:
...
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(mcontext, android.R.layout.simple_spinner_item, mylabels);
// Drop down layout style
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner myspinner = (Spinner) V.findViewById(R.id.spin_prod_promo);
// attaching data adapter to spinner
myspinner.setAdapter(dataAdapter);
...
My problem is that, the text color of the spinner is white... thus unreadable, and I do not set it to be this way anywhere in the code.
The "funny thing" is that I use the exact same code in a different activity and the Spinner has the right colors.
The difference is that in this (BAD) case the spinner is located in a fragment.
So if the spinner is in an activity - colors are fine
If the spinner is located on a fragment - color is messed up
I tried using a custom layout for the items of the spinner, but all I succeeded was to make the spinner look disabled. Plus there is always an item in the dropdown that has the color of the background (it can only be seen if I click on it, then I can see the content as long as I keep the click on)
What is going on here?
How can I solve this problem?
Is there something I can do to set the colors of the spinner to be the colors of the theme?
All other widgets in the fragment have normal colors...
Thank you
Context mcontext = getActivity().getApplicationContext(); Is this wrong?
Yes, this is wrong. Only use getApplicationContext() when you know exactly why you are using getApplicationContext(). In particular, never use getApplicationContext() for inflating layouts, as it will screw things up by not using the right theme.
I would delete mcontext entirely, replacing it with getActivity() where needed in your fragment.
just try this..
String[]array={"A","B","C","D"};
ArrayAdapter<String> adapter=new ArrayAdapter<String> (**YourClassName.this**,android.R.layout.simple_list_item_checked,array);
listView.setAdapter(adapter);
NOTE YourClassName.this is your current class name.This code is working for me.
I found the solution. It is to declare a custom layout for the item of the Spinner and use it instead of the default one. And in the custom layout, I set the color of the TextView text to black.
It works this way.
So the code: (promospin_row.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textColor="#000000"
android:id="#+id/tv_promo_txt"
android:paddingLeft="5dp"
android:paddingTop="5dp" />
</LinearLayout>
and the java code:
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(mcontext, R.layout.promospin_row, R.id.tv_promo_txt, mylabels);
// Drop down layout style
dataAdapter.setDropDownViewResource(R.layout.promospin_row);
Spinner myspinner = (Spinner) V.findViewById(R.id.spin_prod_promo);
// attaching data adapter to spinner
myspinner.setAdapter(dataAdapter);
This way, the item text becomes black. And no other anomalies.
Thank you #CommonsWare for your time
Just use null instead of getApplicationContext().
It must work.
I have tried it and its working.
In your Application you can use null instead of mcontext
I was having this problem too. This is not a solution but a potential workaround for others to try in case the above solutions don't work.
Set the background color of the popup that appears when the spinner is selected to something that contrasts the white text color.
<Spinner
...
android:popupBackground="#color/layout_background"
... />
Here I have defined layout_background as a dark greyish color #565656 in a seperate file called colors.xml located in my src/main/java/res/values folder.
Another solution is to indeed use GetApplication() as the context instead of
getActivity().getApplicationContext();
This way I can use the default item layout without having to declare a new custom layout.
So I guess, CommonsWare was right afterall. It is the context that blew things up.
Please #Commonsware, make your comment an answer so I can accept it, because you deserve it.
EDIT
I was writing this while you wrote your answer, that is why it seems I didn't see your answer :)
Just use getBaseContext() instead of mcontext.
Worked for me. I set the adapter of spinner inside onItemClick function of AutoCompleteTextView and had the same problem of text color until I used getBaseContext(). Hope this answer helps.

How to add icon to spinner (where find the default value)

how i add icon to spinner by xml where find the default value (not in the the list) ??
<Spinner
android:id="#+id/spinner1"
android:layout_gravity="center"
android:layout_weight="0.28"
android:prompt="#string/language_prompt"
android:textSize="12sp" <!-- icon insten of text -->
/>
i try android:Background="#drawable/world_language_icon" but doesn't work.
i dont find src or set drawable .
I dont find here (in google)
i need xml and not javaalso here i find in java
First of all, you should probably try proof reading your question before posting, and make sure you know exactly what you want to know.
how i add icon to spinner by xml
The icon is not added to the spinner itself, as what you use to populate the Spinner is an adapter. In order to add an icon to your Spinner items, you would need to create a custom adapter to set to the Spinner, and use a custom layout for the List Items.
where find the defult value (not in the the list) ??
That, I'm not sure what you are asking for. If your question is how to set a default item/value for the Spinner, it's not possible. The default item set will always be the first item on the List you populate the Spinner with. You can of course automatically select a different Spinner item in code, but even then you'll notice that the first item was selected before your other actions.

Change font size on actual Spinner button object

I know it is possible to change the font size of the Spinner menu that pops up when the button is clicked, and I know it is possible to change the font size of an ordinary button - but I can't seem to find anything on changing the size of the text in the actual spinner button itself. Is this possible? If so, how can it be done?
This is the generic layout from simple_spinner_item.xml, I've added a textSize attribute at the bottom, let's save it as spinner_item.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="36sp" />
And you use it just like any other layout in an adapter:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.spinner_dropdown_item);
spinner.setAdapter(adapter);
There is no "text in the actual spinner button itself", except what you put there. You determined the layout that appears in the Spinner, in both the closed and open states, by how you set up your SpinnerAdapter (e.g., via layout files supplied to the constructor and to setDropDownViewResource()). If you want to change anything about those layouts, you need to change your layout resources or change your SpinnerAdapter.

Categories

Resources