Select all text when EditText has focus doesn"t work - android

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

Related

Show/Hide hint text for editText when enable fullsreen mode

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?

How to highlight text when edittext gets focused?

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

Android - Do something *after* text is changed in EditText

I build an android application. I have an EditText. I want to save changes (or anything else) automatically after the user change the text.
Now I use
editText.addTextChangedListener(textWatcher);
and
TextWatcher textWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
...
}
But it saves the changes after every little change (add \ remove a letter), and I want that it'll be saved only after the user finished (closed the keyboard, clicked on a different edittext, etc. ).
How can I do that?
Implement onFocusChange of setOnFocusChangeListener and there's a boolean parameter for hasFocus. When this is false, you've lost focus to another control and you should save the data of editext. for example
EditText editText= (EditText) findViewById(R.id.editText);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
//SAVE THE DATA
}
}
});
Implement setOnFocusChangeListener for all EditTexts and you should handle Back Press event too. If the user change the data of edittext and didn't goto another EditText and pressed back then edited vallue will not save. So should use onKeyDown or onBackPressed too
As Kotlin is now official android language:
var editText = findViewById<EditText>(R.id.editText)
editText.onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
if (!hasFocus) {
//SAVE THE DATA
}
}

Android - EditText in ListView and OnFocusChangeListener

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

EditText focus lost

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

Categories

Resources