Set Focus On Spinner When Selected In Android
I have a vertical scrolling layout with multiple EditText and Spinner controls. Problem is when i choose any spinner option after selection the screen scrolls back to the last Edi
Solution 1:
Here's my solution.
mSpinner.setFocusableInTouchMode(true);
mSpinner.setOnFocusChangeListener(newView.OnFocusChangeListener() {
@OverridepublicvoidonFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
if (mSpinner.getWindowToken() != null) {
mSpinner.performClick();
}
}
}
});
Solution 2:
Use this to avoid EditText Focus :
scrollView = (ScrollView) findViewById(R.id.scrollView_id);
scrollView.setOnTouchListener(newOnTouchListener() {
// to solve foocus problem on scrollingpublicbooleanonTouch(View v, MotionEvent event) {
if (myEditText.hasFocus()) {
myEditText.clearFocus();
}
if (myEditText1.hasFocus()) {
myEditText1.clearFocus();
}
returnfalse;
}
});
Solution 3:
I solved the problem with the help of Ryderz answer. here is the code :
InputMethodManagerimm= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
scrollView = (ScrollView) findViewById(R.id.sv_main);
scrollView.setOnTouchListener(newOnTouchListener() {
// to solve focus problem on scrollingpublicbooleanonTouch(View v, MotionEvent event) {
IBinderwindowToken=null;
if (myEditText1.hasFocus()) {
myEditText1.clearFocus();
windowToken = myEditText1.getWindowToken();
}
if (myEditText2.hasFocus()) {
myEditText2.clearFocus();
windowToken = myEditText2.getWindowToken();
}
if (myEditText3.hasFocus()) {
myEditText3.clearFocus();
windowToken = myEditText3.getWindowToken();
}
if (windowToken != null) {
imm.hideSoftInputFromWindow(windowToken, 0);
}
scrollView.requestFocusFromTouch();
returnfalse;
}
});
Then I set the android:focusable="true" for my textViews so that on scroll when focus is removed from editText then the Textviews can be picked for focus. In that way the user doesn't see any focused control on screen.
Solution 4:
I had the same problem like you and I've resolved with this solution.
I've redefined OnItemSelectedListener and when the spinner changes the value, I take the focus and after that, I release again.
This is my class CustomOnItemSelectedListener:
publicclassCustomOnItemSelectedListenerimplementsAdapterView.OnItemSelectedListener{
private Spinner mSpinner;
privateint iCurrentSelection;
publicCustomOnItemSelectedListener(Spinner s){
mSpinner = s;
iCurrentSelection = mSpinner.getSelectedItemPosition();
}
@OverridepublicvoidonItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if (iCurrentSelection != arg2) {
mSpinner.setFocusableInTouchMode(true);
mSpinner.requestFocus();
mSpinner.setFocusableInTouchMode(false);
mSpinner.clearFocus();
}
iCurrentSelection = arg2;
}
@OverridepublicvoidonNothingSelected(AdapterView<?> arg0) {
//No hace falta hacer nada
}
}
In my code I do this
Spinnerspinner= (Spinner) rootView.findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(newCustomOnItemSelectedListener(spinner));
If you want to keep focus, don't do "clearFocus()".
Post a Comment for "Set Focus On Spinner When Selected In Android"