How to change Spinner drop down image icon? - android

I want to change the default spinner drop down icon also want to change the background of it, by using which property I can do that?
I am having following spinner code:
<Spinner
android:id="#+id/security_ques"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
style="#style/spinner_style"
android:prompt="#string/prompt" >
</Spinner>
please give any solution on it

See this link for custom spinner drop down box.
They have created customized xml layout for spinner and called that layout to the spinner using these code
Spinner mySpinner = (Spinner) findViewById(R.id.spinner_show);
mySpinner.setAdapter(new MyAdapter(this, R.layout.custom_spinner, spinnerValues));

use it in your xml
android:background="#drawable/your_imagename"

Related

android Setting style to programmatically created spinner

I have a spinner which is created programmatically like this:
spinnerLogger = new Spinner(context);
spinnerLogger.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
SpinnerAdapter spinnerAdapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_dropdown_item, spinnerNames);
spinnerLogger.setAdapter(spinnerAdapter);
in my code I also use xml created spinners which use a style to make sure the user understands they are spinners:
<Spinner
android:layout_width="90dp"
android:layout_height="wrap_content"
android:id="#+id/spinnerHour"
android:spinnerMode="dropdown"
style="#style/Widget.AppCompat.Spinner.Underlined" />
I want to apply the Widget.AppCompat.Spinner.Underlined style to the programmatically created spinner but can't seem to find how I can achieve this.
I tried by creating an xml containing only a spinner with the right style (xml's name is spinner_underlined and it's located in the layout folder):
<?xml version="1.0" encoding="utf-8"?>
<Spinner android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner"
xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/Widget.AppCompat.Spinner.Underlined"/>
when i try to apply this using
SpinnerAdapter spinnerAdapter = new ArrayAdapter<>(context, android.R.layout.spinner_underlined, spinnerNames);
it gives me a cannot resolve symbol 'spinner_underlined' error
You need change to R.layout.... not android.R.layout....

setAdapter() in Android Spinner removes the custom background drawable

Here is the background drawable image of my spinner, when I've not set adapter :
Here is the xml :
<Spinner
android:id="#+id/cats"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="bottom"
android:background="#drawable/bgspinner"
android:textSize="12sp" />
But, when I set adapter, the background looks like has been overridden:
and here is the java code :
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, R.layout.newcardialogitme, lables);
cat.setAdapter(dataAdapter);
But when I do this with custom layout.xml, background image of the spinner is overridden! .
Please help me in understanding this.
Use custom adapter and in getview methosset background to parent view as below. It will work.
parent.setBackgroundResource(R.drawable.spinner_background);
Have you checked your ..
R.layout.newcardialogitme file..
because your spinner taking that layout's background..
please set its background to transparent color..
then you will have you background..
Appreciation welcomed

How to remove radio button in spinner only in layout view android

I have strange problem with spinner.
My spinner display radio button when i don't use it, check this out:
Image 1 Radio when not use
Image 1 Radio when use spinner
Someone know how fix it? I want radio button when i click on spinner and dropdown list shows, but now when i dont use spinner.
Thanks in advance.
when you set adapter for your spinner you can set it's layout like this:
String[] arr = new String[]{"a","b","c"};
selectionSpinner = (Spinner) findViewById(
R.id.dr);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arr);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectionSpinner.setAdapter(spinnerArrayAdapter);
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000" >
<Spinner android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/dr" />
</RelativeLayout>
result:

how to create a spinner with radio buttons

In main.xml I would like to have a spinner1 with two radio buttons and a spinner2 with 3 checkboxes. I don't know how to define and create this spinners in Main.java.
Need some help.
main.xml
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Spinner
android:id="#id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
spinner1 - needs to have radio buttons and spinner2 need to have multiple checkboxes
main.java
privare Spinner spiner1,spiner2;
public void OnCreate(BUndle SaveInstaceState)
{
super.OnCreate(savedInstanceState);
setContentView(R.layout.main)
spiner1=(Spinner)findViewById(R.id.spinner1);
spiner2=(Spinner)findViewById(R.id.spinner2);
//what to do from here?
}
create a strings.xml file in res/values/ and add the following:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="spinnerstr">Choose an item</string>
<string-array name="spinner_array">
<item>apple</item>
<item>orange</item>
<item>grapes</item>
</string-array>
In your spinner.java, add the followoing:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.spinner_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Hope this will help you.
Accepted answer doesn't work anymore.
Instead use this to have the radio button look:
adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
And for those who are using custom layouts, just add android:checkMark="?android:attr/listChoiceIndicatorSingle" and android:gravity="center_vertical" which makes the radio button align with the text.

Android - Text is Pushed to the Left in a Spinner

I have a spinner with a style. The style only contains a background for the spinner. The problem is,that no matter which image I use for the background, the text of the spinner is always pushed to the left.
This is the declaration of the Spinner in the XML -
<Spinner
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/minus"
android:layout_width="wrap_content"
android:layout_below="#+id/female"
android:id="#+id/spin"
android:gravity="center"
android:background="#drawable/spin"
android:layout_marginTop="10dip">
</Spinner>
Also, I get a warning under the android:gravity attribute that says it's an unknown XML attribute.
I can't figure why it does that.
Thanks
Continuing from my last comment above...
The following code modifies the Hello Spinner tutorial application to display the spinner text contents centered horizontally, as seen in the following two screenshots.
res/layout/my_spinner_textview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" />
public class HelloSpinner extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array,
// android.R.layout.simple_spinner_item);
R.layout.my_spinner_textview);
// adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(R.layout.my_spinner_textview);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
//No other modification needed.
This hopefully provides enough direction to fix the problem.
Use android:textAlignment="center" tag on spinner
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/state"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"/>
Add gravity in your xml file...
<Spinner
android:gravity="center">
</Spinner>
Or add gravity in your java code...
Spinner spinner = (Spinner)findViewById(R.id.spinner);
spinner.setGravity(17);
//17 = center
I had this problem as well and the accepted answer did not work to me.
If this also applies to you, make sure that no parent layout overrides the gravity of the spinner item. In my case, I had to set the gravity of the entire Spinner to right.
If you want to see the checkboxes in the dropdown menu while using your own layout:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
mContext, R.array.room_list, R.layout.spinner_layout);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
yourSpinner.setAdapter(adapter);
Please notice the "android" before "R.layout...." in the line where you put the drop down view resource!

Categories

Resources