EditText focus lost - android

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

Related

Select all text when EditText has focus doesn"t work

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

Disabling edittext searchview after user input

How to disable EditText searchview after completion of input on fragment?
You can set View.OnFocusChangeListener to your editText and after focus change call setEnabled(false). for eg.
mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
//let the user input
} else {
//disable you search view
mEditText.setEnabled(false);
}
}
});
P.S. -If you are still facing a problem, please update your code snippet.
If user have to click button after enter text into searchview, you can handle this in event button click method. In your button click method you can write:
search.setEnabled(false);

How Do I Use setOnFocusChangeListener with RecyclerView?

I have the following on onBindViewHolder() in Adapter Class for RecyclerView:
holder.answerEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
String answer = holder.answerEditText.getText().toString();
mDatasetAnswers.add(answer);
}
}
});
The above only returns input from the first editText in the recyclerview. What could I be doing wrong?
I would like it to return text from all EditTexts in the recyclerview.
This happens because of the keyboard which pops up once you click on any Edit box in a recyclerview because onbindview is called and the focus changes to the first box in the recyclerview as all rows are reinflated again.
Hence, monitor for on focus gain and do ur stuff first before keyboard pops up. Hope this helps.
Try to add these lines:
holder.answerEditText.setFocusable(true);
holder.answerEditText.setFocusableInTouchMode(true);
With these lines you make sure that the component can capture the focus.

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

Categories

Resources