Reputation:
I have a button which goes to another page when the edittext textfields have been entered correctly. However what I want to do is make a toast appear if the user has not entered in the textfields correctly. The error checking does work, as it prevents the user going to the next page, i.e. if there isn't 16 numbers in cardchecker field or 3 numbers in cvv checker field.
This is for a windows server. I have tried changing after the Toast.makeText(, to getActivity().getApplicationcontext
, this
& getActivity
on its own.
public class DonateFragment extends Fragment {
EditText cardno;
Button buy;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_donate,container,false);
final EditText cardcheck1 =(EditText) rootView.findViewById(R.id.cardno);
final EditText sortcheck1 =(EditText) rootView.findViewById(R.id.sortcode);
final EditText cvvcheck1 =(EditText) rootView.findViewById(R.id.cvv);
final EditText amountcheck1 =(EditText) rootView.findViewById(R.id.amount);
Button buy = (Button) rootView.findViewById(R.id.buy);
buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String cardcehcker = cardcheck1.getText().toString();
String sortchecker = sortcheck1.getText().toString();
String cvvchecker = cvvcheck1.getText().toString();
String amountchecker = amountcheck1.getText().toString();
if (cardcehcker.trim().length() == 16){
if (cardcehcker.trim().length() < 16){
Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
}
if (sortchecker.trim().length() == 6)
if (cvvchecker.trim().length() == 3)
if(amountchecker.trim().length()>0){
{
Intent in = new Intent(getActivity(), Donation_thankyou_activity.class);
startActivity(in);
}}}}
});
return rootView;
}
}
I expect a toast to appear if the user has not entered it in correctly, but no toast appears.
Upvotes: 0
Views: 99
Reputation: 67
try this
public class DonateFragment extends Fragment {
EditText cardno;
Button buy;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_donate,container,false);
final EditText cardcheck1 =(EditText) rootView.findViewById(R.id.cardno);
final EditText sortcheck1 =(EditText) rootView.findViewById(R.id.sortcode);
final EditText cvvcheck1 =(EditText) rootView.findViewById(R.id.cvv);
final EditText amountcheck1 =(EditText) rootView.findViewById(R.id.amount);
Button buy = (Button) rootView.findViewById(R.id.buy);
buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String cardcehcker = cardcheck1.getText().toString();
String sortchecker = sortcheck1.getText().toString();
String cvvchecker = cvvcheck1.getText().toString();
String amountchecker = amountcheck1.getText().toString();
if (cardcehcker.trim().length() == 16 && sortchecker.trim().length() == 6 && cvvchecker.trim().length() == 3 && amountchecker.trim().length()>0){
Intent in = new Intent(getActivity(), Donation_thankyou_activity.class);
startActivity(in);
}
else{
Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
}
}
});
return rootView;
}
}
Upvotes: 2