Reputation: 13
I am trying to make a unit converter and it seems its working i just need to give them an error if they don't put any number on the first field and other error if they put a number on the second field (so they cant convert it backwards)
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editCentimeters = (EditText) findViewById(R.id.editCentimeters);
final EditText editInches = (EditText) findViewById(R.id.editInches);
Button buttonConvert = (Button)findViewById(R.id.buttonConvert);
buttonConvert.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
double centimeters = Double.valueOf( editCentimeters.getText().toString());
double inches = centimeters * 0.393700787 ;
editInches.setText(String.valueOf(inches));
}
});
}
}
Upvotes: 0
Views: 86
Reputation: 10829
editinches should b a textview so that user can't edit or add any value... Next use should do a check if editcentimeter is empty, if yes you display an error... Can be Alertdialog, toast to show the error message.
if(editcentimeter.isEmpty(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle("Missing value");
// set dialog message
alertDialogBuilder
.setMessage("Please enter a value");
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Please enter a numbert!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dismiss();
}
});
}
}
Upvotes: 0
Reputation: 1408
You can use Toast to give a error message.
But I use EditText.setError() method. So user will be able see exactly which field is responsible for validation problem
Click HERE for a tutorial and Here for another solid tutorial.
This is a sample code (I do not have ADT in here, so forgive me if it needs some correction):
final EditText editCentimeters = (EditText) findViewById(R.id.editCentimeters);
final EditText editInches = (EditText) findViewById(R.id.editInches);
@Override
public void onClick(View arg0) {
boolean isValid = true;
if (editCentimeters.getText().toString().isEmpty())
{
editCentimeters.setError("This input cannot be null");
isValid = false;
}
if (editInches.getText().toString().isEmpty())
{
editInches.setError("This input cannot be null");
isValid = false;
}
if (isValid)
{
double centimeters = Double.valueOf( editCentimeters.getText().toString());
double inches = centimeters * 0.393700787 ;
editInches.setText(String.valueOf(inches));
}
}
Upvotes: 1
Reputation: 4292
You must check the value inside each EditText
yourself, and notify the user if its empty:
buttonConvert.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(!"".equals(editCentimeters.getText().toString())) {
// If the field is not empty, proceed to calculation
double centimeters = Double.valueOf( editCentimeters.getText().toString());
double inches = centimeters * 0.393700787 ;
editInches.setText(String.valueOf(inches));
} else {
// If it's empty, show a message to the user)
editCentimeters.setError("This field can not be empty");
}
}
};
Upvotes: 0
Reputation: 83
The Toast class will be suitable for this purpose as:-
if(editText.getText().toString().equals(""))
Toast.makeText(this,"Your Error msg",Toast.LENGTH_LONG).show();
The first argument is the Context of your Activity
Second is the CharSequence
And third is the duration of the Toast.
Upvotes: 0
Reputation: 8856
use setError() method for this. do somthing like this
buttonConvert.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(editCentimeters.getText().toString().equals("")){
editCentimeters.setError("*Field Required");
}else{
double centimeters = Double.valueOf( editCentimeters.getText().toString());
double inches = centimeters * 0.393700787 ;
editInches.setText(String.valueOf(inches));
}
}
});
Upvotes: 0