I have an EditText in my app with textChangeListener that convert number format on afterTextChanged.
this is my EditText in layout file:
<EditText
android:id="#+id/zone_name"
android:layout_width="250dp"
android:layout_height="match_parent"
android:background="#drawable/margin_shape_table"
android:paddingStart="8sp"
android:paddingEnd="8sp"
android:singleLine="true"
android:inputType="text"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
and this is my listener code:
zoneName.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
String char1 = s.toString();
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() > 0){
zoneName.removeTextChangedListener(this);
zoneName.setText(converter.convertNumbers(s.toString()));
zoneName.addTextChangedListener(this);
zoneName.setSelection(s.length());
}else {
zoneName.setText("");
}
}
});
The problem is that when I run it on Asus ZenPad8.0 (android 6.0.1) it only get first entered character and after that "Editable s" contains only first character.
I have this problem on this device only and for other devices it gets full entered text.
Anyone have any suggestion how to solve this problem?
the bug is with the Asus ZenUI's keyboard and you may add android:inputType="textNoSuggestions" to avoid the issue.
for more complete answer please check my answer to another question
https://stackoverflow.com/a/55933884/3880796
Related
Update: Solved.
I am using EditText as Label display.
I added these lines in XML, but no change.
android:cursorVisible="false"
android:inputType="textMultiLine|textNoSuggestions"
android:textIsSelectable="false"
In code side, added this line:
editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
I tried all of those settings, none of theme helped, also used all of them together still does not work.
What is the appropriate way?
Use this tag in your xml will fix suggestion problem:
android:inputType="textFilter|textMultiLine"
but for backspace or delete problem you can use addTextChangedListener like this:
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) {
// here you can check the changes and revert change or anything you like
}
#Override
public void afterTextChanged(Editable s) {
}
});
for more detail see the official docs of TextWatcher
I want to use an EditText for the input from Barcode Scanners.
The Barcode Scanner acts like a physical Keyboard.
I scan a Barcode
It types the Barcode like an external Keyboard with a enter at the end
I have an EditText which should always be focused but never show the
software keyboard. I don't want to deactivate the Keyboard completely because it's needed in the same Class on a different EditText.
<EditText
android:id="#+id/barcodeText"
android:layout_width="match_parent"
android:layout_height="40dp"
android:ems="10"
android:singleLine="true"
android:cursorVisible="false"
android:gravity="center"
android:background="#00000000"
android:textColor="#color/secondary_text"
>
then use TextChange Listener for your Edittext. Like this
editText.setInputType(InputType.TYPE_NULL);
editText.requestFocus();
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() != 0)
// Your action
}
});
Please try with this property
android:inputType="none"
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);
}
}
In my Android project I have restricted Edittext to allow only Alphanumeric characters. I have using below code snippet to achieve this
<EditText
android:id="#+id/edt_text"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
android:ems="10"
android:imeOptions="actionNext"
android:maxLength="8"
android:padding="5dp"
android:singleLine="true" />
But while using this code if I tab the Space Bar showing in soft keypad it is acting as a BackSpacebutton and removing the characters in EditText. Anyone please help me to solve this issue.
You can also handle this Programetically.
mEditText1.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) {
if (s.equals(" ")) {
mEditText1.getText().toString().trim();
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
I hope it may help you. :)
I know it's very too late, but the problem is in the input digit restriction you have set.
How about allowing the spacebar to create a space, and just remove it programmatically?
use this RegEx it will return true if it is alphanumeric else it will return false.
public boolean isAlphaNumeric(String s){
String pattern= "^[a-zA-Z0-9]*$";
if(s.matches(pattern)){
return true;
}
return false;
}
I am using EditText and providing some Hint there. I am putting it like this:
android:hint="#string/user_name_hint"
android:textColorHint="#ffffff"
android:gravity="left"
But here hint is coming in the left side. But i want the hint is in the middle of the box and when i am clicking the text should start from the left.
How it is possible??
Use this EditText
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="user_name_hint"
android:textColorHint="#fffff"
android:gravity="center_horizontal"
android:id="#+id/editText1"/>
Then you need to add addTextChangedListener to solve your problem.
final EditText hintText = (EditText) findViewById(R.id.editText1);
hintText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() == 0) {
hintText.setGravity(Gravity.CENTER);
} else {
hintText.setGravity(Gravity.LEFT);
}
}
});
set Gravity to center.. android:gravity="center_horizontal"
Not sure you can do it in a "straight forward way". Probably some workaround - either suggested by nagesh or by placing a button on top of the edit text and switching to edit text when it gets clicked or focused