1

I have one activity with an EditText and a button. When the button is pressed, I call

myEditText.setClickable(false);
myEditText.setFocusable(false);

I have another button, which when pressed, changes the activity.

Intent myIntent = new Intent(view.getContext(), DestinationScreen.class);
startActivityForResult(myIntent, 0);

When I return from activity2 to my main activity which has the EditText, I want it to regain the focus. That is, I want to be able to type in some new values in it. Any idea how that is possible?

I tried to do this in my main Activity

startActivityForResult(myIntent, 0);
myEditText = (EditText) findViewById(R.id.textBox);
myEditText.setClickable(true);
myEditText.setFocusable(true);
myEditText.requestFocus();

It doesn't seem to work.

rekaszeru
  • 19,130
  • 7
  • 59
  • 73

2 Answers2

0

I haven't stepped through the Android source to fact check this, but the symptoms imply to me that:

  • #onActivityResult() is called sometime before #onResume(), and
  • requesting focus probably requires the view to be shown, which isn't true yet (because the activity's view hierarchy isn't attached to its window, see View#isShown()).

As such, you can fix it by requesting focus from a runnable on the main thread, which will run immediately after the activity is resumed. In your onActivityResult() definition:

final EditText myEditText = (EditText) findViewById(R.id.textBox);
runOnUiThread(new Runnable() {
    @Override
    public void run() {
        myEditText.requestFocus();
        // Also move the cursor to the end
        myEditText.setSelection(myEditText.length());
    }
});
Fabian Tamp
  • 4,416
  • 2
  • 26
  • 42
0

As you said, you'd like the EditText to regain focus when you return from the second activity.
Then probably that's what you should try: since you are already invoking the activity2 with the startActivityForResult method (requestCode: 0), you could take advantage of it:

You should override the

onActivityResult(int requestCode, int resultCode, Intent data)

method inside your main activity, check whether the requestCode == 0, and if so:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode)
    {
        case 0:
            EditText myEditText = (EditText) findViewById(R.id.textBox);
            myEditText.setClickable(true);
            myEditText.setFocusable(true);
            myEditText.requestFocus();
        default:
            break;
    }
}
rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • @rekaszeru , I am still not able to edit the text. Whatever I had entered previously is still visible and I am not able to type in anything. – Pradeep P Bomble Apr 22 '11 at 18:40
  • Make sure that your myEditText is initialized locally, it shouldn't be a global variable – rekaszeru Apr 22 '11 at 18:53
  • I am able to do this in my main activity after the return from activity2 b = (Button) findViewById(R.id.code); b.setClickable(true); I am still not able to use EditText myEditText = (EditText) findViewById(R.id.textBox); myEditText.setClickable(true); myEditText.setFocusable(true); myEditText.requestFocus(); to good effect. – Pradeep P Bomble Apr 22 '11 at 18:55
  • Any help? I am still not able to get it. – Pradeep P Bomble Apr 22 '11 at 19:07
  • Do you get any exception? Does the onActivityResult method of your main activity get called? – rekaszeru Apr 22 '11 at 19:17
  • Nothing. I am able to make the buttons clickable again by using b.setClickalbe(true); I am not able to do the same for EditText. I have declared all the variables locally – Pradeep P Bomble Apr 22 '11 at 19:19
  • However, I am able to use myEditText.setText("SomeTEXT"); Only that is working. – Pradeep P Bomble Apr 22 '11 at 19:21
  • Yes, onActivityResult method of my main activity gets called. That is where I have added, b.setClickalbe(true); – Pradeep P Bomble Apr 22 '11 at 19:22
  • I'll look into it as soon as i get home. – rekaszeru Apr 22 '11 at 19:38
  • @rekaszeru, Thanks a lot. It's working fine now. I just called myEditText.setFocusableInTouchMode(true); – Pradeep P Bomble Apr 22 '11 at 19:58
  • Please don't forget to post your solution here, and accept it (for accepting your own answer, you have to wait 2 days), so that this issue to be marked as solved, and others can learn from it. thanks! – rekaszeru Apr 22 '11 at 21:52