How to set tittle for spinner in android - 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>

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();

Custom row Layout for a Multiple Choice Dialog

I would like to implement a Custom Multiple Choice Dialog, so following the directions from this answer, this is what I did:
I created the xml for my row, which is a CheckedTextView
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#000000"
android:fontFamily="sans-serif-condensed"
style="?android:textAppearanceSmall"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:clickable="true"/>
Now, my Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setAdapter(
new ArrayAdapter<String>(getActivity(),
R.layout.dialog_list_item, tables), null)
// Set the action buttons
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alerDialog.show();
As you can see, I have not implemented an setOnItemClickListener yet, but I do not know how though. However, the Dialog looks fine. How can I call a ClickListener for each of the CheckedTextView?
Than you very much
The way that #Rahul Gupta suggested works after implementing a ListView and OnItemClickListener, but if the ListView contains too many items, the Views that are not shown are going to be "checked" as well. I suppose the Views are not yet generated until the user actually scrolls down and see the rest of the items.
The way that I made it work was creating a Layout with a single ListView and setting the value of choice mode to CHOICE_MODE_MULTIPLE. In that case, I don't have to deal with each item, but I can retrieve an array of the items selected by using listview.getCheckedItemPositions()
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater li = LayoutInflater.from(getActivity());
View view= li.inflate(R.layout.listview_dialog, null);
builder.setView(view);
builder.setTitle(title);
listView = (ListView) view.findViewById(R.id.listview);
ArrayAdapter<String> ad = new ArrayAdapter<String>(getActivity(), R.layout.dialog_list_item , R.id.text2, tables);
listView.setAdapter(ad);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setDivider(null);
builder.create();
Yes, you can implement thee onClickListener for your CheckedTextView.
You just have to find its reference. Now as it is in your dialog, you will have to do this :-
CheckedTextView dialogCheckedTextView = (CheckedTextView ) alertDialog.findViewById(R.id.yourcheckedtextboxid);
dialogCheckedTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO
}
});

How to make a Button act like a Spinner

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

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