I am working on a text editor.
I have an EditText widget in which the text is entered and a line below with various formatting widgets such as buttons and Spinners.
The user can select text and then press a button to switch bold, italic and other formatting options on and off and this works without trouble.
However, for the Spinners, by which the user can select things like color or fonts, on an Asus tablet running Android 4.1 or a Nexus 4/7/10 running 4.2.2, the text selection disappears as soon as the Spinner is tapped.
On a Samsung Galaxy SII, this behavior does not happen. The selection is kept throughout the interaction with the Spinner. (It runs Android 4.0.4 and I am not sure if the issue is Android version related or due to Samsung doing something differently).
I would like to have the behavior of Samsung on every device. How can I make sure this is the default behavior?
Creating a custom EditText class with the following override does the trick:
@Override
public void onWindowFocusChanged (boolean hasWindowFocus) {
boolean hadSelection = this.hasSelection();
int start=0, end=0;
if(hadSelection) {
start = getSelectionStart();
end = getSelectionEnd();
}
super.onWindowFocusChanged(hasWindowFocus);
if(hadSelection) {
setSelection(start, end);
}
}
I am not totally sure that in some cases it may not create some unwanted side-effects. If you use this and encounter any issue, please post a comment or publish your solution that avoids them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With