Same button with multiple taps

The idea is that a button can do one thing on the first click and another thing on the second click.

button_food   = (Button) findViewById(R.id.foodicon_layout);
button_travel = (Button) findViewById(R.id.travelicon_layout);
button_fuel   = (Button) findViewById(R.id.fuelicon_layout);
button_fetch  = (Button) findViewById(R.id.fetchicon_layout);

button_travel.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        // Perform action on click
        button_food.setVisibility(View.GONE);
        button_fuel.setVisibility(View.GONE);
        button_fetch.setVisibility(View.GONE);
    }
});

      

In this example, when the button_travel button is clicked, the other buttons become invisible. on clicking this again, I want the other buttons to be visible again.

+3
java android


source to share


6 answers


You can force the button to set its visibility by getting their current visibility and toggling it.

button_food   = (Button) findViewById(R.id.foodicon_layout);
button_travel = (Button) findViewById(R.id.travelicon_layout);
button_fuel   = (Button) findViewById(R.id.fuelicon_layout);
button_fetch  = (Button) findViewById(R.id.fetchicon_layout);

button_travel.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        int visibility = button_food.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE;

        // Perform action on click
        button_food.setVisibility(visibility);
        button_fuel.setVisibility(visibility);
        button_fetch.setVisibility(visibility);
    }
});

      



Writing this type is just a simple way to write an if statement

int visibility;
if(button_food.getVisibility() == View.VISIBLE){
     visibility = View.GONE;
} else {
     visibility = View.VISIBLE;
}

      

+7


source to share


Just check the current state and act accordingly.



public void onClick(View v) {

    // Perform action on click
    if (button_food.getVisibility() == View.VISIBLE) {
        button_food.setVisibility(View.GONE);
    } else {
        button_food.setVisibility(View.VISIBLE
    }

    if (button_fuel.getVisibility() == View.VISIBLE) {
        button_fuel.setVisibility(View.GONE);
    } else {
        button_fuel.setVisibility(View.VISIBLE
    }

    if (button_fetch.getVisibility() == View.VISIBLE) {
        button_fetch.setVisibility(View.GONE);
    } else {
        button_fetch.setVisibility(View.VISIBLE
    }
}

      

+5


source to share


Are you looking for this ?:

public void onClick(View v) {

             if(visible){
                    visible=false;
                    button_food.setVisibility(View.GONE);
                    button_fuel.setVisibility(View.GONE);
                    button_fetch.setVisibility(View.GONE);
             }else{ 
                    visible=true;
                    button_food.setVisibility(View.VISIBLE);
                    button_fuel.setVisibility(View.VISIBLE);
                    button_fetch.setVisibility(View.VISIBLE);
            }
    }

      

+3


source to share


what you can do is check their visibility if they are visible that set visiblity for go else added visisiblity in Visible for reference, see below code

  if (button_food.getVisibility()==View.VISIBLE) {
  button_food.setVisibility(View.GONE);
    } else {
         button_food.setVisibility(View.VISIBLE);
    }

if (button_fuel.getVisibility()==View.VISIBLE) {
  button_fuel.setVisibility(View.GONE);
    } else {
         button_fuel.setVisibility(View.VISIBLE);
    }

 if (button_fetch.getVisibility()==View.VISIBLE) {
         button_fetch.setVisibility(View.GONE);
    } else {
         button_fetch.setVisibility(View.VISIBLE);
    }

      

+2


source to share


If you are using an array I think it is too simple

Button btnArray[] = new Button[4];

    btnArray[0] = (Button) findViewById(R.id.button_food);
    btnArray[1] = (Button) findViewById(R.id.button_travel);
    btnArray[2] = (Button) findViewById(R.id.button_fuel);
    btnArray[3] = (Button) findViewById(R.id.button_fetch);


    View.OnClickListener btnListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            for (int i = 0; i < btnArray.length; i++) {
                if (btnArray[i].getId() != v.getId())
                    if (btnArray[i].getVisibility() == View.GONE)
                        btnArray[i].setVisibility(View.VISIBLE);
                    else
                        btnArray[i].setVisibility(View.GONE);
            }
        }
    };
    for (int i = 0; i < btnArray.length; i++)
        btnArray[i].setOnClickListener(btnListener);

      

+1


source to share


I think this is the cleanest way:

button_food   = (Button) findViewById(R.id.foodicon_layout);
button_travel = (Button) findViewById(R.id.travelicon_layout);
button_fuel   = (Button) findViewById(R.id.fuelicon_layout);
button_fetch  = (Button) findViewById(R.id.fetchicon_layout);
private boolean visible;

button_travel.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        if(visible){
            visible=false;
            button_food.setVisibility(View.GONE);
            button_fuel.setVisibility(View.GONE);
            button_fetch.setVisibility(View.GONE);
        }else{ 
            visible=true;
            button_food.setVisibility(View.VISIBLE);
            button_fuel.setVisibility(View.VISIBLE);
            button_fetch.setVisibility(View.VISIBLE);
        }
    }
});

      

+1


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics