If what you want is to check whether the text box is empty, then you might need to do:
if (Capital.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(), "Please insert something!", Toast.LENGTH_LONG).show();
}
Question
Sorry if this is a nooby question, but I recently tried to catch if a float is null with the if-statement, like:
Calculate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final EditText Capital = (EditText)findViewById(R.id.Capital);
final float Capitalf = Float.valueOf(Capital.getText().toString());
if(Capitalf == "0"){
Toast.makeText(getApplicationContext(), "Please insert something!",
Toast.LENGTH_LONG).show();
}
}
});
I already tried to make a String from Capital and check it with .equals, but it wasnt working. I also tried to catch the exception with try and catch, but the app still crashed by clickling on the button with no values.
It would be great, if anyone could help me, and sorry for my bad english, Im from austria.
Solution
If what you want is to check whether the text box is empty, then you might need to do:
if (Capital.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(), "Please insert something!", Toast.LENGTH_LONG).show();
}
OTHER TIPS
// try this way hope this will help you...
1.Xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:id="@+id/container">
<EditText
android:id="@+id/edtCapital"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Capital"/>
<Button
android:id="@+id/btnCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Calculate">
</Button>
</LinearLayout>
2.Activity
public class MainActivity extends Activity{
private Button btnCalculate;
private EditText edtCapital;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCalculate = (Button) findViewById(R.id.btnCalculate);
edtCapital = (EditText) findViewById(R.id.edtCapital);
btnCalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (edtCapital.getText().toString().length() > 0) {
float value1 = 0f;
float value2 = Float.parseFloat(edtCapital.getText().toString());
if (value1 == value2){
Toast.makeText(MainActivity.this,"Capital Value Can Not Be 0",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"Capital Value Is "+value2,Toast.LENGTH_SHORT).show();
}
} else {
edtCapital.setError("Value Required");
}
}
});
}
}