Android: How disable EditText depending focus - android

I'm doing a app wich have two EditText. What I wanna do is, if you click on one and write on it, the other must erease anything It has and only shows the hint string. I'm triying doing it witha a check method that does:
if(celsius.isFocused()){
faren.setText(faren.getHint().toString());
}
if(faren.isFocused()){
celsius.setText(celsius.getHint().toString());
}
Then I call this method within the onCreate() method, but of course It only checks one time, and if use that checkMethod inside a loop, the app doesn't show anthing, It freezes. An suggestions?

Use the OnFocusChangeListener.
faren.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
celcius.setText(""); // This will automatically show the hint text
}
});

Related

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.

Remove Focus change Listener of a view in android

I have added FocusChangeListener to EditText. like
etZip.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
{
callAreaDetailService(zipCode,false);
}
}
});
Now If I have focus on etZip and I click on Button to go to anotherActivity. etZip loses focus and show Response alert in Next Activity.
I know I can add a boolean flag for not calling service if button is clicked. But I am sure there must be an android build in method to stop focus change.
There is no function like etZip.removeFocusChangeListener . So is there any way to do this using Android SDK.

android setError method interfering with manually setting icons

I'm using the setError method to show if an EditText field is incorrectly filled when the focus from that field shifts away. If it's correctly filled, I'm showing an icon: drawable.validated Here's my code:
#Override
public void afterTextChanged(Editable s) {
zip.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
if (isValidZip(zip.getText().toString())) {
zip.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.validated, 0);
} else {
zip.setError(getString(R.string.zip_error));
}
}
}
});
The problem is, the setError method is interfering with my manually setup of icon. And thus I can't see the drawable.validated icon even when the field is verified.
MORE DETAILS on when I get the error:
CASE 1 I fill the EditText field correctly in the very first time -> I change the focus from that field -> I can see the validated icon. See the following screenshot:
CASE 2 I fill the EditText zip field incorrectly -> I change the focus to Mobile Number field-> I see error in zip field-> Get back to the zip field and fill it correctly. Error in zip disappears -> change focus to Mobile Number field-> Now I can't see the VALIDATED icon in the zip field even if it is correctly filled:
drawable.validated is the blue-tick.
12345 is the zip field
What might be the issue and how to resolve that?
First of all, why do you setOnFocusChangeListener every time text change, you don't need to do this, you can just set it once.
I have just created test project and test it, works fine
mEditText = (EditText) findViewById(R.id.edit_text_input);
mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
if (isValidZip(mEditText.getText().toString())) {
mEditText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_validated, 0);
} else {
mEditText.setError(getString(R.string.error_invalid_zip));
}
}
}
});
private boolean isValidZip(String s) {
return s.equals("correct");
}
Works as it should.I think your problem is that you are setting change listener every time text change.
You can try my application to see if it works correctly https://dropmefiles.com/a9ydI
Here the link to the complete source code
https://bitbucket.org/CROSP/testedittexterror/commits/all

How to know when editText ends being editted?

I have two editTexts and when the user edits one and then hits done, it executes a method. I want to be able to know when the user stops editing it. Like in that case, with hitting the "done" button. Unfortunately, the user can 'stop' editing it, if he/she selects the other editText. For some strange reason, the OnEditorActionListener doesn't catch that case. What can I do about it? I've tried with onFocusChange, but that one is very unpredictable...
You can assume when the EditText looses focus
EditText txtEdit= (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
//probably here!
}
});

Edit box does not gain focus in android

i'm facing wired problem in android when i try to focus for a EditText control.
In my application, i am dynamically adding the edit controls, whenever new control is added i wanted to
give auto focus and popup the softkeypad automatically.
sometimes editbox get focus, but if i type text will not appear, sometimes text will appear in the other text control.
i'm observing this behaviour in my phone.
Here is my code snippet which i used to provide focus.
if(mOldEditBox!=null)
{
mOldEditBox.clearFocus();
}
textView.requestFocus();
//textView.setInputType(InputType.TYPE_CLASS_PHONE); //to popup numpad
//textView.setOnFocusChangeListener(focuschange);
mOldEditBox = textView;
i tried setting focuschangelistener event, still it didnt worked :(
OnFocusChangeListener focuschange = new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus){
EditText txt = (EditText)v;
//txt.setInputType(InputType.TYPE_CLASS_PHONE); //to popup numpad
((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE))
.showSoftInput(txt, InputMethodManager.SHOW_FORCED);
}
}
};
Kindly help me where is the problem.. thanks in advance
You are probably not on the UI thread when requesting focus which might cause the strange behavior of the EditText. Try adding it to the message queue using the post(Runnable) method:
textView.post(new Runnable() {
public void run() {
textView.requestFocus();
}
});

Categories

Resources