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
Related
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);
I am populating my spinner from database. I have a collection of mobile brands. I have added "Add a new brand" also... But when I am setting the spinner items from DB, it comes in somewhere middle.. I want it to go at the end.. Can i do it? if yes, how? please help, thanks in advance.
Spinner brand;
brand=(Spinner)findViewById(R.id.spinner_brand);
private void loadSpinnerData() {
// database handler
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
// Spinner Drop down elements
List<String> Brand = db.getBrands();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, Brand);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
brand.setAdapter(dataAdapter);
}
Add time column in your brand table and on querying sort by time. So, you will get last added item at last in spinner.
Create an ArrayList and add all your database items to it. and then add your "Choose a brand" string to it. and then pass the ArrayList to Spinner.
Although I should warn you, you are "probably" doing it wrong. If you add "Choose a brand" item to spinner, it will also be selectable, which you might not want :)
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'm trying to create spinner which should not have any select but instead of it, it should show Blank, after clicking that items can be selected.
Here is my code, please help.
urineGlucoseSpinner = (Spinner) view.findViewById(R.id.spnner_urine_glucose);
ArrayList<String> ugList = new ArrayList<String>();
ugList.add(0,"");
ugList.add("1.5");
ugList.add("5.5");
ugList.add("0.8");
ugList.add("9.5");
ugList.add("12.0");
//ArrayAdapter<String> urineGlucoseAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, ugList);
ArrayAdapter<String> urineGlucoseAdapter = new ArrayAdapter<String>(getActivity(),R.layout.custom_spinner_text, ugList);
urineGlucoseAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
urineGlucoseSpinner.setAdapter(urineGlucoseAdapter);
urineGlucoseSpinner.setSelection(0);
urineGlucoseSpinner.setOnItemSelectedListener(new OnUGItemSelected());
By default spinner takes array 0th element if u not selecting any one..u have to make object of ArrayList and for 0th element u have to put "" (null Sting) inside semicolon and make it as 0th element...i think this is the only solution for your question..
ArrayList<String> ugList = new ArrayList<String>();
ugList.add("");
I can see two ways to do this.
1) Add the blank line to your data at position 0, and then create a custom spinner adapter and override the getView method and in it use an if to set the 0 position view to GONE (thus getting rid of the blank line in the listing).
2) An alternative might be setting an empty EditText in your form, and when it gains focus pop a listview in a dialog with your possible choices.
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. <:(