BigBug
BigBug

Reputation: 6290

CheckedTextView in LinearLayout

I have the bellow xml. I would like to make the imageview clickable. However, it appears that i can only click the checkedTextView. Is there a way where i can have the checkedtextview as well as the imageview independently clickable? It seems like the checkedTextView is consuming the entire space, with the imageview behind the checkedtextview (maybe?). Any ideas? Pretty new to android development, and this has be very confused. I attempted giving weights, but it didn't seem to help at all.

 <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android" 
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent" 
            android:layout_height="60dp" 
            android:orientation="vertical">
        <CheckedTextView
            android:id="@+id/checked_text_view" 
            android:layout_width="match_parent" 
            android:layout_height="match_parent" 
            android:layout_marginLeft="5dp" 
            android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
            android:gravity="center_vertical" 
            android:text="@string/hello_world" 
            android:textColor="@color/white" 
            android:padding="20dp"
            android:textSize="20sp"
            android:textStyle="bold"/>
            <ImageView
                android:id="@+id/my_image_pencil" 
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:src="@drawable/image_pencil" />
        </LinearLayout>

Upvotes: 0

Views: 756

Answers (3)

Piyush
Piyush

Reputation: 18923

Use this one...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="60dp" >

 <CheckedTextView
    android:id="@+id/checked_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:gravity="center|right"
    android:padding="20dp"
    android:text="Hello"
    android:textColor="#fff"
    android:textSize="20sp"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/my_image_pencil"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="16dp"
    android:src="@drawable/ic_launcher" />

</RelativeLayout>

If you want to click event of ImageView in your row item then set

android:focusable="false

for your CheckedTextView.

Upvotes: 1

Sush
Sush

Reputation: 3874

try this,

   <LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent" 
    android:layout_height="60dp" 
        android:orientation="vertical">
<CheckedTextView
    android:id="@+id/checked_text_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="5dp" 
    android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
    android:gravity="center_vertical" 
    android:text="@string/hello_world" 
    android:textColor="@color/white" 
    android:padding="20dp"
    android:textSize="20sp"
    android:textStyle="bold"/>
    <ImageView
        android:id="@+id/my_image_pencil" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onclick="onImageClick" 
        android:src="@drawable/image_pencil" />
</LinearLayout>

In the activity have a method like this

public void onImageClick(View view)
{
  // handler for imageclick
}

Upvotes: 0

Kimmi Dhingra
Kimmi Dhingra

Reputation: 2289

As when we use checkboxes, button, imagebutton etc in list view. It takes all focus of listview, And thus when we click on the listview or other elements of listview like images etc, then there is no any focus on these widgets, And thus no clickable action perform on them.

So just change your code for checkbox like following..

<CheckedTextView
            android:id="@+id/checked_text_view" 
            android:layout_width="match_parent" 
            android:layout_height="match_parent" 
            android:layout_marginLeft="5dp" 
            android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
            android:gravity="center_vertical" 
            android:text="@string/hello_world"  
            android:padding="20dp"
            android:textSize="20sp"
            android:focusable="false"
            android:textStyle="bold"/>

I just added in your code this line.. android:focusable="false

Upvotes: 0

Related Questions