Search code examples
javaandroidandroid-relativelayoutandroid-checkbox

how to check the checkbox when the layout is clicked?


I have a relative layout. In that relative layout i have a textview at left end and check box at right end. Now what i want is, when i clicked the entire layout( above relative layout) the check box need to check. any please help me.

<RelativeLayout
    android:id="@+id/layoutavailable"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/layouttaxexempt"
    android:layout_marginTop="25dp"
    >


    <TextView 
        android:id="@+id/txt_avl"
        android:layout_width="wrap_content"
        android:layout_height="25dp"
        android:hint="IsAlwaysAvailable"
        android:textSize="20sp"
        android:textColorHint="@color/white"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="13dp"
        />

    <CheckBox 
       android:id="@+id/c_avl"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_marginTop="5dp"/>

    </RelativeLayout>

Solution

  • Set the listener to the layout

     CheckBox cBox = (CheckBox) findViewById(R.id.c_avl);
     findViewById(R.id.layoutavailable).setOnClickListener(new OnClickListener() {             
          @Override
          public void onClick(View v) {
              cBox.setChecked(!cBox.isChecked());                
          }
        });