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"
Related
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"/>
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
}
});
I'm making a small app on android studio and I have different EditText fields that I want to change the background to 3 colors depending on the value inside. 1 for if value is equal to goal and 1 for less than goal and last one for more (<,=,>). I already set up colors in the style category but I have no idea how to make the code on the main.java so that it recognizes the EditText fields and change the color according to the value given when compared to the goal.
Text Watcher
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#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
}
});
However you want to use this. You can do it as the user is type in their message, or if they click a button, etc.
To use it, you can do something like this... (in side the method)
if(s.equals("1")){
editText.setBackgroundResource(getResources().getColor(R.color.blue));
}
First, use findViewById to get a reference to the EditText field you want to compare, and assign it to a variable:
EditText enteredValueEditText = findViewById(R.id.entered_value_edittext);
Next, retrieve the value from the EditText field:
String value = enteredValueEditText.getText().toString().trim();
Convert to an integer:
int valueInt = Integer.parseInt(value);
Then, compare it to your goal value and set the background color appropriately using the setBackgroundColor command:
if (goal > valueInt) {
enteredValue.setBackgroundColor(getResources().getColor(R.color.green));
} else if (goal < valueInt) {
enteredValue.setBackgroundColor(getResources().getColor(R.color.red));
} else if (goal == valueInt) {
enteredValue.setBackgroundColor(getResources().getColor(R.color.blue));
}
I have a number picker set up with strings but I am having a problem with getting the string that is being displayed.
Here is the code for the construction of the number picker.
fractionPicker is my Number picker .
void fractionPickerFunction(){
final String[] arrayString= new String[]{"None", "1/8", "1/4", "3/8", "1/2", "5/8", "3/4", "7/8"};
fractionPicker.setMinValue(0);
fractionPicker.setMaxValue(arrayString.length-1);
fractionPicker.setFormatter(new NumberPicker.Formatter() {
#Override
public String format(int value) {
return arrayString[value];
}
});
}
Set a change listener if you need to just get the result. If you need to modify the variable with the setFormatter still then just use both. call your setFormatter, then also add the change listener. It will then return to you, your modified variable.
Change
void fractionPickerFunction(){
final String[] arrayString= new String[]{"None", "1/8", "1/4", "3/8", "1/2", "5/8", "3/4", "7/8"};
fractionPicker.setMinValue(0);
fractionPicker.setMaxValue(arrayString.length-1);
fractionPicker.setFormatter(new NumberPicker.Formatter() {
#Override
public String format(int value) {
return arrayString[value];
}
});
}
To this:
fractionPicker.setOnValueChangedListener(new OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// TODO Auto-generated method stub
String Old = "Old Value : ";
String New = "New Value : ";
}
});
You should refer to the android documentation on the NumberPicker. In short,
you are going to want to set a OnValueChangeListener by calling setOnValueChangedListener on your NumberPicker. You can do that with the following:
fractionPicker.setOnValueChangedListener(new OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// Do your work here
// Parameters
// picker NumberPicker: The NumberPicker associated with this listener.
// oldVal int: The previous value.
// newVal int: The new value.
}
});
My app has an input where the user can enter a port number,I tried out of curiosity inputting more than 9 digits of number (Ex: 1234567890),and the app will crash.How do I prevent it from crashing?
EDIT
I am using Java language for Android
CODING
public class addActivity extends Activity{
EditText savedName,savedIP,savedPort,savedUserID,savedUserPass,savedChannel;
Button btnSave;
Button btnBack;
String addressList;
FileOutputStream outputStream;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
savedName = (EditText)findViewById(R.id.editName);
savedIP = (EditText)findViewById(R.id.editIP);
savedPort = (EditText)findViewById(R.id.editPort);
savedUserID = (EditText)findViewById(R.id.userID);
savedUserPass = (EditText)findViewById(R.id.userPass);
savedChannel = (EditText)findViewById(R.id.editChannel);
btnSave = (Button)findViewById(R.id.btnSave);
btnBack = (Button)findViewById(R.id.btnBack);
try{
String channelNumString = savedChannel.getText().toString();
int channelNum = Integer.parseInt(String.valueOf(channelNumString));
addressList = savedName.getText().toString();
outputStream = openFileOutput(addressList, Context.MODE_PRIVATE);
//outputStream.write(savedName.getText().toString().getBytes());
outputStream.write("4-".getBytes());
outputStream.write(savedIP.getText().toString().getBytes());
outputStream.write(":".getBytes());
outputStream.write(savedPort.getText().toString().getBytes());
outputStream.write("/user=".getBytes());
outputStream.write(savedUserID.getText().toString().getBytes());
outputStream.write("&password=".getBytes());
outputStream.write(savedUserPass.getText().toString().getBytes());
outputStream.write(System.getProperty("line.separator").getBytes());
outputStream.close();
Toast.makeText(getBaseContext(), "Address Saved !", Toast.LENGTH_SHORT).show();
savedName.setText("");
savedIP.setText("");
savedPort.setText("");
savedUserID.setText("");
savedUserPass.setText("");
savedChannel.setText("");
else{
Toast.makeText(getBaseContext(),"Apps does not support " + channelNumString + " channels",Toast.LENGTH_LONG).show();
}
}catch (Exception e){
e.printStackTrace();
}
LAYOUT
<EditText
android:layout_width="350dp"
android:layout_height="wrap_content"
android:id="#+id/editChannel"
android:background="#android:drawable/edit_text"
android:maxLines="1"
android:layout_gravity="center_horizontal"
android:inputType="number"
android:digits="0123456789."/>
This is the part I'm having problem at
#Fay Zan
Basically you don't want user to input more than 9 digits for your field,
This can be achieved using two ways programmatically or using layout views properties,
In you xml simple give this attribute to your EditText
android:maxLength="9"
OR programmatically by checking the length of your field. for example suppose the id of your field is #id+/portNo
then do validation like this
private EditText mportNo;
inside your onCreate()
mportNo = (EditText) findViewById(R.id.portNo);
String portValue = mportNo.getText().toString();
if(portValue.length() > 9) {
// throw error
}
I Assumed you are using EditText. so you need to use addTextChangedListener().
Try the following way,
YOUR_EDIT_TEXT.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start,int count, int after){}
public void onTextChanged(CharSequence s, int start,int before, int count)
{
if(s.length() > 9)
{
//Do your stuff .
// You may show the intimation to user like don't try with more than 9 digits
}
else
{
//Do your stuff
}
}
});
This may helps you.