vinuraj_pg
vinuraj_pg

Reputation: 57

How to check invisible checkbox in android studio

I have created a checkbox with text and set as invisible. Currently it is unchecked and I know the position of the checkbox as I have boxes above. I want the box to be checked and visible when I tap at the position ( invisible at the moment ).

I am not sure if this is possible or not as I could not find anything after hours of Googling.Sharing the codes below.

xml code

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="5dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_diabetes"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:layout_gravity="center_vertical"
                android:fontFamily="@font/ubuntu_regular"
                android:layout_weight="0.2"
                android:text="@string/diabetes"
                android:textColor="@color/textColorAsh"
                android:textSize="16sp" />

            <CheckBox
                android:id="@+id/check_diabetes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:theme="@style/checkBoxStyle"
                android:visibility="invisible" />
        </LinearLayout>

Here is how I am try to make it visible programmatically. However it is not getting visible when I tap on the place where it is rendered.

       case R.id.check_diabetes:
            if (b == true) {
                tv_diabetes.setTextColor(getResources().getColor(R.color.textColorBlue));
                check_diabetes.setVisibility(View.VISIBLE);
                diabetes = "1";
            } else {...

Upvotes: 0

Views: 1145

Answers (3)

cgb_pandey
cgb_pandey

Reputation: 1025

This is a hacky way to get what you want. In your layout file, set android:alpha="0", but set android:visibility="true" all the time.
And then in code

final CheckBox checkDiabetes= new CheckBox(R.id.check_diabetes);
            checkDiabetes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked)
                checkBox.setAlpha(1f);
            else
                checkBox.setAlpha(0f);
                }
            });

Upvotes: 0

Stack Fox
Stack Fox

Reputation: 1211

Simply, use onCheckedChangeListener

checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
  if(isChecked) {
  buttonView.setVisibility = View.VISIBLE

}

Upvotes: 0

Raul Potor
Raul Potor

Reputation: 29

You could try adding an onClickListener like this:

 check_diabetes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            check_diabetes.setVisibility(View.VISIBLE);
            check_diabetes.setChecked(true);
        }
    });

Upvotes: 1

Related Questions