SelectAll on EditText with Two-Way Data Binding - android

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

Related

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

How to call TextWatcher from Class

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.

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

Adding OnClickListener on EditText makes next and done button not working anymore

I have several EditText displaying in a RelativeLayout to collect data from the user.
They are declared like this:
<EditText
android:id="#+id/accountNameEdit"
android:layout_gravity="fill_horizontal"
android:inputType="textPersonName" >
Every EditText has its validation logic, and when the input is wrong an error message is displayed. When a user clicks again on the text field with errors, the error message should disappear, so I added an OnClickListener on the EditText like this:
accountName = (EditText) findViewById(R.id.accountNameEdit);
accountName.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
accountName.setError(null);
}
});
This is working as expected, however the next and done buttons on the soft key don't work anymore, so the user has to click back to lose focus over the EditText.
First of all, is setting an OnClickListener the best approach or should I hook to another event? How do I get back the original behaviour of the soft key buttons?
I'm compiling against 4.1.2.
Try to add TextWatcher and you can remove the error popup when the user enters something
accountName.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
accountName.setError(null);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});

Android: How to keep Default number always even if user wants to delete it, and how to delete default number when user inputs a value?

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" />

Categories

Resources