I need use numberSigned EditText.
EditText works but i have a problem with entering numbers with a minus.
Take a number in this way:
charsEntered = Integer.parseInt(et.getText().toString());
Numbers without minus works ok but when i need enter for example -3 it don't works.
Use in your XML layout the following :
<EditText
....
android:inputType="numberSigned" />
Try following to get signed number
String tmpstr = et.getText().toString();
charsEntered = Integer.parseInt(tmpstr);
if(tmpstr.charAt(0).equals("-")) {
charsEntered *= (-1) ;
}
Related
In my application I want use TextView and I want when this TextView if get l line show 3dos (...)
I write this lines in TextView :
android:ellipsize="end"
android:maxLines="1"
But always set ... end of textView.
But I want if line after 1 lines then show ...
how can I it?
Try this code :
if (holder.newsTitle.getLineCount() > 1) {
holder.newsTitle.setEllipsize(TextUtils.TruncateAt.END);
}
I hope help you dear
If you want to get the number of lines of a TextView, you can do this...
textView.setText("Here is my text");
int numOfLines = textView.getLineCount();
if (numOfLines > 1) {
//code here
}
The BEST way to do this is by character count.
theString = "This is my string";
if (theString.length() > 5) {
textView.setText(theString.substring(0, 5) + "...");
}
The above code will say, if the character count is greater than 5, cut off the characters after 5 and add ...
The above code prints out:
This ...
am trying to put only a specific value in my edit text.
I have used this this my layout.
android:digits="0123468"
however, i also do not want that number 1 and 3 should work in my edit text.
Sample; enter 32... it gives me a list of items
but enter 3 should not allow me to do something.
Can someone help me on this?
This could be an aproach:
<EditText
android:id="#+id/edtNumber"
android:digits="0123456789"
android:inputType="number" />
And if you want to discard '1' and '3' you could get input number like this:
Integer.parseInt(edtNumber.getText().toString())
and compare it with values you don't want.
Also if for some reason you want to use decimals do this:
<EditText
android:id="#+id/edtNumberDecimal"
android:digits="0123456789."
android:inputType="numberDecimal" />
Use regular expressions.
#Override
public void afterTextChanged(Editable s) {
String text = s.toString();
int length = text.length();
Pattern pattern
= Pattern.compile("(?s)\\d|[024-9]{2,}");
if(length > 0 && !Pattern.matches(pattern, text)) {
s.delete(length - 1, length);
}
}
I am new to android development , i am trying to develop an app where user can keep a few text field empty,
However when user doesn't provide any input in the text field app crashes.
How do we handle empty text field in android
Following is my code for text Field.
<EditText
android:layout_width="40dp"
android:layout_height="40dp"
android:inputType="number"
android:ems="10"
android:id="#+id/editText1"
android:layout_weight="1"
android:background="#ffb7ffbf"/>`
java code:
TextView t1 = (TextView)findViewById(R.id.editText1);
a1 = Integer.parseInt(t1.getText().toString());
you should cast EditText instead of TextView.
EditText t1 = (EditText)findViewById(R.id.editText1);
Ensure if the TextBox is not empty before parsing the value to the int as
if (e.length()>0) {
int a1= Integer.parseInt(e.getText().toString());
}
Else you can get a java.lang.NumberFormatException: for Invalid int: "";
Try this:
TextView t1=(TextView)findViewById(R.id.editText1);
String aux = t1.getText.toString();
if(aux.length() > 0)
a1= Integer.parseInt(aux);
else
// the text is empty
getText.toString will bring you something always so it can be and string size 0, wich is empty. that will make the parseInt() throw an error because it won find a number in the string.
So you have to ask if the length of the string > 0, before the parse.
i have a edit text box in which i can enter alphanumeric charracter but when i click the summit button then a checking would be perform for either edit text contains values 0 to 9 and also it can contain "." and in other case it will show a message that "pls enter numeric values" so how to achive it?
means validate 0123456789 and "." in edit textbox
public void onClick(View arg0) {
String Ammount =
((EditText) findViewById(R.id.price))
.getText().toString();
double db = Math.ceil(Integer.parseInt(Ammount)*100)/100;
ammount = Double.toString(db);
}};
Why to go hard just don't allow to enter string in your xml add:
<EditText
....
android:inputType="numberDecimal"
...
/>
add this tag in its xml android:numeric = "decimal"
One of the option would be use regex. Show the error message when validate return false.
boolean validate(String s )
{
return s.matches("[0-9\\.]*");
}
Scanner scanner = new Scanner(lapsPerMile_st);
if (!scanner.hasNextDouble()) {
Context context = getApplicationContext();
String msg = "Please Enter Digits and Decmials Only";
int duration = Toast.LENGTH_LONG;
Toast.makeText(context, msg, duration).show();
lapsPerMileEditText.setText("");
return;
} else {
//Edit box has only digits, Set data and display stats
data.setLapsPerMile(Integer.parseInt(lapsPerMile_st));
lapsRunLabel.setVisibility(0);
lapsRunTextView.setText(Integer.toString(data.getLapsRun()));
milesRunLabel.setVisibility(0);
milesRunTextView.setText(Double.toString(data.getLapsRun()/data.getLapsPerMile()));
}
<EditText
android:id="#+id/mileCount"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginTop="110dp"
android:inputType="numberDecimal"
android:maxLength="4"
/>
For some reason if I enter a non decimal number such as 3, or 5, it works fine but when I enter a floating point such as 3.4 or 5.8 it force closes. I cant seem to figure out whats going on. Any ideas?
Use the right type: Integer.parseInt, Float.ParseFloat, ... and take in account that you are using Java so if one ouf the parse's fails you'll get an exception: NumberFormatException.
String int_string = "1";
int data = 0; // 0 as default value
try
{
data = Integer.parseInt (int_string);
}
catch (NumberFormatException e)
{
// You are trying to parse and int from a string that is not an int!
}
The culprit is almost certainly parseInt. Go ahead and connect to your device using the adb (adb logcat -v time) to view the log, as well as the stack trace generated when your app crashes.
ParseInt doesn't like any non-integer characters (I.E. It's bombing when it hits the decimal point).
I recommend using try-catch to surround your parseInt or Parse"Anything" methods.
Next, you may want to restrict the allowable characters to integer-type only within your layout XML:
https://developer.android.com/reference/android/widget/TextView.html#attr_android:numeric