0

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 {...
Community
  • 1
  • 1
vinuraj_pg
  • 57
  • 1
  • 8
  • If you just need to check the checkbox programmatically, you can use ```CheckBox.setChecked(true)```. –  May 30 '20 at 08:01
  • @upgye1wi I Need the checkbox as checked and visible when someone taps there. I tried and it doesn't changes anything ! – vinuraj_pg May 30 '20 at 08:15
  • Then you can set onClickListener to the checkbox and it must do the trick. Making a view invisible doesn't disable its listener. https://stackoverflow.com/questions/32140625/android-invisible-objects-still-clickable –  May 30 '20 at 08:27
  • 1
    In xml replace your `CheckBox` `invisible` to `gone` – Dinesh May 30 '20 at 08:41
  • @Dinesh This seems to be working with onClickListener and setting to gone. – vinuraj_pg May 30 '20 at 09:04
  • @vinuraj_pg or you can set alpha property to 0 and then back to 1. Check my edited answer. – cgb_pandey May 30 '20 at 09:19

3 Answers3

1

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);
        }
    });
Raul Potor
  • 29
  • 3
0

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);
                }
            });
cgb_pandey
  • 985
  • 1
  • 11
  • 18
0

Simply, use onCheckedChangeListener

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

}

Stack Fox
  • 1,201
  • 9
  • 14