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()); } } }); 

3 Answers 3

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()); } } } 
Sign up to request clarification or add additional context in comments.

12 Comments

Interesting, I will try that. I am new to Android programming, what does the "!" exclamation point do?
it means negation, i.e. not initialized, when initialized is true, !initialized has the value false
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?
developer.android.com/guide/topics/resources/… 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.
stackoverflow.com/questions/32434609/… this is one of the problem you would face if you register configChanges
|
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()); } 

2 Comments

Ok, I'm curious which solution would be more efficient, your "newConfig" solution or the "onResume" method recommended by Derek Fung?
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, my solution will avoid activity restart and won't update resources, so more efficient.
0

add requestFocus in public void onRestoreInstanceState(Bundle savedInstanceState)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.