Think I have 4 button on navbar. First button contains some data or items and other buttons also contains some data or items.
Note: All four buttons under a same xml file.
I want if I click first button it will show me first button data or items and same time it will hide others three buttons data or items. And I want to use same method for all buttons.
How can I do that please help me.
CodePudding user response:
A button has different visibility, if you want to put your button non visible and non clickable programmatically the you should use:
Button button;
button.setVisibility(View.GONE);
If you don't want to see your button but you want an onClick than use:
Button button;
button.setVisibility(View.INVISIBLE);
CodePudding user response:
As you want to hide other three buttons you can use switch case where you can get the click of the button and on the click of one button you can hide all other buttons.
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
Button b3 = (Button) findViewById(R.id.button3);
Button b4 = (Button) findViewById(R.id.button4);
b1.setOnClickListener(this)
b2.setOnClickListener(this)
b3.setOnClickListener(this)
b4.setOnClickListener(this)
when you define onclick method you will get the id of the clicked view.
public void onClick(View v) {
// Perform action on click
switch(v.getId()) {
case R.id.button1:
b1.setVisibility(View.VISIBLE);
b2.setVisibility(View.GONE);
b3.setVisibility(View.GONE);
b4.setVisibility(View.GONE);
break;
case R.id.button2:
b1.setVisibility(View.GONE);
b2.setVisibility(View.VISIBLE);
b3.setVisibility(View.GONE);
b4.setVisibility(View.GONE);
break;
case R.id.button3:
b1.setVisibility(View.GONE);
b2.setVisibility(View.GONE);
b3.setVisibility(View.VISIBLE);
b4.setVisibility(View.GONE);
break;
case R.id.button1:
b1.setVisibility(View.GONE);
b2.setVisibility(View.GONE);
b3.setVisibility(View.GONE);
b4.setVisibility(View.VISIBLE);
break;
}
}
