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.
Related
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);
}
}
Declaration :
DecimalFormat mAmtFormat = new DecimalFormat("##,##,##,##0.00");
edtAmounts = (EditText) findViewById(R.id.txtAmounts);
From xml File Edit text as
<EditText
android:id="#+id/txtAmounts"
android:layout_height="50dip"
android:layout_marginLeft="10dip"
android:background="#FFFFFF"
android:gravity="center_vertical"
android:inputType="numberDecimal"
android:singleLine="true"
android:textSize="18dip"
android:textStyle="bold"
android:width="170dip" />
From Back End mCurtotamt is 565656565(double)
Fetching Data From Sqlite DataBase:
edtAmounts.setText(String.valueOf(mAmtFormat.format(mDoubleformat
.parse(mCurtotamt).doubleValue())))
but the value set into the edit text as 56,56,57,000.00
what is happening over here.
When you get the value from the back end, do not get it as a String like this:
String mCurtotamt = cursor.getString(cursor.getColumnIndex("column_name"));
Instead, get it as a double directly, like this:
double mCurtotamt = cursor.getDouble(cursor.getColumnIndex("column_name"));
Then you don't need to parse it when you set the edit text, but can format it directly:
edtAmounts.setText(String.valueOf(mAmtFormat.format(mCurtoamt)));
The problem is being introduced when converting it to a String when you call cursor.getString().
Use this one for indian format like this "##,##,##0.00" :
static public String formatCurrency(Double doubleVal) {
return new DecimalFormat("##,##,##0.00").format(doubleVal);
}
This function return the value in correct given format. If you pass 5555555.00 value then function return 55,55,555.00 as a string.
I have problem in my android app:
I write a multiply code like 2223 * 3.456 that it will 7682.688 , my problem is that i don't want that text view display this 7682.688 but I want to display 7682.6.
I know about android:maxlength but when I use this it doesn't display my comment like "foot" beside of it , My question is that how can i limit the calculating of this?
My Textview in xml is:
<TextView
android:id="#+id/foot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLength="8"
android:textAppearance="?android:attr/textAppearanceLarge" />
and my multiply code in Activity is:
TextView point = (TextView)findViewById(R.id.foot);
if(ft1.getText().toString().length() == 0 ){return;}
int first = Integer.parseInt(ft1.getText().toString());
double equal = first *3.456;
String x = equal+" foot";
foot.setText(x);
you could use DecimalFormat to only display the number of decimals you want.
example
DecimalFormat formatDecimal = new DecimalFormat("#.00");
foot.setText(formatDecimal.format(YourCalculationResult));
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) ;
}
I am attempting to collect data from a user in the form of an edittext. The user will input a string and click a button to perform the action below:
public String encode(String s){
String result = "";
String element = "";
HashMap<String, String> translate = new HashMap<String, String>();
//initializing translate
translate.put("A",".-");
translate.put("B","-...");
translate.put("C","-.-.");
translate.put("D","-..");
translate.put("E",".");
translate.put("F","..-.");
translate.put("G","--.");
translate.put("H","....");
translate.put("I","..");
translate.put("J",".---");
translate.put("K","-.-");
translate.put("L",".-..");
translate.put("M","--");
translate.put("N","-.");
translate.put("O","---");
translate.put("P",".--.");
translate.put("Q","--.-");
translate.put("R",".-.");
translate.put("S","...");
translate.put("T","-");
translate.put("U","..-");
translate.put("V","...-");
translate.put("W",".--");
translate.put("X","-..-");
translate.put("Y","-.--");
translate.put("Z","--..");
translate.put("1",".----");
translate.put("2","..---");
translate.put("3","...--");
translate.put("4","....-");
translate.put("5",".....");
translate.put("6","-....");
translate.put("7","--...");
translate.put("8","---..");
translate.put("9","----.");
translate.put("0","-----");
s = s.toUpperCase();
for(int i=0; i < s.length();i++)
{
element = (String) translate.get(String.valueOf(s.charAt(i)));
if(element == null)
result += String.valueOf(s.charAt(i));
else
result += element;
}
return result;
}
If the user hits "enter" on the keyboard of the phone it will insert a newline / carriage return. How can I address this so that it does add a new line? I wouldn't mind using the carriage return as a way to issue the command to change focus OUT OF the edittext area, but if not that then just not allow it to be used at all.
You should use android:singleLine="true" in your EditText's XML tag, like this:
<EditText
android:id="#+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
/>
This will make the enter button change the focus to the next view in your UI, rather than inserting a new line in your EditText.
I would put this in the comments but I think my reputation is still not high enough to add comments.
Since android:singleLine="true" is now deprecated, another option is to use android:inputType="text". It's a slight change to #Tiago_Pasqualini's answer:
<EditText
android:id="#+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
/>
This too will make the enter button change focus to the next view in the UI and skip entering a new line into the EditText.