I have an editText and I set setOnFocusChangeListener
to open a spinner.
Once I select an item from the spinner, I want the selected item to show on the same edit Text.
Below is the code I have tried.
List<String> tal = new ArrayList<String>();
tal.add("1");
tal.add("2");
tal.add("3");
tal.add("4");
tal.add("5");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, tal);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View arg0, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus){
spinner.performClick();
}
}
});
editText.setText(spinner.getSelectedItem().toString()); //this is taking the first value of the spinner by default.
Xml:
<EditText
android:hint="Select A Value"
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_edittext"
android:layout_weight="0.05"
android:singleLine="true"/>
<Spinner android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner" />