I want to display a Drop down that contain the items from Array List when click on button on action bar.
Is there any good practice/tutorial to accomplish this?
i think you can somthink like that put the arraylist as the spinner database
Spinner mySpinner = (Spinner) findViewById(R.id.my_spinner);
//adapter helps to add data to spinner.
ArrayAdapter<String> adapter = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_spinner_dropdown_item,user_spinner_string);
mySpinner.setAdapter(adapter);
Related
I have an activity that recieves two variables called l_source and l_destination and I want to set the default values of two spinners equal to those variables. To do so, I have made a search and I understand that I need to get the position of the value of the variable (will give a number) and then set the new position of the spinner. The problem is that I am trying to use getAdapterPosition and I am sent Cannot resolve method getAdapterPosition(l_source).
EDIT
I finally managed to do it, but it is only working in one spinner. Have you got any idea of why? Here is my code:
Spinner spinner = (Spinner) findViewById(R.id.lista_origen);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(Main2Activity.this,
R.array.l_source, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(adapter.getPosition(l_source));
Spinner spinner2 = (Spinner) findViewById(R.id.lista_destino);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(Main2Activity.this,
R.array.l_source, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setSelection(adapter2.getPosition(l_target));
Thank you for your time.
get position of selected item is spinner by int spinnerPosition = spinner.getSelectedItemPosition();
need to do so when you click on a specific button (application type translator and arrow key) changed one of the selected items to another spinner itam swapped Well, here is how it can be implemented?
here is the code but it does not work!
int spinner1Index = spinner.getSelectedItemPosition();
spinner.setSelection(spinner2.getSelectedItemPosition());
spinner2.setSelection(spinnerfirst);
can offer something similar or fix!
You would have to change the adapter of your spinner and call notifyDataSetChanged()
I would suggest adding this to your switch case for when the item is selected.
Set a custom adapter for the spinner on item click.
//An array of what you want to populate the spinner with
Array spinnerArray [] = {"One", "Two", "Etc.."};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, spinnerArray);
spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
//Before setting the adapter, clear and notify the previous ArrayList used
arrayList.clear();
arrayList.notify();
spinner.setAdapter(spinnerArrayAdapter);
I want to have a drop down spinner in my app, but when I tapped on spinner, spinner open a dialog and show the items in that in fact I want to have a spinner like this image:
but, my spinner is like this
Thanks.
You can try the below code : - Where mMyArray is array of String to be displayed. mAcitvity is context .
Spinner mMySpinner = (Spinner)findViewById(R.id.myValues);
ArrayAdapter<String> mMyadapter = new ArrayAdapter<String>(mActivity,
android.R.layout.simple_spinner_item, mMyArray);
mMyadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mMySpinner.setAdapter(mMyadapter)
Let me know if it works
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 have an activity which has a Spinner widget to display categories. Initially I was using an ArrayAdapter to populate the the spinner as in the following code
private static final String[] arrayCategories = {
"Business",
"Personal"
};
mCatSpinner = (Spinner) findViewById(R.id.thecategory);
ArrayAdapter<String> catAdapter = new ArrayAdapter<String>(this, R.layout.track_category_item, arrayCategories);
catAdapter.setDropDownViewResource(R.layout.track_category_dropdown_item);
mCatSpinner.setAdapter(catAdapter);
This works fine, and the spinner displays the first array item by default if no selection is made. It does show the selected item when an item is actually selected
But now I want to use a SimpleCursorAdapter to pull the list contents from a db. So I changed it to
SimpleCursorAdapter scaCategories = new SimpleCursorAdapter(this, R.layout.track_category_item,cCategories,new String[] {DBAdapter.KEY_CATEGORIES_NAME},new int[]{R.id.text1});
scaCategories.setDropDownViewResource(R.layout.track_category_dropdown_item);
mCatSpinner = (Spinner) findViewById(R.id.thecategory);
mCatSpinner.setAdapter(scaCategories);
This populates the dropdown, but it does not display the first item in the spinner. Even if selected, it does not show the selected item.
I tried to setSlection to the first item using
if(mCatSpinner.isSelected() != true) {
mCatSpinner.setSelection(0);
}
but it didn't work
What is wrong?
Ok, it would help if I specified the widget id in the layout xml. <:(