Android spinner - always select index 0 - android

I have a spinner in my layout and I would like to select only first option (index 0) no matter user selection have been made. I have some code, but there is some text flicker when spinner change rapidly from user selection to index 0. So I would like to make no user selection / select index 0 at first.
This is my code:
final Spinner onespinner = (Spinner) findViewById(R.id.spinner0);
String[] items0 = new String[] { "item 1", "item 2", "item 3","item 4","item 5" };
ArrayAdapter<String> adapter0 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, items0);
onespinner.setAdapter(adapter0);
onespinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// automaticaly select first option
onespinner.setSelection(0,true);
// but there is some text flicker
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});

Related

How do you create a dropdown answer in android?

I'm looking for something similar to a drop down menu but when you click on it a block of text drops instead of a list of clickable items. Then you should be able to click to close it again when finished. My app is an informational app so, I want to make a list of several of these within a glossary in my app.
Any ideas??
you should use Spinner in android:
Here is the example :
Spinner mSipnner = (Spinner)findViewById(R.id.spin_beneficiary_targetcode);
List<String> categories2 = new ArrayList<String>();
categories2.add("Choose code");
categories2.add("001");
categories2.add("002");
categories2.add("003");
categories2.add("004");
categories2.add("005");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories2);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
////Spinner item selected
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String targetCode_item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
//Toast.makeText(parent.getContext(), "Selected: " + category_item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
// attaching data adapter to spinner
mSpinner.setAdapter(dataAdapter);

Android: how get ID of an item selected in a spinner?

I have this private method to render a spinner in a fragment:
private void renderSpinner() {
List<String> spinnerArray = new ArrayList<String>();
for (int i = 0; i<mPromotionList.size(); i++) {
ModelPromotion modelPromotion = (ModelPromotion) mPromotionList.get(i);
String name_promotion = modelPromotion.getName();
int id_promotion = modelPromotion.getIdPromotion();
spinnerArray.add(name_promotion);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mBaseApp, android.R.layout.simple_spinner_item, spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinnerPromotion.setAdapter(adapter);
mSpinnerPromotion.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String name_promotion_selected = parent.getItemAtPosition(pos).toString();
//TODO REMOVE
Log.d(LOGTAG, "Il nome della promo selezionata รจ " + name_promotion_selected);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
As you can see, I have "ready" the id_promotion (It's the real value that I need)... And I know that I'm not adding (for now) at the List.
How I can get it, inside the onItemSelected?
Thank you very much
You can use the position of the selected item and use that to get the ID from the original list.
((ModelPromotion) mPromotionList.get(pos)).getIdPromotion()
Another option is to create a custom ArrayAdapter subclass that will accept ModelPromotion instead of String and override getItemId to return the idPromotion
An example about custom ArrayAdapter on Github.

Android change value of Spinner

I created 2 Spinners and I to fill them with an array of numbers. What I want is that the user chooses the number they want on the first one, and then I save it to a variable that I use to populate the dropdown list of the second one with values that start with that number. How can I do this?
How can I change the text in the second spinner to start with the number chosen.
I have this, I store the item of the Spinner on the variable x, and I want a second Spinner to start with that variable.
mySpinner
.setAdapter(new ArrayAdapter<String>(NewGuiaActivity.this,
android.R.layout.simple_spinner_dropdown_item,
establist));
// Spinner on item click listener
mySpinner
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
// TODO Auto-generated method stub
// Locate the textviews in activity_main.xml
x = estab.get(position).getID();
Toast.makeText(getApplicationContext(), x,
Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
If I understand what you want to do correctly, for example if you select the number 4 from the drop down of spinner1, you want spinner2 to be set to the 4th position in it's list:
Try something like this, say you have mySpinner1 and mySpinner2, let your class implement OnItemSelectedListener and give it a class variable private int spinner2Pos = 0;
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
Spinner spinner = (Spinner) parent;
if(spinner.getId() == case R.id.spinner_1_id){
spinner2Pos =
(spinner.getItemAtPosition(position) < mySpinner2.getAdapter().getCount())?
spinner.getItemAtPosition(position): 0;
mySpinner2.setSelection(spinner2Pos);
}
}
This should set mySpinner2 to the item you select with mySpinner1, which is stored as a class variable spinner2Pos. I haven't had time to check it so let me know if there are errors.

Select item from drop down menu and insert in body of new message to edit

I want to insert my drop down menu selected item in the edit text of body of new message so that i can edit the text and made necessary updates if required. My xml code for edit text is:
<EditText
android:id="#+id/editSMS"
android:layout_width="fill_parent"
android:layout_height="132dp"
android:gravity="top"
android:inputType="textMultiLine"
android:lines="9" />
code to select item from drop down menu
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// On selecting a spinner item
label = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "You selected: " + label,
Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
and code for new message
editSMS = (EditText) findViewById(R.id.editSMS);
public void onClick(View v) {
String phoneNo = editPhoneNum.getText().toString();
String sms = label + editSMS.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),"SMS faild, please try again later!",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
Code to load spinner data
private void loadSpinnerData() {
// database handler
DbHelper db = new DbHelper(getApplicationContext());
// Spinner Drop down elements
List<String> lables = db.getAllNames();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
Add do your drop down item selected listener. Cast parent to appropriate view class first, and then get selected item
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// On selecting a spinner item
label = ((Spinner) parent).getSelectedItem().toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "You selected: " + label,
Toast.LENGTH_LONG).show();
EditText etSMS= (EditText) findViewById(R.id.editSMS);
etSMS.setText(label);
}

How to capture List Dialog result - Android

I am new to Android. I am stuck, need help.
I have a List as my Activity. OnItemLongClickListener for any element of a List, I am displaying a Dialog with Custom List (Red, Green, Blue) & on the selection of any of the items (Red, Green, Blue) I need to change the back color of the selected item (on which the event raised the Dialog) of a List (main activity).
The dialog box pops, but I am stuck how to get the selected item (of Dialog). Below is my code.
public class SimpleList extends ListActivity
{
String[] contactNames = {"Name 1", "Name 2", "Name 3", "Name 4", "Name 5", "Name 6"};
ArrayAdapter<String> contactAdpater;
String itemSelected;
String choosenColor;
private final Context context = this;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ArrayList<String> myContactList = new ArrayList<String>(Arrays.asList(contactNames));
OnItemLongClickListener itemChangeColorListener = new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View arg1, int position, long arg3) {
itemSelected = parent.getItemAtPosition(position).toString();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
final String[] colorNames = {"Red","Green","Blue"};
builder.setTitle("Pick a Colour!")
.setItems(colorNames, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
choosenColor = colorNames[which];
//Toast.makeText(getApplicationContext(), colorNames[which], Toast.LENGTH_SHORT).show(); <<-- its working fine here
//I am not able to access parent here... I want to perform,
**//parent[position].setBackgroundColor(Color.RED); in case Red is selected from Dialog**
}
});
builder.show();
return false;
}
};
contactAdpater = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
myContactList);
setListAdapter(contactAdpater);
getListView().setOnItemLongClickListener(itemChangeColorListener);
}
}
Yes got your problem
You have to use view's setBackgroundColor(int color); for changing the color if selected item of first ListView with the color selelcted from Dialog.
So In onClick you must use :
choosenColor = colorNames[which];
if(choosenColor.equals("Red"))
{
view.setBackgroundColor(Color.RED);
}
else if(choosenColor.equals("Blue"))
{
view.setBackgroundColor(Color.BLUE);
}
else if(choosenColor.equals("Green"))
{
view.setBackgroundColor(Color.GREEN);
}

Categories

Resources