I am trying to get a message to appear when a button is clicked to tell the user to fill in the blank field. Currently, if the field is blank, it crashes/force closes the app. the criteria that i need are to have spinners for the Celsius/Fahrenheit options and toast messages if the spinners are both on the same option or if the value is blank
Here is the code
public class MainActivity extends AppCompatActivity {
private TextView result;
private EditText enterTemp;
double result0;
Spinner ConvertFromUnitTypeSpinner;
Spinner ConvertToUnitTypeSpinner;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//set spinner variables
ConvertFromUnitTypeSpinner = (Spinner) findViewById(R.id.ConvertFromUnitTypeSpinner);
ConvertToUnitTypeSpinner = (Spinner) findViewById(R.id.ConvertToUnitTypeSpinner);
//String for spinner items
String [] unitTypes = {"Fahrenheit", "Celsius"};
//set up adapter for the layout of the spinner
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, unitTypes);
//set up adapter to spinner
ConvertFromUnitTypeSpinner.setAdapter(spinnerAdapter);
ConvertToUnitTypeSpinner.setAdapter(spinnerAdapter);
//set up convert button
Button convert = findViewById(R.id.convert);
result = findViewById(R.id.result);
enterTemp = findViewById(R.id.enterTemp);
convert.setOnClickListener(new View.OnClickListener(){
@SuppressLint("SetTextI18n")
@Override
public void onClick (View v) {
double temp = Double.parseDouble(enterTemp.getText().toString());
if(ConvertFromUnitTypeSpinner.getSelectedItem()=="Fahrenheit"&&
ConvertToUnitTypeSpinner.getSelectedItem().toString()=="Celsius")
{
result0 = (temp - 32 )*5/9;
result.setText(String.valueOf(result0));
}
if(ConvertFromUnitTypeSpinner.getSelectedItem()=="Celsius"
&&ConvertToUnitTypeSpinner.getSelectedItem().toString()=="Fahrenheit")
{
result0 = (temp * 9/5) 32;
result.setText(String.valueOf(result0));
}
if (ConvertToUnitTypeSpinner.getSelectedItem()
==ConvertToUnitTypeSpinner.getSelectedItem()){
result.setText("Units are the same!");
}
if (enterTemp.getText().toString().length() == 0)
{
Toast.makeText(MainActivity.this, "It's empty", Toast.LENGTH_SHORT).show();
}
}
});
}
}
I tried replacing the code
if (enterTemp.getText().toString().length() == 0) {
Toast.makeText(MainActivity.this, "It's empty", Toast.LENGTH_SHORT).show();
}
to this
if(isEmpty(enterTemp.getText().toString())){
Toast.makeText(MainActivity.this, "It's empty", Toast.LENGTH_SHORT).show();
}
also no luck
CodePudding user response:
You should use TextUtils
to achieve your desired result. Something like:
import android.text.TextUtils;
if ( TextUtils.isEmpty(enterTemp.getText()) ) {
// Display the toast
}
CodePudding user response:
Your code will be:
if(enterTemp.getText().toString().trim().length() == 0 ){
Toast.makeText(MainActivity.this, "It's empty", Toast.LENGTH_SHORT).show();
}
CodePudding user response:
Try this->
if (enterTemp.getText().toString().equals("")) {
Toast.makeText(MainActivity.this, "It's empty", Toast.LENGTH_SHORT).show();
}
CodePudding user response:
First of all you should check EditText is empty or not and in else part do your conversion:
if (enterTemp.getText().toString().isEmpty())
{
Toast.makeText(MainActivity.this, "It's empty", Toast.LENGTH_SHORT).show();
} else {
if(ConvertFromUnitTypeSpinner.getSelectedItem()=="Fahrenheit"&&
ConvertToUnitTypeSpinner.getSelectedItem().toString()=="Celsius")
{
result0 = (temp - 32 )*5/9;
result.setText(String.valueOf(result0));
}
if(ConvertFromUnitTypeSpinner.getSelectedItem()=="Celsius"
&&ConvertToUnitTypeSpinner.getSelectedItem().toString()=="Fahrenheit")
{
result0 = (temp * 9/5) 32;
result.setText(String.valueOf(result0));
}
if (ConvertToUnitTypeSpinner.getSelectedItem()
==ConvertToUnitTypeSpinner.getSelectedItem())
{
result.setText("Units are the same!");
}
}