I have a spinner that's populated like this:
Spinner spinner = (Spinner) findViewById(R.id.m_spinner);
final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, spinnerValuesList);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
spinner.setPrompt("CPU Frequency Governor");
It's created like this:
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
android:id="#+id/my_spinner"/>
Everything works fine, but I'd like to align the text of the dialog title and the spinner items. I attached a picture to show what it currently looks like and what I'm looking to achieve.
Thanks
Instead of
final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, spinnerValuesList);
Try this:
final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1
, spinnerValuesList);
Let me know if it works for you.
Related
I would like to programmatically change a name of one of my spinner item but I am not sure how I can do this.
For example given the code below
Spinner spinner = (Spinner) MenuItemCompat.getActionView(item);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinner_list_item_array,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
I would like to
Change spinner ITEM name at position 1 to "NEW TEXT"
You should change the data in R.array.spinner_list_item_array.
After you have changed the data you will need to then call adapter.notifyDataSetChanged() and then adapter.setAdapter(adapter).
CharSequence[] fiilliste = getResources().getStringArray(R.array.spinner_list_item_array);
ArrayAdapter<CharSequence> adapter =
new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_dropdown_item, fiilliste);
spinner.setAdapter(adapter);
fiilliste[1] = "NEW TEXT";
adapter.notifyDataSetChanged();
I use two Spinner and get data from asyncTask when I get first spinner and want get the 2th spinner but when setadapter .. two spanner's data are the same...
This is i setadapter first time.
List<String> none=new ArrayList<String>();
none.add("none");
ArrayAdapter<String> adapterchoseTime =
new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, none);
adapterchoseTime.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapterchoseProm =
new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, none);
adapterchoseProm.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
choseTime.setAdapter(adapterchoseTime);
choseProm.setAdapter(adapterchoseProm);
I get Spinner 1's data
ArrayAdapter<String> adapterchoseTime=(ArrayAdapter<String>)choseTime.getAdapter();
adapterchoseTime.clear();
adapterchoseTime.addAll(time);
choseTime.refreshDrawableState();
and i get spinner2's data
ArrayAdapter<String> adapterchoseTime = (ArrayAdapter<String>)choseTime.getAdapter();
adapterchoseTime.clear();
adapterchoseTime.addAll(time);
ArrayAdapter<String> adapterchoseProm = (ArrayAdapter<String>)choseProm.getAdapter();
adapterchoseProm.clear();
adapterchoseProm.addAll(prom);
choseTime.setAdapter(adapterchoseTime);
choseProm.setAdapter(adapterchoseProm);
And then I get two same spinner...
It looks like you're referencing the same spinner from your setDropDownViewResource.
adapterchoseTime.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
One of these needs to reference the second spinner.
I have created a Spinner dynamically, which is easy with new Spinner(context).
But now I need to set the spinnerMode dynamically, and I find no method to do so.
What should I do?
in android >= 3.0 (API 11) you can use this :
Spinner spinner = new Spinner(this,null,android.R.style.Widget_Spinner,Spinner.MODE_DROPDOWN);
OK you can try with..
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, yourArrayAdapter);
spinner.setAdapter(spinnerArrayAdapter);
Or,
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, yourArrayAdapter);
spinner.setAdapter(spinnerArrayAdapter);
Also you can see..
How to set spinnerMode in Android 2.2?
Hi friends wanna remove the radio button appearing on spinner.. Which is increasing the layout size of my dialog.. Thanks in advance..
set your spinner's adapter as follow:
adapterSpinner = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, new String[]{"1", "2"});
adapterSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
For this you need to change your Spinner Style
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Registration.this,android.R.layout.simple_spinner_item, Your Array);
spinner.setAdapter(adapter);
I'm currently bereft at the moment. I'm working on an application (Android) using a spinner but "incredible is nothing" it doesn't work at all. I mean, It displays the title but the list of items doesn't show up. It would be really nice if you could give me an explanation. (I'm a newbie in this tech ...). I followed all possible deals but nothing ...
Here is my source (of the activity) :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form_find_layout);
/********************** Les composants ********************/
String[] listLoyer = {"200","250","300","350","400","450","500","550","600","650","700"};
String[] listType = {"T1", "T2", "T3", "T4"};
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listLoyer);
Spinner loyersMin = (Spinner)findViewById(R.id.min_spin);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.les_prix, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
loyersMin.setAdapter(adapter);
loyersMin.setOnItemSelectedListener(this);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, listLoyer);
Spinner loyersMax = ((Spinner) findViewById(R.id.max_spin));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
loyersMax.setAdapter(adapter1);
loyersMax.setOnItemSelectedListener(this);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, listType);
Spinner type_appart = ((Spinner) findViewById(R.id.type_spin));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
type_appart.setAdapter(adapter2);
type_appart.setOnItemSelectedListener(this);
Button loginBtn = (Button)findViewById(R.id.find_btn);
loginBtn.setOnClickListener(this);
}
Thanks for all
I've solved my trouble. For those who get the same trouble with thier spinner, make sure that your "setContentView" targets the right layout, besides, that you correctly called your activity by the "mother" (or the caller class) activity (in my case).
Hope it helps you!