In my android app, I have a bunch of edititexts that's a numeric keyboard. On the keyboard is a next key, which when clicked moves the cursor to the next edittext. How can I make it highlight the text of the new focused edit text when the next key is clicked? Basically I want to just click next, then type some new numbers which replaces the old one.
Thanks.
Add this to your EditText on xml:
android:selectAllOnFocus="true"
editText.setOnFocusChangeListener(new OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
if(hasFocus)
{
editText.setSelection(editText.getText().toString().length());
}
}
}
);
Related
I have the following preference screen on my API 29 app :
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory app:title="#string/sample_header">
<EditTextPreference
app:key="sample_id"
app:title="#string/sample_id_title"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
</PreferenceScreen>
The EditTextPreference is set to numeric input :
public void onBindEditText(#NonNull EditText editText) {
editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
}
When the user opens the sample_id preference, the text is set to the old numeric value, as expected, and the cursor is placed at the beginning of the text. It's important for the application that the current value is shown to the user.
To change that value, the user has to first scroll to the end of the text or select it, delete the old content, and then enter the new numeric text.
I would like to have the following behavior: The old text get deleted when the user types in a new text.
One implementation would be to have the text content already selected. Therefore it would be replaced as soon as the user types in a new text.
I haven't found a way to force the selection of the text on edit.
Another sub-optimal solution would be to have the cursor placed at the end of the text on edit. Just pressing a couple of backspace is more convenient than having to fist move to cursor to the end or to select the old text content.
My question is :
How to avoid the need to first delete the old content to enter a new one ?
You can overide the focus so when that editText is focus'd it can either clear the text or change the value to hint.
setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus & v instanceof EditText){
((EditText) v).setText("");
}
}
});
Set to hint
.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus & v instanceof EditText){
String value = ((EditText) v).getText().toString();
((EditText) v).setText("");
((EditText) v).setHint(value);
}
}
});
When an EditText has a focus, I want to select all text inside by default, but it doesn't work:
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
// Go to the end
editText.setSelection(getEditTextView().getText().length());
// Select all the content
editText.selectAll();
}
}
});
Thank you very much guys!
There are 2 good way to select the text in an EditText :
Inside your main.xml :
android:selectAllOnFocus="true"
Or :
editText.setSelectAllOnFocus(true);
(If you want to do it programatically)
SOURCE : Select all text inside EditText when it gets focus
How show hint text when open edittext in fullscreen mode?
I'm can show hint text with OnFocusChangeListener.
editChangeName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
editChangeName.setHint(b ? getResources().getString(R.string.name) : "");
}
});
But.
How hide hint text after closing edittext?
// assuming you have a EditText view with id
View viewById = getView().findViewById(R.id.editTextId);
if(viewById!=null) {
// set listener to this view "many views could share one listener"
viewById.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// if view match id && has no focus
if(v.getId() == R.id.editTextId && !hasFocus)
// clear hint
((EditText) v).setHint(null);
// else show hint
}
});
}
hasFocus <- as name says
has (true) -> show
doesn't have (false) -> hide
if you are not sure if view by id is one you assign to an edit text do a check ( double check will not hurt)
if (EditText.class.isAssignableFrom(v.getClass()))
// do cast
EditText editText = (EditText) v;
This code is correct. But. How hide hinttext, after closing softkeyboard.. edittext have focus – Eugenu Yansky
Detecting when user has dismissed the soft keyboard
How to remove focus without setting focus to another control?
How to clear focus and remove keyboard on Android?
I have a ListView that contains an EditText widget in each row. The user can update the numeric value in the EditText, and once the focus is lost on that EditText, I want a TextView updated on the screen that contains the sum. I'm using the below code to check if the focus has been lost, but it every time I put focus on an EditText, it enters the if statement, and I think that has to do with the fact it is contained within a ListView. I'd like it to only enter the if statement when the user removes the focus from the EditText.
MyEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
//Perform Calculation
}
}
});
In a custom ListView, there are two columns, one contains a TextView and the other a EditText component.To enter some preferences, as the user clicks on the EditText, the software keyboard comes in focus but focus from the EditText is lost. How I can do this?
Focusable EditText inside ListView
For editText use method setFocusable(false).
For textView use setFocusable(true).
Also write a listener on focus lost for both textView and editText:
textView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
{
//do job here when EditText loses focus
}
}
});