How to call TextWatcher from Class - android

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.

Related

Is there a way to display text as you type in Android Studio?

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.

SelectAll on EditText with Two-Way Data Binding

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

how to change button background when all edittext is filled

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

Space button click puts Auto suggestion to the EditText Android

Please, help me to solve this strange problem. I try to implement EditText, where user can add links to user profiles, hashtags and so on. I have OnTextChanged method where I get current text from EditText, process it and put it to EditText backward.
#OnTextChanged(R.id.share_article_say_something)
public void onTextChanged() {
if (mIsTextChangedManually) {
mIsTextChangedManually = false;
return;
}
mIsTextChangedManually = true;
SpannableStringBuilder builder = mCommentsSpannable.format(mSaySomethingTv.getText().toString());
mSaySomethingTv.setTextKeepState(builder);
}
EditText looks like that
<com.slangwho.views.SWEditText
android:id="#+id/share_article_say_something"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:textColor="#color/black"
android:textColorHint="#color/my_friend_pressed_color"
android:background="#android:color/transparent"
android:textSize="#dimen/splash_screen_button_text_sp"
android:hint="#string/say_something"
android:layout_margin="10dp"
android:imeOptions="actionDone"
android:textCursorDrawable="#drawable/ed_cursor_drawable_black"
app:fontName="ProximaNova-Regular"/>
This EditText extends regular EditText just to add 1 additional parameter, which is fontName.
When I type some text I receive wrong suggestions(cursor is after single standing "s")
When I typing "Space", I receive:(The First suggestion adds after single standing "s")
It turned out that it was a problem with Samsung Swipe Keyboard. When I set text to the edit text using EditText.setText() or EditText.setTextKeepState() the keyboard suggestions behave strange. The solution was not to set text to the EditText, but set TextWatcher to the EditText and transform Editable in the method afterTextChanged(Editable s).
public class SocialTextWatcher implements TextWatcher {
CommentsSpannable mCommentSpannable;
public SocialTextWatcher(CommentsSpannable commentsSpannable) {
mCommentSpannable = commentsSpannable;
}
#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) {
mCommentSpannable.formatEditable(s);
}
}

Android - EditText saves T9 suggested values

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!

Categories

Resources