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:
Related
I have created two spinner, but when I add a custom layout to adapter, during scroll some items disappear.
xml file:
<?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="25sp"
android:padding="20sp"
android:textColor="#000000"
android:textAlignment="center"/>
java file:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_text, list);
spinner.setAdapter(adapter);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, R.layout.spinner_text, list2);
spinner2.setAdapter(adapter2);
How can i resolve the problem?
This is probably happens when you are supporting multi languages in you application, where different languages requires different layout direction.
Try adding android:layoutDirection to your text view in the custom spinner item layout and this will be fixed.
I am trying to add a custom layout to my listview where I am calling:
mylistAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, carList);
I was told I am able to replace the simple_list_item_single_choice with a custom xml. I made a xml and added it to the layout folder in my project and called it mytextview.xml, here it is:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:paddingTop="2dip"
android:paddingBottom="3dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="2dp" />
My issue is that when I go to change simple_list_item_single_choice to mytextview, it does not find my custom layout file. Anybody have any idea how to correct this?
Just replace your code
mylistAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, carList);
With this
mylistAdapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_single_choice, carList);
I hope this will help you.
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"
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.
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!