I have two editText. First edit text is amount, second edit text is description. I get the amunt edit text values float, description edit text values string. But error when control values. I think wrong "(tutarEdit.getText().toString().equals("")".
Thanks in Advance..
final EditText tutarEdit = (EditText) layout.findViewById(R.id.editTextTutar);
final EditText aciklamaEdit = (EditText) layout.findViewById(R.id.editTextAciklama);
Float tutar = Float.parseFloat(tutarEdit.getText().toString());
String aciklama = aciklamaEdit.getText().toString();
if(tutarEdit.getText().toString().equals("") || aciklamaEdit.getText().toString().equals("")){
Toast.makeText(MainActivity.this, "Void", Toast.LENGTH_LONG).show();
}
You are getting parse error when tutarEdit is "", surround it with try/catch
Float tutar = 0;
try {
tutar = Float.parseFloat(tutarEdit.getText().toString());
} catch (Exception e) {
e.printStackTrace();
}
Related
I want to call a specific EditText which is named after my matrice location, I mean, building the id for EditText with a string and setting it afterwards.
now I need to set the editText01 text in the layout, normally I would set like this:
EditText et = (EditText) findViewById(R.id.editText01);
editText01.setText("WHATEVER I NEED");
BUT, I can't access by the id name because I have to access a specific one, based on the row, column so it needs to be something like:
String row = "0"; // row index converted to string, for example
String column = "1"; // column index converted to string, for example
String string = "editText" + row + column; // string should be editText01
string.setText("WHATEVER I NEED"); //WRONG LINE
Solution 1:
In your case, you can check the R.java class and get the id of editText.
But I recommend solution 2 to avoid use reflection in your code.
Here is the code of using reflection.
private int findIdByName(String nameOfId) {
try {
Class IdFolder = Class.forName(context.getPackageName()+".R$id");
Field field = IdFolder.getField(nameOfId);
return (int) field.get(null);
} catch (ClassNotFoundException e) {
Log.e(TAG, "can not find R.java class");
e.printStackTrace();
} catch (NoSuchFieldException e) {
Log.e(TAG, "the field of resource not defined");
e.printStackTrace();
} catch (IllegalAccessException e) {
Log.e(TAG, "can not get static field in R");
e.printStackTrace();
} catch (ClassCastException e) {
Log.e(TAG, "the value of field is not integer");
e.printStackTrace();
}
return 0;
}
String idName = "editText" + row + column; // string should be editText01
int id = findIdByName(idName);
if (id != 0)
EditText editText01 = findViewById(id);
Solution 2:
You must create EditText in for and set an id for each one. Then put each EditText into an array list.
So every time that you want access to an EditText you have all object in the array list. for more understanding what I said see below:
List<EditText> list = new ArrayList();
for (int i=0; i<100; i++) {
EditText editText = new EditText(context);
editText.setId("editText" + row + column);
list.add(editText);
}
and when you want an EditText you can call this method:
private EditText findEditText(String id) {
for (EditText editText: list)
if (editText.getId().equals(id)
return editText;
return null;
}
also don't forget to add each EditText in the view. For example you can put a LinearLayout in your layout and after create each EditText add that into LinearLayout. something like this put in the for:
LinearLayout linear = findViewById(R.id.linear);
for (int i=0; i<100; i++) {
//...
linear.addView(editText)
/...
}
If you do not understand what I said, feel free to put the comment and ask questions.
to create id using String to call specific edit text use this code
String viewID="editText" + row + column; // string should be editText01
//id for view
int resID= getResources().getIdentifier(viewID,"id", getPackageName());
EditText edit= (EditText) findViewById(resID);
edit.setText("WHATEVER I NEED");
In this code create Edittext id using string
You should set the tags for all edit text like
EditText et = new EditText(this);
et.setTag(<some tag >);
then make use to findViewByTag API to retrieve the edit text
EditText et = (EditText)findViewByTag(<tag name>);
You can use getIdentifier():
int id = context.getResources().getIdentifier("editText" + row + column, "id", context.getPackageName());
EditText et = (EditText) findViewById(id);
et.setText("WHATEVER I NEED");
You can omit context inside an activity class.
I am making a simple currency calculator for learning purposes and I just have no idea why my code won't work. Currently, when I press the "Convert" button there is a message saying "Appname stopped working"
Here is the code :
public class MainActivity extends AppCompatActivity {
//Button convertButton;
public void convert(View view) {
EditText dkk = (EditText) findViewById(R.id.dkkField);
EditText gbp = (EditText) findViewById(R.id.gbpField);
EditText eur = (EditText) findViewById(R.id.eurField);
EditText usd = (EditText) findViewById(R.id.usdField);
Double dkkNum = Double.parseDouble(dkk.getText().toString());
Double gbpNum = Double.parseDouble(gbp.getText().toString());
Double eurNum = Double.parseDouble(eur.getText().toString());
Double usdNum = Double.parseDouble(usd.getText().toString());
TextView textView = (TextView) findViewById(R.id.resultView);
double dkkTogbp;
double dkkToeur;
double dkkTousd;
double gbpToDkk;
double eurToDkk;
double usdToDkk;
if(dkk!=null){
dkkTogbp = dkkNum * 0.114001;
dkkToeur = dkkNum * 0.134489;
dkkTousd = dkkNum * 0.142729;
textView.setText(dkk+ "DKK is "+ dkkTogbp+" GBP, "+dkkToeur+"EUR, "+ dkkTousd +"USD");
}else if(gbp!=null){
gbpToDkk = gbpNum * 8.77189;
textView.setText(dkk+ "GBP is "+ gbpToDkk+" DKK");
}else if(eur!=null){
eurToDkk = eurNum * 7.43555;
textView.setText(dkk+ "EUR is "+ eurToDkk+" DKK");
}else if(usd!=null){
usdToDkk = usdNum * 7.00630;
textView.setText(dkk+ "USD is "+ usdToDkk+" DKK");
Toast.makeText(getApplicationContext(), dkk.toString() + " DKK", Toast.LENGTH_LONG).show();
}
dkk.setText("");
gbp.setText("");
eur.setText("");
usd.setText("");
}
I discovered that when i remove the code block below and the if statements the program can run without crashing :
Double dkkNum = Double.parseDouble(dkk.getText().toString());
Double gbpNum = Double.parseDouble(gbp.getText().toString());
Double eurNum = Double.parseDouble(eur.getText().toString());
Double usdNum = Double.parseDouble(usd.getText().toString());
Can someone,please, explain why is this happening?
I don't get any errors.
The problem is by parsing the text to a double value.
At first, you should use a try-catch block to save your application for crashing, like this:
try
{
Double dkkNum = Double.parseDouble(dkk.getText().toString());
Double gbpNum = Double.parseDouble(gbp.getText().toString());
Double eurNum = Double.parseDouble(eur.getText().toString());
Double usdNum = Double.parseDouble(usd.getText().toString());
}
catch(NumberFormatException ex)
{
// If the format of the input string is wrong, then you get here the error message
}
catch(NullPointerException ex)
{
// If the input string is null, then you get here the error message
}
Check, whether the text, that should be parsed, has the right syntax, like "1.9".
UPDATE:
If the input string is null, the "parseDouble" method throws a "NullPointerException". For more informations see this link. I have updated the code.
file.xml
edittext(inputtype=numberdecimal, id=text1) value(1.5)
edittext(inputtype=numberdecimal, id=text2) value("")
file.java
txt1 = ((EditText)findViewById(R.id.Text1));
txt2 = ((EditText)findViewById(R.id.Text2));
s1 = txt1.getText().toString();
v1 = Float.parseFloat(s1); //true
s2 = txt2.getText().toString();
v2 = Float.parseFloat(s2); //error i want parse to (0)
Why is Error?
if text not input data i want default value to zero
"" is not a valid number, so you are most likely getting a NumberFormatException as said in the Android docs. Easiest thing to do is catch that NumberFormatException and set your text there. Repeat as needed.
Eg
try {
v2 = Float.parseFloat(s2);
}
catch (NumberFormatException e)
{
v2 = 0;
}
I want to make the text inside the EditText converted to float
anyone know how to cast from EditText.getText() to float?
I make this solution
float salary= new Float(salaryET.getText().toString());
and it work ok
thanks for every body
Float f = Float.valueOf(EditText.getText().toString());
this code to convert EditText text to float
String str = EditText.getText().toString;
try{
Float f = new Float(str);
System.out.println("Float value is: = " + f);
}catch (Exception e){
System.out.println("Exception: " + e.getMessage());
}
use the above code to convert editable to float. Also added the try catch block for any exceptions while converting.
Updated:::
Float f = Float.parseFloat(str);//you can also use this
Is there way to convert a string to double. This is how i did.
String s = b.getText().toString();
double d = Double.parseDouble(s);
It gives NumberFormatException
Try this
String s = b.getText().toString();
double d = Double.valueOf(s.trim()).doubleValue();
You are probably starting off with a zero-length string. Check the length before you parse it. It also wouldn't hurt to catch NumberFormatException, although that shouldn't be problem if you have android:inputType="number" in the view's layout.
double d = 0;
CharSequence cs = b.getText();
if(cs != null && cs.length() > 0) {
d = Double.parseDouble(cs.toString());
}
i have a similar problem.
for correct formatting EditText text content to double value i use this code:
try {
String eAm = etAmount.getText().toString();
DecimalFormat dF = new DecimalFormat("0.00");
Number num = dF.parse(eAm);
mPayContext.amount = num.doubleValue();
} catch (Exception e) {
mPayContext.amount = 0.0d;
}
this is independet from current phone locale and return correct double value.
hope it's help;