NikhilReddy
NikhilReddy

Reputation: 6954

Button should work as check box

Hi guys,

I am new to Android. I am having a button and layout, and I want to write a program such that when the button is selected the layout should be visible - otherwise, the layout should be invisible.

Upvotes: 0

Views: 86

Answers (2)

IvoryCirrus
IvoryCirrus

Reputation: 622

if you wanna use normal button, see below

    //create ImageView
    final ImageView imgView = new ImageView(this);
    imgView.setImageResource(android.R.drawable.btn_star);

    //create button and set action
    Button checkBtn = new Button(this);
    checkBtn.setText("Check Button");
    checkBtn.setOnClickListener(new OnClickListener() {
        private boolean ischecked = false;
        @Override
        public void onClick(View v) {
            ischecked = !ischecked;
            imgView.setVisibility(ischecked?View.VISIBLE:View.INVISIBLE);
        }
    });

    this.addContentView(imgView, null);
    this.addContentView(checkBtn, null);

Upvotes: 0

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

use toggle button

togglebutton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            if (togglebutton.isChecked()) {    // button on layout visible                  
                alarm_LL.setVisibility(View.VISIBLE);
            } else {                // button off layout invisible   
                alarm_LL.setVisibility(View.INVISIBLE);
            }
        }
    });                 
  }
}); 

Upvotes: 2

Related Questions