How to make a Button act like a Spinner - android

I want a Button pull up a menu like a Spinner but it doesn't need to store data like the prompt in a Spinner.
A Spinner looks like this:
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.social_list, android.R.layout.simple_gallery_item);//select_dialog_multichoice);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
I need to work same thing as a Button... Thanks

i think you should put spinner.performClick(); on button click method

You can make use ofContextMenu.
Here is a link about Context Menu Demo.
http://mobile.dzone.com/news/context-menu-android-tutorial
But little modifications are required. Inside the button click event, you have to open the ContextMenu.

just open the dialog with list on button click will be looks same as spinner .....
as in
http://saga-androidapplication.blogspot.in/2011/05/dialog-list-item.html
http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList
use Resources res = getResources();
final String[] items = res.getStringArray(R.array.social_list);
//final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
Android Custom List Dialog

Related

Which layout will make a similar dropdown list like standard listbox dropdown list?

I found and tried this code from developer.android.com to create a spinner populated from a string-array resource :
ArrayAdapter<CharSequence> produits_adapter = ArrayAdapter.createFromResource(this, R.array.produits_array, android.R.layout.simple_spinner_item);
produits_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
I don't like the design of the dropdown list after clicking the arrow of the spinner :
I want a dropdown list like standard listbox's dropdown list which is also situated below the spinner. So what is the best layout for that instead of android.R.layout.simple_spinner_dropdown_item ?
You may implement your own structure easily with TextView and ListView
You can use AlertDialog.Builderfor showing list.
AlertDialog dialog;
CharSequence[] items = {"A", "B", "C"};
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle("List")
.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if(item==0){
}
else if(item==1){
}
else if(item==2){
}
}
});
dialog=builder.create();
dialog.show();

How to set tittle for spinner in android

For spinner how to set tittle ,i tried with 'prompt' in both xml and activity file ,it is showing as tittle for dropdown list after clicking on spinner but i want to give tittle for spinner how to do it,help me.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setPrompt(getResources().getString(R.string.hello_world));
Try this :
spinner.setPrompt("Select Things");
I ended up using a Button instead. While a Button is not a Spinner, the behavior is easy to customize.
First create the Adapter as usual:
String[] items = new String[] {"One", "Two", "Three"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, items);
Note that I am using the simple_spinner_dropdown_item as the layout id. This will help create a better look when creating the alert dialog.
In the onClick handler for my Button I have:
public void onClick(View w) {
new AlertDialog.Builder(this)
.setTitle("the prompt")
.setAdapter(adapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO: user specific action
dialog.dismiss();
}
}).create().show();
}
And that's it!
use that
<Spinner
android:id="#+id/spinner3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/gender"
android:prompt="#string/Gender_prompt1"
/>
and string.xml
<string name="Gender_prompt1">Choose a Gender</string>

Spinner won't display items (switch situation : different item lists for one and only one spinner - code inside)

My application works this way : there is a menu with different buttons ("Category 1", "Category 2", etc.) and all these buttons open the same activity called "ResultListViewActivity", which displays a ListView with content from a database, depending on the button you clicked before.
So there is ONE activity displaying a listview whose content varies. I am trying to add a "filter mode" for this list view. I have a "filter button" opening an alertdialog window, this alert dialog has a spinner inside. I am working on a way to display different content in this spinner, and then to assign different listeners to each item. So for example, if on the main menu you click on "Category 1" button, it sets a variable "filterVariable" to the value 1, if you click "Category 2" it would be to value 2, and so on.
I have written this with a switch attribute but it crashes my app with a pointer null exception. Here is my code:
OnClickListener filter_listener = new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater li = LayoutInflater.from(context);
View dialogView = li.inflate(R.layout.alertdialog_filter, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
Bundle bundle = getIntent().getExtras();
int filterVariable = bundle.getInt("filterVariable");
switch (filterVariable) {
// if you click on button "category 1" in main menu
case 1:
ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(ResultListViewActivity.this, R.array.spinner_category1, R.id.alertdialog_spinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
break;
// if you click on button "category 2" in main menu
case 12:
ArrayAdapter<?> adapter1 = ArrayAdapter.createFromResource(ResultListViewActivity.this, R.array.spinner_category2, R.id.alertdialog_spinner);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter1);
break;
}
alertDialogBuilder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Lorsque l'on cliquera sur annuler on quittera
// l'application
finish();
}
});
// set alertdialog_filter.xml to alertdialog builder
alertDialogBuilder.setView(dialogView);
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}; }
It really drives me crazy, I had managed to do it in the case of a popup menu ( my code is here for those who might be interested: How to set specific listeners for varying popup menus ? (code inside) ) but now it's not popup menu anymore but spinner, everything is different and what I did for popup menu is of no use.

spinner with out "select option" but it should show blank

I am creating a spinner. The spinner shows the first row value as the default text.
I want the Spinner's text to be blank initially.
I could add a new empty row with list.add(" "); but I think that this approach looks ugly.
list.add("");//this make my ui ugly but with out this i can't make my spinner blank in starting.
list.add("1");//if i remove add("").then spinner take add("1") this should not happen
list.add("2");
How do I create a Spinner that initially doesn't display any text?
Update:
urineGlucoseSpinner = (Spinner) view.findViewById(R.id.spnner_urine_glucose);
ArrayList<String> ugList = new ArrayList<String>();
ugList.add("select");
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.setOnItemSelectedListener(new OnUGItemSelected());
Write this code in oncreate method:
Spinner gender = (Spinner) findViewById(R.id.gender);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.gender_array,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
gender.setAdapter(adapter);
gender.setSelection(0);
and copy the below code and paste in string.xml
String.xml
<string-array name="gender_array">
<item> </item>
<item>Male</item>
<item>Female</item>
</string-array>
it will work properly.
You should include extra entry in adapter that represents "Select" or something , and make it the initial selected item in the Spinner.
OR may be you will have to create custom spinner or something which i am not sure of.
Take image look like dropdown and put them in background of textview.
Textview gender=(Textview)findViewById(R.id.gender)
gender.setText("");
gender.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder conductor = new AlertDialog.Builder(
Calculator.this);
conductor.setTitle("Select Gender");
int resId = getResources().getIdentifier("gender_array",
"array", getPackageName());
conductor.setItems(resId,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int index) {
int resId1 = getResources().getIdentifier(
"gender_array", "array",
getPackageName());
gender.setText(getResources()
.getStringArray(resId1)[index]);
}
});
AlertDialog alert = conductor.create();
alert.show();
}
});
String.xml
<string-array name="gender_array">
<item>Male</item>
<item>Female</item>
</string-array>

How to display an arraylist in AlertDialog

i'm trying to get an arraylist on alert dialog but i can see the list items only if i click them. any idea whats wrong with the below code .any suggestions pls....
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final ArrayAdapter<String> aa1=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_single_choice, matches);
builder.setSingleChoiceItems(aa1, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
numbers.add(aa1.getItem(item));
aa.notifyDataSetChanged();
dialog.dismiss();
}
});
alert = builder.create();
alert.show();
I believe this is caused by a bug when using the default themes and AlertDialog.Builder.
You should be able to work around it by copying the android.R.layout.simple_list_item_single_choice layout xml out of the platform and creating a local layout file with the android:textColor properties overridden to something other than themed text color attributes.
Just use this one
android.R.layout.simple_spinner_dropdown_item
simple dialog here try this. Just you have to pass string or charsequence array to it shows simple dialog..

Categories

Resources