Reputation: 117
COnsider this following code:
Button add_product_button = (Button) findViewById(R.id.add_product_button);
add_product_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Before going further, we check whether all the information is provided or not
EditText item_title_text = (EditText) findViewById(R.id.item_title);
if(isEmpty(item_title_text)){
break;
}
I want the 'break;' to be the response that I would not want to proceed further (not executing the rest code and just get out).
This is to alarm user that insufficient information has been provided. How can I achieve this?
Upvotes: 0
Views: 5008
Reputation: 178
To get out of it without going on with the code just
if(isEmpty(item_title_text)){
return;
}
You might want to show a Toast with the error in there:
if(isEmpty(item_title_text)){
Toast.makeText(...);
return;
}
Upvotes: 0
Reputation: 1127
I would actually recommend disabling your button if the required information is not there so the user isn't able to click it. You could display a message on whatever dialog/view to inform the user that information is required, or add a string like "(required)" after the EditText hint.
Button add_product_button = (Button) findViewById(R.id.add_product_button);
EditText item_title_text = (EditText) findViewById(R.id.item_title);
if(isEmpty(item_title_text)){
add_product_button.setEnabled(false);
}
However, if you need to do it the way you described for some reason, to answer how you "get out", you just need to return
out of the onClick method.
if(isEmpty(item_title_text)){
return;
}
Before returning you could call some method that does whatever you want to do (your own method that perhaps displays a message to the user that they have not enough information).
if(isEmpty(item_title_text)){
notifyUserOfEmptyField();
return;
}
Upvotes: 1