2

I have an EditText line on a UI screen for a user to enter a sentence. If the the user leaves the cursor in the middle of the sentence and then does an orientation change, I want the cursor to move to the end of the sentence. My understanding was that the OS creates a new Activity and new focus on orientation change so I set up the below code, but to no avail. Please advise.

partial Activity file:

...
cListenerEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
         @Override
         public void onFocusChange(View v, boolean hasFocus) {
             if (hasFocus && (cListenerEditText.getText().length() > 0)) {
                cListenerEditText.setSelection(cListenerEditText.getText().length());
             }
         }
    });
AJW
  • 1,578
  • 3
  • 36
  • 77

3 Answers3

3

Try to do this in onResume instead, checking a boolean field to have it work only on recreation.

if(!initialized) {
    initialized = true; 
    cListenerEditText.setSelection(cListenerEditText.getText().length());
}

Edit:

Sample Activity

public class MainActivity extends AppCompatActivity {

  private EditText cListenerEditText;
  private boolean initialized = false;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    cListenerEditText = findViewById(R.id.listenerEditText);
  }

  @Override
  protected void onResume() {
    super.onResume();
    if(!initialized) {
      initialized = true;
      cListenerEditText.setSelection(cListenerEditText.getText().length());
    }
  }
}
Derek Fung
  • 8,171
  • 1
  • 25
  • 28
  • Interesting, I will try that. I am new to Android programming, what does the "!" exclamation point do? – AJW Sep 08 '15 at 03:40
  • it means negation, i.e. not initialized, when `initialized` is `true`, `!initialized` has the value `false` – Derek Fung Sep 08 '15 at 03:43
  • Thank you. I'm curious as to which solution would be more efficient, your "onResume" method or Wesley's Manifest.xml solution recommended below. Thoughts? – AJW Sep 08 '15 at 03:45
  • http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange didn't want to comment on ours solution at first, but registering `android:configChanges="orientation"` means you are going to take care ALL changes needed when orientation change by yourself, so I would say that is not what you need. – Derek Fung Sep 08 '15 at 03:47
  • http://stackoverflow.com/questions/32434609/android-actionbar-not-resizing-with-onconfigurationchanged-appcompat/32434653#32434653 this is one of the problem you would face if you register `configChanges` – Derek Fung Sep 08 '15 at 03:49
  • Ok so if I am using onResume then I dont understand how "!initialized" functions. Wouldnt the fact that the Activity was resuming due to recreation of the Activity mean I would just initialize if recreation was true? – AJW Sep 08 '15 at 04:06
  • when config changes, e.g. screen rotation, system will destroy your current Activity and create a new one, so if you have a field `private boolean initialized=false;`, because it is a new Activity, the variable initialized would be false the first time `onResume` is called – Derek Fung Sep 08 '15 at 04:22
  • added a sample for you, in case you don't understand – Derek Fung Sep 08 '15 at 04:28
  • I'm not understanding. Do you mean the cursor would move to the line the second time onResume is called? If so, I thought onResume would only be called once right after onCreate and onStart. – AJW Sep 08 '15 at 05:36
  • to know when onResume is called, you have to know onPause. onPause is called when your application goes to background, e.g. pressing home button, when it returns from background onResume is called. Of course onResume is called after onStart as well – Derek Fung Sep 08 '15 at 05:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89028/discussion-between-ajw-and-derek-fung). – AJW Sep 08 '15 at 05:43
  • Did it go well for you? – Derek Fung Sep 10 '15 at 05:51
1

First, you need to add a property of the Activity in Manifest.xml file, like this: android:configChanges="orientation".

Then, in your Activity, override onConfigurationChanged, because every orientation changes, this method gets called.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    cListenerEditText.setSelection(cListenerEditText.getText().length());
}
Wesley
  • 4,084
  • 6
  • 37
  • 60
  • Ok, I'm curious which solution would be more efficient, your "newConfig" solution or the "onResume" method recommended by Derek Fung? – AJW Sep 08 '15 at 03:42
  • Mine is more efficient. If you have no `android:configChanges` tag in your `Manifest`, your Activity will restart by Android OS, hence the `onResume` works. According to this [documentation](http://developer.android.com/intl/zh-cn/guide/topics/resources/runtime-changes.html#HandlingTheChange), my solution will avoid activity restart and won't update resources, so more efficient. – Wesley Sep 08 '15 at 03:51
0

add requestFocus in public void onRestoreInstanceState(Bundle savedInstanceState)

Raju-san
  • 166
  • 1
  • 2
  • 12