Swayam
Swayam

Reputation: 16364

Android EditText loses Focus on TabChange

In my application, I have multiple tabs. In one of the tabs, I have an EditText which needs to have focus by default when the user moves to that tab.

But, when the tab is changed, the EditText loses focus.

I tried all this.

In my Xml file to the EditText to which I want to have the focus.

<requestFocus />

In my java code onCreate()

userIdInput.post(new Runnable() 
    {
      public void run() 
       {
        userIdInput .requestFocus();
       }
    });

And also,

userIdInput.setFocusable(true);
    userIdInput.setFocusableInTouchMode(true);
    userIdInput.requestFocus();

But still, the focus is lost when the tab is changed. Can anyone tell me what I might be missing here ?

Upvotes: 0

Views: 609

Answers (1)

Arun George
Arun George

Reputation: 18592

Assuming you have a different container layout for each of your tabs, implement the setOnFocusChangeListener() for that container layout (one which contains the edit text) and inside the onFocusChange() check if the container layout has focus, if yes, use

userIdInput.post(new Runnable() 
{
  public void run() 
   {
    userIdInput .requestFocus();
   }
});

Upvotes: 1

Related Questions