There is an EditText, what I want is when I select it ,I should get the cursor position. The maximum length of the edittext is 13.
Also in my edittext, the first three character's type is text and the other character's type is number. If the first three characters are selected, the input type of keyboard should show text. And if any other characters are selected ,then the input type should be number. How to do this?
I think this code can help you ❤ :
Class Code :
edittext = (EditText) findViewById(R.id.edtxt);
edittext.setInputType(InputType.TYPE_CLASS_TEXT);
edittext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(final CharSequence s, int start, int before, int count) {
if (edittext.getText().length() < 3 ) {
edittext.setInputType(InputType.TYPE_CLASS_TEXT);
}
else if (edittext.getText().length() == 3 ) {
edittext.setInputType(InputType.TYPE_CLASS_NUMBER);
}}
#Override
public void afterTextChanged(Editable s) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
});}}
Xml Code :
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:maxLength="13"
android:id="#+id/edtxt"
android:ems="10"/>
Related
EditText
<android.support.v7.widget.AppCompatEditText
android:id="#+id/itemEditText_id"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="8dp"
android:layout_marginStart="2dp"
android:layout_marginEnd="4dp"
android:lines="2"
android:scrollHorizontally="false"
android:maxLength="69"
android:scrollbars="vertical"
android:background="#drawable/round_border_edit_text"
android:hint="Go ahead \nSend messge"
android:inputType="textMultiLine|textLongMessage"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:maxLines="2"
android:minLines="1"
/>
this is my xml code if something needs to be added or removed here please tell.
java code which i'm using to achieve what i want like whatsapp's editText but its not working quite well
final AppCompatEditText editText = holder.itemEditText;
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// if edittext has 10chars & this is not called yet, add new line
if(editText.getText().length() == 34 && !isReached) {
editText.append("\n");
isReached = true;
}
// if edittext has less than 10chars & boolean has changed, reset
if(editText.getText().length() < 34 && isReached) isReached = false;
Log.d("char", "onTextChanged: "+charSequence);
Log.d("i", "onTextChanged: "+i);
Log.d("i1", "onTextChanged: "+i1);
Log.d("i2", "onTextChanged: "+i2);
}
#Override
public void afterTextChanged(Editable editable) {
}
});
}
this code is not working like i want it to or you can say i'm not being able to code the way i want editText to act.
i want my editText to work like Whatsapp's editText like when i type something and it reaches to the icon inside the editText, cursor goes to a new line with the typing word. In my editText the words are going underneath the ImageButton which im using as a send icon. i dont want the words to go underneath the send icon. Please Help.
ImageButton
<android.support.v7.widget.AppCompatImageButton
android:id="#+id/itemSendButton_id"
android:layout_width="25dp"
android:layout_height="20dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="11dp"
android:background="#color/colorFacebookBtnText"
android:clickable="true"
android:focusable="true"
android:scaleType="fitCenter"
android:src="#drawable/ic_send" />
i'm new to programming so please help me out and thanks in advance.
Use this
et1.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start,int before, int count)
{
// TODO Auto-generated method stub
if(et1.getText().toString().length()==size) //size as per your requirement
{
et2.requestFocus();
}
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
This is my edittext
<EditText
android:id="#+id/etWaardeVan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:digits="0123456789."
android:inputType="number|numberDecimal"
android:scrollHorizontally="true"
android:singleLine="true"
/>
How do I programmatically allow for only one dot to be input.
Is this what you are looking for?
Set EditText Digits Programmatically
etWarDevan.setKeyListener(DigitsKeyListener.getInstance("0123456789."));
I'd remove the last entered character if its a dot and there has been one before.
EditText field = view.findViewById(R.id.etWaardeVan);
field.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable editable) {
String str = editable.toString();
String strold = str.substring(0, str.length() - 2);
String lastchar = str.substring(str.length() - 1);
if(strold.contains(".") && lastchar.equals(".")) field.setText(str);
}
});
Hint: this only works if the user doesn't jump back and enters the dot in the middle of the string. You may want to disable cursor movement. (Like this or using android:cursorVisible)
Alternatively (if the input always contains a dot) you can just create two EditTexts without a dot allowed.
Here is my solution, and it works:
dot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//only show one dot if user put more than one dot
if(input.contains(".")){
//then save what it has
input = textView.getText().toString();
}else {
//concat the input value
input=input+".";
}
}
});
Basically, EditText is derived from TextView which has a
void addTextChangedListener(TextWatcher watcher)
method. TextWatcher has callbacks, like
abstract void afterTextChanged(Editable s)
that you can implement in order to filter the new text to only contain period.
Regex: (?<=^| )\d+(\.\d+)?(?=$| )
Example
et1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
// Do something here with regex pattern
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
In AfterTextChangeListener add Regex
val isSingleDot = Regex("\.").findAll("sample.sample").count() <= 1
Set inputType attribute in EditText/TextInputEditText as android:inputType="numberDecimal"
In my project I have 7 EditText field.
User will only need to fill their own value in first two EditText.
I need a series calculation like this...
(after user input) subtract ed1-ed2 the result automatically show in ed3 field.
multiply ed3*2 = result show in ed4 field automatically.
multiply ed3*3= result will show in ed5 field.
multiply ed3*4=result will show in ed6 field.
Finally sum of ed4+ed5+ed6= result will show in ed7 field automatically.
So how to do it any one please help.
Thank you in Advance.
All EditText use a same TextWatcher. A simple solution like this.
EditText et1;
EditText et2;
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) {
int num1 = Integer.valueOf(et1.getText().toString()); // Make sure you get a number
int num2 = Integer.valueOf(et2.getText().toString());
// do your calculation
}
#Override
public void afterTextChanged(Editable s) {
}
};
et1.addTextChangedListener(textWatcher);
et2.addTextChangedListener(textWatcher);
I've recycler view with 0 items, I've added an option to manually add the items, my stracture is simple :
RV_Item.xml (contains EditText).
MyItem, which is an Object for RV ( contains private String Text; ).
MainActivity.java, where the stuff happen.
// My List<Object>
List<MyItem> Items = new ArrayList<>();
// For Adding, I've added FAB-Button, When Clicked, it does the following :
Items.add(new MyItem());
CheckForEmptyItems();
mAdapter.notifyItemInserted(0);
Now, When the user click the save button, i want to take all the edittext in all the items he had added, i'm doing in the following way :
for(MyItem items : Items){
Log.i("PrintingInfo", items.getText() );
}
The problem is, i'm not getting the text he entered in all EditText fields, and it's returning Null in all of them, What's the issue in this ?
So, i don't know why always i know the answer after posting, but here's how you gonna know what the user typed :
in your Adapter Class, in onBindViewHolder method, add textlistener for the EditText, here's an example :
holder.MyEditText.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) {
itemList.get(position).setText(s.toString());
}
});
Hope that helps you!
I'm trying to get a character count of an EditText. I've looked into different properties of the EditText and TextView classes, but there doesn't seem to be a function that returns the amount of characters. I have tried to use the TextWatcher, but that is not ideal since sometimes I load a saved message into the EditText from the preferences, and TextWatcher does not count characters not typed right then.
Any help would be great!
Cheers!
Just grab the text in the EditText as a string and check its length:
int length = editText.getText().length();
EditText edittext;
private final TextWatcher mTextEditorWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//This sets a textview to the current length
textview.setText(String.valueOf(s.length());
}
public void afterTextChanged(Editable s) {
}
};
editText.addTextChangedListener(mTextEditorWatcher);
In Kotlin it's very easy
<EditText_name>.Text.length