I am trying to setText in Spinner but is not visible in device. (Visible in emulater) .
Please give solution for this.
You cant settext to spinner with settext(). You have to use ArrayAdapter for displaying the content in spinner. like this,
Declare an array-
String[] type_array = {"Monthly","Quaterly","Yearly"};
In onCreate()-
spinner_type = (Spinner) findViewById(R.id.spinner_type_susa);
spinner_type.setOnItemSelectedListener(this);
ArrayAdapter<String> adapter_type = new ArrayAdapter<String>(this,R.layout.spinner_row,type_array);
adapter_type.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_type.setAdapter(adapter_type);
You gave the text a holo color that is only available on a high API level and your device doesn't know what to do with it so it gave the default color of text which is black, which just happens to be the color of the background in your spinner. Yes this is a wild guess.
Related
I am using a spinner in my application and setting its item values via xml. I have set spinner item default color via style, I have to enable and disable my spinner as required in my application.
I want to change its item color programmatically when I set its state disable. I have searched through google but could not find any solution for this.Is there anyone who has any idea about this how can I set it.I have used this link how do you set the spinner text color?, but it is giving null pointer exception.
m_spnDia = (Spinner)findViewById(R.id.spiDia);
TextView oTextView = (TextView)m_spnDia.getChildAt(0);
oTextView.setTextColor(Color.RED);
You can create custom layout for your spinner item as like created here :
How to change spinner text size and text color?
and use it inside your ArrayAdapter.
Now, As per your requirement you have to create two xml files to provide two different colors to spinner items :
1) When your Spinner is Enabled.
2) When your Spinner is Disabled.
Just change the line below as :
if(spinner1.isEnabled){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item1,list);
}else{
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item2,list);
}
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.
Can We add a hint to an Android spinner like EditText: hint, please send me any code. Thanks in Advance.
Put your array into a arraylist and write down
arraylist.add("whatever hint you want")
and then fill the spinner by this
ArrayAdapter<String> ad_state = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,arraylist);
ad_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
state.setAdapter(ad_state);
Android Spinner with Hint Sample project
override getview to display the hint before any value was selected.
I'm using default spinner
<Spinner
android:id="#+id/mySpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
((Spinner)findViewById(R.id.mySpinner)).setAdapter(spinnerAdapter);
How come elements are on a white background and a foreground color gray? It seems strange to me...
I know this is an old question, and this may help others comes here for answers.
Basically, it is because of the use of application context here. If you use the application context, the base theme will be applied on your layout and not the theme of your activity and that is the reason the color is white.
The details are well explained in the link: spinner text color is white? and here
Thanks commonsware for providing the answer to the original question.
The reason is you set it with android.R.layout.simple_spinner_item.
You can write your own Adapter class:
Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
spinner.setAdapter(new MyCustomAdapter(CLASS.this, R.layout.spinner_layout, spinnerAdapter));
Then it will look like the way you design in spinner_layout.
I would like to change the autocomplete dropdown boxes element size to something smaller.
Any change i do with textview.settextsize affects only the value in the fieldbox and not in the drop down box!
I am adding the list items dynamically and my adapter is set to the resource
adapterForFromAutoText.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
Should i add my own resource to customize the style of the font, if so does it matter that i use adapterForFromAutoText.add(displayName); to dynamically add data through the adapter.
Do you have the attribute "android:textSize" within your "android.R.layout.simple_dropdown_item_1line"? You may want to try to change the textSize there.
You may also want to check out this tutorial.
Use This Layout , It will change the autocomplete dropdown boxes element size to smaller
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, aList);