My app has seven editTexts and one button below them. I want the button to change its background when the user filled all editText with numeric values (int or double, except zero).
I think I should use OnTouchChangeListener but I am not sure how to do it, any advice?
Add addTextChangedListener to your EditText.
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Add your logic here
}
#Override
public void afterTextChanged(Editable s) {
}
};
editText.addTextChangedListener(textWatcher);
// Remember to remove when whatever actions required have completed.
editText.removeTextChangedListener(textWatcher);
Assuming all your EditText views require the same logic, create a single TextWatcher object and bind the listener to each as above. You can also implement this in your class:
class MyClass implements TextWatcher
And add the override methods into the class and write the code in the onTextChanged() method.
Put a line inside your
android:digits="123456789"
And in java file check for the length of edittext.if the length of all editext is greater than 1 then change the color of the button.
If worked ...check my answer
Related
Is there a way to display text on screen automatically as you type an input in a text field in Android Studio?
For example, if I type in a number, it gets automatically multiplied by 20 and the result is shown on the screen below the text field.
One simple way is to use on Textview Event listeners to handle your actions!
here is how u can do it.
field1 = (EditText)findViewById(R.id.field2);
field1.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{ //Called after the changes have been applied to the text.
//You can change the text in the TextView from this method.
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
//Called before the changes have been applied to the text.
}
public void onTextChanged(CharSequence s, int start,int before, int count) {
TextView.setText("Here is the changing text"+s);
//Similar to the beforeTextChanged method but called after the text changes.
}
});
/*
You can prevent an infinite loop like this
#Override
public void afterTextChanged(Editable s) {
if (_ignore)
return;
_ignore = true; // prevent infinite loop
// Change your text here.
// myTextView.setText(myNewText);
_ignore = false; // release, so the TextWatcher start to listen again.
}
*/
I could not understand the question properly so here is the solution for the 2 different cases.
The text to display is in the same Activity or Fragament.
Use a TextWatcher class like this
et1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//This method is called to notify you that, within s, the count characters beginning at start have just replaced old text that had length before.
newTextView.text=s.toString;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// This method is called to notify you that, within s, the count characters beginning at start are about to be replaced by new text with length after.
}
#Override
public void afterTextChanged(Editable s) {
// This method is called to notify you that, somewhere within s, the text has been changed.
}
});
The output is in a different screen (fragment/drawer)
You can do it using a combination of TextWatcher and LiveData.
Put a TextWatcher on your editText and store the data in a Livedata. (https://developer.android.com/topic/libraries/architecture/livedata)
Then observe this liveData and change the Textview accordingly.
I have an Activity with a fragment... In this fragment I'm using two-way DataBinging in an EditText. This EditText is binded to a Double property of the object, and because of this, I had to implement an InverseMethod to convert String -> Double and Double -> String...
In my EditText I configured android:selectAllOnFocus="true", and I'm forcing it also on onCreateView method of the fragment: edQtd.selectAll()
The problem is, that when the fragment appears, the EditText has the focus, but the text is not selected, instead, the cursor is before the first number...
I wanted it to show with all the text selected...
Tried to instead of using the inverse method, just concatenate an empty String, but the result was the same...
From what I saw debbuging it, the binding class generated, sets the text after the fragments creation (after I manually called edQtd.selectAll()), removing the selection...
Any ideas how to solve it?
Edit:
For now I solved it adding a TextChangedListener to the EditText, where I select all the text only the first time the text is changed:
edQuantidade.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
if(selectAllEdQtdText) {
edQuantidade.selectAll();
setSelectAllEdQtdText(false);
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
Add following attributes on EditText in Layout.
<EditText
android:focusable="true"
android:focusableInTouchMode="true"
android:selectAllOnFocus="true"
/>
And remove edQtd.selectAll() from code.
Edit
Because none solution works. This will work because this will trigger selectAll after a delay. Add this after setting model in binding.
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
#Override
public void run() {
edQtd.selectAll();
}
}, 500);
I have a hint text and Normal text on a EditText field, I have specified different Font-Size, Font-Color, Font-Name for both. I have put that in my ActivityClass , and its working fine. I have around 10 Edit Text fields, So it will be very odd if i add the Textwatcher for all, and doing the same stuff again and again. So i decided to create a class and put TextWatcher there.
But after adding that, it stops working. Its not changing the text color, Kindly guide me how to achieve this through class
public class FormTextCosmetics {
public void changeHintText(final EditText editText){
hintFontColor= context.getResources().getColor(R.color.white);
normalFontColor= context.getResources().getColor(R.color.grayCheckoutFont);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if(s.length() > 0){
editText.setTextSize(normalTextSize);
editText.setTypeface(Typeface.create(normalFontFamily, Typeface.NORMAL));
editText.setTextColor(normalFontColor);
editText.setAlpha(1);
}else{
editText.setTextSize(hintFontSize);
editText.setTypeface(Typeface.create(hintFontFamily, Typeface.NORMAL));
editText.setTextColor(hintFontColor);
editText.setAlpha(0.52f);
}
}
});
}
Main Activity Class
FormTextCosmetics formTextCosmetics= new FormTextCosmetics(this);
formTextCosmetics.changeHintText(etName);
I have also tried to put above code in another Textwatcher, but it also not effecting the text.
Your editText is not 'aware' that it should call your class whenever something changes in it.
You should do it this way - your class should implement TextWatcher:
public class FormTextCosmetics implements TextWatcher {
//Your code goes here
}
And then you use it like this -
etName.addTextChangedListener(new FormTextCosmetics());
Now the editText will call your class whenever something happens in it's text.
I have a simple EditText, which inputs only numbers.
I want to show 0(zero) even if user presses backspace.
Now zero is getting deleted.
Moreover, I need to remove this default zero, when user starts entering values.
How do i achieve it?
You should use android:hint="0" or in more complex way..
You should use TextWatcher and override the below three methods. THer you can get the text in the arguments as CharSequence in the beforeTextChanged and onTextChanged methods and in afterTextChanged you have Editable from where you can get the data in the EditText. Write in the desired function as per your logic and it should work like a charm.
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(final CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
It sounds like you want:
<EditText
android:hint="0" />
I have an application where user inputs text into EditText field. After user clicks OK (in keyboard input mode), a correct value is in the EditText (lets say "Smile").
if (answers.get(counter).getText().equals(opponentAnswers.get(counter)))
But this if statement fails, because the same EditText has the values that were suggested by T9 option, when user was inputing his answer (for example values of EditText would be "Smile Smiling Smiled"), while it should only have a value "Smile".
Any ideas how to solve this issue?
That's really weird. These are kind of guesses, but this is what I'd try next if I were you:
A. Instead of doing an equals against getText(), try doing a toString on getText(), so:
if (answers.get(counter).getText().toString().equals(opponentAnswers.get(counter)))
B. If that doesn't work then you could try adding a TextWatcher using addTextChangedListener on the EditText, and getting the value from that. Calling toString() on the editable returned in afterTextChanged might give you the value you want.
private class SearchTextWatcher implements TextWatcher {
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
#Override
public void afterTextChanged(Editable s) {
//Get the text the user sees
String textShownToUser = s.toString();
}
}
Hope this helps! Best of luck!