Reputation: 120
I am trying to hide a button using the View.GONE method. But since I have a lot of validations, I can't seem to hide the button in my latest validation. Is there a way to override all other hide button commands? There is nothing wrong with my logic. I tested them using Toasts. But the button simply doesn't hide. I tried making it not clickable. It doesn't work either. This is my code. I'm trying to hide the button if the date in my TextView is lesser than today.
SimpleDateFormat dateFormat = new SimpleDateFormat("M-dd-yyyy");
try {
today = dateFormat.parse(dateFormat.format(new Date()));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String chkdate = edate.getText().toString();
SimpleDateFormat sdf = new SimpleDateFormat("M-dd-yyyy");
try {
mydate = sdf.parse(chkdate);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
result = mydate.compareTo(today);
System.out.println("result "+result);
}catch(NullPointerException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
if(result < 0){
don.setClickable(false);
don.setVisibility(GONE);
Toast toast= Toast.makeText(getApplicationContext(),
"button gone", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
else if(result >= 0){
don.setClickable(true);
don.setVisibility(VISIBLE);
Toast toast= Toast.makeText(getApplicationContext(),
"Button visible", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
Upvotes: 1
Views: 35
Reputation: 1318
I guess it's hard to explain like this. What I believe is that, you have to add the new validations where your other validations are. So that it doesn't conflict and it easier for you to debug
Upvotes: 1
Reputation: 655
you should use don.setVisibility(View.INVISIBLE)
....
if(result < 0){
don.setClickable(false);
don.setVisibility(View.INVISIBLE)
Toast toast= Toast.makeText(getApplicationContext(),
"button gone", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
....
Upvotes: 1
Reputation: 969
Do you not need
don.setVisibility(LinearLayout.GONE)
after if(result < 0){
Upvotes: 0