Milos Cuculovic
Milos Cuculovic

Reputation: 20223

Android: Request the focus for a boutton instead of the EditText

I have some edit texts in my Scroll Layout. On the bottom, I have a button.

Actually, on each opening of the view, the first editText requests the focus automaticaly. I would like that the button gets the focus.

I have try this, but without success:

final ScrollView s = (ScrollView)findViewById(R.id.ScrollView01);
    s.post(new Runnable() {
       public void run() {
             Button button =(Button) findViewById(R.id.SaveProfileEditingButton);
             button.requestFocus();
       }
    });

Thank you for your hlep.

Upvotes: 0

Views: 104

Answers (2)

waqaslam
waqaslam

Reputation: 68177

Set android:windowSoftInputMode="stateHidden" to your activity in AndroidManifest.xml to hide the keyboard (opened due to focus on EditText). Afterwards your other views may receive the focus.


Update

final ScrollView s = (ScrollView)findViewById(R.id.ScrollView01);
s.post(new Runnable() {
       public void run() {
             EditText editText =(EditText) findViewById(R.id.yourEditText);
             (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
                   .hideSoftInputFromWindow(editText.getWindowToken(), 0);

             Button button =(Button) findViewById(R.id.SaveProfileEditingButton);
             button.setFocusable(true);
             button.requestFocus();
      }
});

Upvotes: 1

Krishnakant Dalal
Krishnakant Dalal

Reputation: 3578

Your code is working but how you checked or what's the sense to set focus on Button. To Check it is working or not use

boolean r = btn.requestFocus();
Log.i("Tag", String.valueOf(r));

Upvotes: 0

Related Questions