I have an edittext and a spinner with two items (" " and "1"). I disabled my edittext. Now what I want to do is enable my edittext when I select 1 in my spinner. But it's not enabling.
public void onCreate(Bundle savedInstanceState) {
.......
enable();
}
public void enable() {
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setAdapter(ArrayAdapter.createFromResource(this, R.array.spinner1, android.R.layout.test_list_item));
EditText edit1 = (EditText) findViewById(R.id.edit1);
String s1 = spinner1.getSelectedItem().toString();
edit1.setEnabled(false);
if (s1.equals("1")) {
edit1.setEnabled(true);
edit1.setFocusable(true);
edit1.setFocusableInTouchMode(true);
}
}
You must implement the method onItemSelected:
Callback method to be invoked when an item in this view has been
selected. This callback is invoked only when the newly selected
position is different from the previously selected position or if
there was no selected item.
public abstract void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
}
you should create on change item listener and set it to your spinner. This where you should post the code for enabling the edittext
Use the Spinners onclick listener to 'hear' when the spinner has been interacted with, take the value of the spinner inside the onclick and manipulate other objects as you need.
Extent OnItemSelectedListener of Spinner class and on
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
Take the action of enabling and disabling the edit text.
Related
I have a spinner and I want to get the last selected value of it when I click on the spinner (basically just before the items in the list are being shown).
How can I achieve this?
Update:
Example: There are a dynamic amount of spinners. User clicks in spinner 1 and selects a value, let's say "house". Then clicks somewhere else. Then clicks in spinner 1 again. What I now need is to return the value "house" that was previously was selected before the user selects a different value i.e. "car" in that spinner. I can not use the local storage to save that value beforehand because it's going to be a dynamic amount of spinners to be added.
And yes, I did in fact read the documentation at http://developer.android.com/guide/topics/ui/controls/spinner.html already but what I need isn't explained there.
Maybe you can declare a String array and then use it to storage the items values. Showing this in a TextView is a way to see if it works.
Something like this:
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
selection.setText(items[arg2]);
}
I already have faced a similar situation, but I don't remember exactly how I solved it. Tell me if it gives a better idea to you, I'm new participating here.
Spinner mySpinner=(Spinner) findViewById(R.id.your_spinner);
String text = mySpinner.getSelectedItem().toString();
When the user selects an item from the drop-down, the Spinner object
receives an on-item-selected event.
To define the selection event handler for a spinner, implement the
AdapterView.OnItemSelectedListener interface and the corresponding
onItemSelected() callback method. For example, here's an
implementation of the interface in an Activity:
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
...
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
The AdapterView.OnItemSelectedListener requires the onItemSelected()
and onNothingSelected() callback methods.
Then you need to specify the interface implementation by calling
setOnItemSelectedListener():
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
IN YOUR CASE:
TO se if the user clicked on the spinner, do this:
if (spin.getSelectedItemPosition() < 0) {
Spinner mySpinner=(Spinner) findViewById(R.id.your_spinner);
String text = mySpinner.getSelectedItem().toString();
}
This means user clicked the spinner, but not any items of it.
EDIT AGAIN
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
> ...
>
> public void onItemSelected(AdapterView<?> parent, View view,
> int pos, long id) {
> parent.getItemAtPosition(pos) //THIS GETS YOU THAT VALUE THAT THE USER CLICKED ON DYNAMICALLY
> }
>
> public void onNothingSelected(AdapterView<?> parent) {
> // Another interface callback
> }
> }
http://developer.android.com/guide/topics/ui/controls/spinner.html
Solution is to add a TextView widget with each added Spinner. Set the TextView to "gone" (not visible). When the spinner changes set the value to that TextView. This way you can store the last selected spinner value in the TextView. When the user clicks on the spinner and changes the value you can still access the previously selected/unselected value from the spinner through the TextView widget.
// Use this TextView to temporarily store the spinner value
final TextView hiddenTextView = new TextView(getContext());
hiddenTextView.setVisibility(View.GONE);
final Spinner spinner = new Spinner(getContext());
spinner.setPrompt(getString(R.string.email_other));
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(), R.array.email_types, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Log.d(TAG, "TextView Value before spinner change: " + hiddenTextView.getText());
String spinnerValue = String.valueOf(spinner.getSelectedItem());
hiddenTextView.setText(spinnerValue);
Log.d(TAG, "TextView Value after spinner change: " + hiddenTextView.getText());
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
return;
}
});
My codes goes like this.
spinner.setAdapter(mAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
chosenPosition = position;
spinner.setEnabled(false);
}
My intention is to save the position chosen by user and store it and immediately disable the spinner. But, when I set the Adapter, the first item automatically gets selected and the spinner gets disabled. How do I prevent it from calling the onItemSelected method when the Adapter is set?
Thank You.
Define a flag on top such as :
boolean isFirstSelection = true;
and in your onItemSelected method:
spinner.setEnabled(isFirstSelection);
if(isFirstSelection) {
isFirstSelection = !isFirstSelection;
}
I build a spinner in which i put element through array list and .i store the value of get selcted item from spinner in a variable,in my spinner it having 15 element,now i want to try when a specific item is selected from spinner then a dialog box is open with edit text and button then user can edit and save it in spinner.how can i do this.
my code for spinner is
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.faultid);
addItemsOnSpinner2();
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection()
{
Spinner faultSpinner = (Spinner) findViewById(R.id.spinner1);
mspinner.setOnItemSelectedListener(new
CustomOnItemSelectedListener());
}
public void addItemsOnSpinner2() {
ArrayList<String> faulttypespinner = new ArrayList<String>();
faulttypespinner.add("XL-Cross Level");
faulttypespinner.add("AL-Alignment");
faulttypespinner.add("UN-Unevenness");
faulttypespinner.add("XL-Cross Level");
faulttypespinner.add("AL-Alignment");
faulttypespinner.add("UN-Unevenness");
faulttypespinner.add("BD-Ballast Deficiency");
faulttypespinner.add("SE-Super elevation on curve");
faulttypespinner.add("LP-Loose Packing");
faulttypespinner.add("LJ-Low Joint");
faulttypespinner.add("BA-Bridge");
faulttypespinner.add("LC-Level Crossing");
faulttypespinner.add("LJ-Low Joint");
faulttypespinner.add("P and C-Point n Xing");
faulttypespinner.add("OTH-Other Defect");
faulttypespinner.add("SEJ-SEJ");
faulttypespinner.add("WEED-Weed on Cess");
ArrayAdapter<String> faultadapter = new ArrayAdapter<String>
(mConetxt,android.R.layout.simple_spinner_item, faulttypespinner);
faultadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mspinner.setAdapter(faultadapter);
}
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " +
parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}
You'll need to implement Custom Adapter, where you'll have a method something like :
public void changeItemValue(int position, String newValue){
Item itm = getItem(position);
itm.setValue(newValue);
notifyDataSetChanged();
}
When you open the dialogBox onSelectedItem, you'll have to pass an instance of your spinner adapter to a dialog and then to the listener of a dialog button, where you'll call changeItemValue method on button click.
Why u r using custom adapter,when click on spinner get the position of clicked item and show the pop up and on button click set the value to the faulttypespinner arraylist using index stored previously. then add notifydatasetchanged for adapter to update the spinner thats all.
I have implemented multiple(seven) spinners and populated them with three options: Yes, No, and Unknown. And "Unknown" is the default option. Now I want to know whether user clicked spinner or not. Since the default option can also be a valid answer, I could not work with getSelectedItemPosition() in Spinner class.
All I want to know is whether user clicked that particular spinner or not, so that I can generate alert message depending on this Info.
The first thing you should do is read the Spinners guide on the Android developer site. Having done that, you'll find this handy example:
public class MySpinnerActivity extends Activity implements OnItemSelectedListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
Simpy,
First Set OnItemSelectedListener to each Spinner and then check in method,
if you have more spinner then getSelectedItem() using below code inside onItemSeleted Method,
String str1= (String) spinner1.getSelectedItem().toString();
String str2= (String) spinner2.getSelectedItem().toString();
Spinner OnclickListener event executes twice -
Spinner initialization
User selected manually
where as implementation of listener is as :
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Problem definition
I want to save user selected text into data storage, when user choose any item from spinner, and I am able to do this. But my another task is that to show previously selected item (access from data storage) as selected item in spinner, but each time when I call spinner's activity, spinner shows first item as default selected item, and also in data storage it make change previous item to default.
How can I make difference between 'Spinner initialization' and 'User selected manually' events?
You have to handle both events logically. As these references (Android Spinner selection, problem on spinner) says that you have to use flag variable to handle this, I am putting a code sample.
Hope this will help you to clear your logic.
public class TestActivity extends Activity {
//Checks report spinner selection is default or user selected item
private boolean isDefaultSelection;
//Spinner setup
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
// Set true at onCreate
isDefaultSelection = true;
spinner = (Spinner) findViewById(R.id.id_of_spinner);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String> (this, R.layout.drop_down_custom_row, data);
//Implement custom view for drop down of spinner
//spinnerAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(isDefaultSelection) { //If spinner initializes
spinner.setSelection("Set_here_id_of_data_item_from_storage_which_was_previously_stored");
isDefaultSelection = false;
} else { //If user manually select item
int itemPosition = spinner.getSelectedItemPosition();
//Write here code to store selection (itemPosition) of user into data storage
}
}
public void onNothingSelected(AdapterView<?> parent) {
//User selected same item. Nothing to do.
}
});
}
}
Hope it will clear your doubt.
You can call the setSelection at the same time that the items are added to the adapter, see this example: How to avoid onItemSelected to be called twice in Spinners