Aubtin Samai
Aubtin Samai

Reputation: 1361

Button Menu Android

I am trying to make a donation menu for my app. I have figured out the part that when a user clicks donate, more buttons come up saying how much. Now, I want to be able to have the amount buttons go away if they click the same button again. I want the regular DonateButton to remain. How would I come about doing that?

I have already set it as invisible using purchaseButton.setVisibility(View.GONE);

Here is the code for clicking the button and the other buttons appearing:

    public void onClick(View v) {
        switch (v.getId()) {

        case R.id.DonateButton:

              purchaseButton.setVisibility(View.VISIBLE);
              purchaseButton2.setVisibility(View.VISIBLE);
              purchaseButton3.setVisibility(View.VISIBLE);

        case R.id.Donate:
                if(BillingHelper.isBillingSupported()){
                        BillingHelper.requestPurchase(mContext, "donate");
                        // android.test.purchased or android.test.canceled or android.test.refunded
        } else {
                Log.i(TAG,"Can't purchase on this device");
        }

                break;
        default:
                // nada
                Log.i(TAG,"default. ID: "+v.getId());
                break;

case R.id.Donatetwo:
    if(BillingHelper.isBillingSupported()){
            BillingHelper.requestPurchase(mContext, "donate2");
            // android.test.purchased or android.test.canceled or android.test.refunded
} else {
    Log.i(TAG,"Can't purchase on this device");
}

    break;

case R.id.Donatethree:
    if(BillingHelper.isBillingSupported()){
            BillingHelper.requestPurchase(mContext, "donate3");
            // android.test.purchased or android.test.canceled or android.test.refunded
} else {
    Log.i(TAG,"Can't purchase on this device");
}

    break;
        }
}

Screenshot of what I mean:

IMAGE URL (DON'T HAVE 10 REPUTATION YET): https://i.sstatic.net/AMdhS.png

What I am trying to say is.

I want it to still allow them to open and close those buttons more than once though.

Upvotes: 1

Views: 462

Answers (2)

Yugandhar Babu
Yugandhar Babu

Reputation: 10349

Try this.

Use getVisibility() method to know the visible state of button.

int visibility;


visibility = button.getVisibility();
if(visibility == View.VISIBLE) {
    button.setVisibility(View.INVISIBLE);
} else {
    button.setVisibility(View.VISIBLE);
}

You have to check like this for every button.


I will suggest one more thing.

Take a global variable and use it as notifier, use below code in all button click functions.

boolean again = false;

if (again) {
    // make all invisible
    again = false;
} else {
    // make all visible
    again = true;
}

Upvotes: 1

ariefbayu
ariefbayu

Reputation: 21979

a simple state variable should do.

put this in your field definition area:

boolean areButtonAmountVisible = false;

and this code as your onClick():

case R.id.DonateButton:

   if( areButtonAmountVisible )
   {
       areButtonAmountVisible = false;
       purchaseButton.setVisibility(View.GONE);
       purchaseButton2.setVisibility(View.GONE);
       purchaseButton3.setVisibility(View.FONE);
   }
   else
   {
       areButtonAmountVisible = true;
       purchaseButton.setVisibility(View.VISIBLE);
       purchaseButton2.setVisibility(View.VISIBLE);
       purchaseButton3.setVisibility(View.VISIBLE);
   }

Upvotes: 1

Related Questions