keyboard bug with onClickListener in EditText - android

I have a problem with an editText. I want to put a default text in this editText and when you click on the text disappear and you can wirte a number. Every thing is going well with that code for the layout:
<EditText
android:id="#+id/yearEdit"
android:layout_width="145dp"
android:layout_height="45dp"
android:layout_marginBottom="40dp"
android:gravity="center"
android:ems="10"
android:inputType="number|numberDecimal"
android:text="#string/chooseDate"
android:textSize="13sp"
/>
and in the activity the edit text have as textWatcher and onClickListener :
private class ChooseYear implements TextWatcher, OnClickListener {
#Override
public void afterTextChanged(Editable arg0) {
if (!yearEdit.getText().toString().equals("")) {
yearChoose = Integer.parseInt(yearEdit.getText()
.toString());
}
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
#Override
public void onTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
#Override
public void onClick(View arg0) {
if (yearChoose == -1) {
yearEdit.setText("");
}
}
}
But my problem is : When i had it as the onClickListener, the android keyboard doesn't close anymore when i click on its "Done" button. Does someone know why ? Does it exist a proper way to fix it ?
Thanks a lot for your help,
Eliott

For people who are interest, this is my conclusion on my own problem.
It looks like the keyBoard doesn't work proprely (The "Done" button is not closing the keyboard) when you add an OnClickListener on the EditText. To solve the problem I have to add the line :
yearEdit.setOnClickListener(null);
in the code. When the OnClickListener is set to null, The "Done" button restart to work normally. You have to re-set the OnClickListener if you need it again after (not my case).
My opinion is that could be an Android bug, because there is no logic than an OnClickListener have an effect on the keyboard. If you want to share your opinion about it, i will be interest.

Related

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

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

Make editText can type but text not change in Android?

I have a editText:
<EditText
android:textCursorDrawable="#drawable/edittext_cursor"
android:textColorHint="#color/colorBlackHintText"
android:fontFamily="#string/font"
android:inputType="text"
android:id="#+id/idEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:hint="Type something"
android:paddingBottom="#dimen/fab_margin"
android:paddingRight="#dimen/fab_margin"
android:paddingTop="#dimen/fab_margin"
android:textSize="#dimen/text_size" />
When anyone typing, it will call the function:
EditText idEditText = (EditText) findViewById(R.id.idEditText);
idEditText.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) {
ChooseText(); // here
}
#Override
public void afterTextChanged(Editable s) {
}
});
ChooseText() function will show list then people can choose one of them.
It mean, when anyone try to change editText, the text of editText cannot be changed, the list and will show and this text will change in later, people cannot change this text by themselves.
The most important thing is people see text in editText not change.
Any helps. Thank!
Try to call ChooseText() function on afterTextChanged() method
#Override
public void afterTextChanged(Editable s) {
ChooseText();
}
<EditText
android:inputType="text"
android:id="#+id/idEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type something"/>
try this
#Override
public void afterTextChanged(Editable s) {
s.clear();
}
Clicklistener
idEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ChooseText();
}
});
From what you are trying to do it seems more like a Spinner functionality. Where you want user to select from a fixed list of possible texts (values).
http://developer.android.com/guide/topics/ui/controls/spinner.html
Updated:
Checkout AutoCompleteTextView, maybe that matches what you are trying to do.
For the functionality you want, i suppose it would be better to have EditText + Spinner. And a Button to select between the two.
Make Layout such that EditText and Spinner overlay each other. By default have Spinner setVisibility to VISIBLE and EditText to GONE.
So normally user selects from list of items in spinner. When user is in a situation where none of the spinner list items are OK and needs to edit Text, he/she can press a Button you provide.
In OnClick of the button you make Spinner setVisibility GONE and EditText setVisibility VISIBLE. So now user has a EditText to enter Custom Text.
How about AutoCompleteTextView? This is an editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.
Find more examples here

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

Switch to the next field when autocompleted

I have got a lot of AutoCompleteTextViews and EditText in my app, and i wanted to make user input a bit more easy :
I would like to go to the next field when the user hit the dropdown list.
Thank you if you have any infos about how to do that !
Here is what i finally did and which worked :
AutoCompleteField.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView nextField = (TextView)AutoCOmpleteField.focusSearch(View.FOCUS_FORWARD);
nextField.requestFocus();
}
});
hey add this to your edittext xml
android:imeOptions="actionNext"
and add
android:imeOptions="actionDone"
in your last edit text to finish

Categories

Resources