You can use the same button and change its label as an alternative, and by level you can put a check and do the corresponding work.
In accordance with your updated code, find out that you are using ImageView, try this
ImageView btn = (ImageView) findViewById(R.id.secondbutton);
btn.setTag("1");//can use one empty String "" and null instead of "1" and "2" for optimization
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Object obj = v.getTag();
if(obj instanceof String){
if("1".equals(obj)){
v.setTag("2");
//Your first button state
}else if("2".equals(obj)){
v.setTag("1");
//Your second button state
}
}
...
If your case is defined for two buttons, you can use two string comparison operations
ImageView btn = (ImageView) findViewById(R.id.secondbutton);
btn.setTag(null);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(v.getTag() instanceof String){
v.setTag(null);
} else {
v.setTag("");
}
If you want your code to work correctly
btn1 = (ImageView) findViewById(R.id.firstbtn);
btn2 = (ImageView) findViewById(R.id.secondbutton);
btn2.setVisibility(ImageView.GONE);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.setVisibility(ImageView.GONE);
btn2.setVisibility(ImageView.VISIBLE);
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.setVisibility(ImageView.GONE);
btn1.setVisibility(ImageView.VISIBLE);
}
});
source
share