Shasi
Shasi

Reputation: 274

How to make item visible and invisible by clicking same line?

I have one Linearlayout - totalincome and another TableLayout normalincometable, which should appear just below the totalincome. normalincometable will be invisible when the program runs. When the user clicks on "totalincome" the table should display. If the user clicks on "totalincome again", the table should disappear. I have tried this code, but It didnt work.

totalincome.setOnClickListener(new OnClickListener() 
{
    @Override
    public void onClick(View v) 
    {
        int x =0;
        // TODO Auto-generated method stub
        if (x==0)
        {
            normalincometable.setVisibility(View.VISIBLE);
            x=1;
        }
        else 
        {
            normalincometable.setVisibility(View.GONE);
            x=0;
        }
    });
}

From this code, I can make the table visible in first click but It doesnt disappear in next click. Are there any options ?

Upvotes: 1

Views: 1640

Answers (7)

TechArcSri
TechArcSri

Reputation: 2002

Easiest Method is

button.setVisibility(View.VISIBLE == button.getVisibility() ? View.GONE:View.VISIBLE);

Upvotes: 0

Neoh
Neoh

Reputation: 16164

Try this:

@Override
public void onClick(View v) {
  if(normalincometable.getVisibility() == View.VISIBLE) {
    normalincometable.setVisibility(View.GONE);
  } else {
    normalincometable.setVisibility(View.VISIBLE);
  }
}

Upvotes: 4

jyomin
jyomin

Reputation: 1967

Try this

Boolean isFirstTimeClicked=true;
totalincome.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (isFirstTimeClicked)
            {
            normalincometable.setVisibility(View.VISIBLE);
            }
            else 
            {
                normalincometable.setVisibility(View.GONE);

                            }
isFirstTimeClicked=!isFirstTimeClicked;

    });

}

and in your code you have declared int x =0; inside onClick method. So, when ever onClick is called, it assigns 0 to "x". Declare it outside at class scope.

Upvotes: 0

Kishan Dhamat
Kishan Dhamat

Reputation: 3784

Use like this:

   int x =0;    
    totalincome.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        // TODO Auto-generated method stub
                        if (x==0)
                        {
                        normalincometable.setVisibility(View.VISIBLE);

                        x=1;
                        }
                        else 
                        {
                            normalincometable.setVisibility(View.GONE);
                                                x=0;
                                        }


                });

            }

Upvotes: 0

M D
M D

Reputation: 47817

try this way:put x variable outside the button onclick() or defined x globally

int x =0;
totalincome.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        // TODO Auto-generated method stub
        if (x==0)
        {
        normalincometable.setVisibility(View.VISIBLE);

        x=1;
        }
        else 
        {
            normalincometable.setVisibility(View.GONE);
                                x=0;
                        }


});

}

Upvotes: 1

Sushil
Sushil

Reputation: 8488

You have declared int x =0; inside onClick method. So, when ever onClick is called, it assigns 0 to "x". Declare it outside at class scope.

Upvotes: 1

Waqar Ahmed
Waqar Ahmed

Reputation: 5068

because you have define x in the button click code so whenever button click it set to 0. define x outside the button click scope.

Upvotes: 1

Related Questions