Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 EditText is was editing before this spinner. The Focus remains with the last EditText i was editing. Is there a way i can keep focus with the current selected spinner.

I am looking for a solution that can be implemented on all spinners from layout XML or some generic method to apply to all spinners.

like image 363
isumit Avatar asked Apr 15 '14 05:04

isumit


3 Answers

Here's my solution.

mSpinner.setFocusableInTouchMode(true);
mSpinner.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            if (mSpinner.getWindowToken() != null) {
                mSpinner.performClick();
            }
        }
    }
});
like image 87
J P Avatar answered Oct 15 '22 11:10

J P


Use this to avoid EditText Focus :

scrollView = (ScrollView) findViewById(R.id.scrollView_id);
        scrollView.setOnTouchListener(new OnTouchListener() {
            // to solve foocus problem on scrolling
            public boolean onTouch(View v, MotionEvent event) {
                if (myEditText.hasFocus()) {
                    myEditText.clearFocus();
                }
                if (myEditText1.hasFocus()) {
                    myEditText1.clearFocus();
                }

                return false;
            }
        });
like image 40
Amresh Avatar answered Oct 15 '22 11:10

Amresh


I solved the problem with the help of Ryderz answer. here is the code :

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
scrollView = (ScrollView) findViewById(R.id.sv_main);
    scrollView.setOnTouchListener(new OnTouchListener() {
        // to solve focus problem on scrolling
        public boolean onTouch(View v, MotionEvent event) {

            IBinder windowToken = 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();
            return false;
        }
    });

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.

like image 3
isumit Avatar answered Oct 15 '22 09:10

isumit