Ziwei Zeng
Ziwei Zeng

Reputation: 701

Android layout not clickable when include ImageView and TextView

I have a layout like following. There are total five similar tabs. When I set onClickListener to the LinearLayout tab_dashboard in code, nothing fired. I have to set click listener to the ImageButton and TextView as well to make the whole area clickable. I tried to set all child items clickable="false" and duplicateParentState="true", no luck. I also tried to change the ImageButton to ImageView, same issue. Anyone has idea on this?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/tab_dashboard"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/white"
        android:gravity="center"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/dashboard_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="@android:color/white"
            android:src="@drawable/ic_dashboard" />

        <TextView
            android:id="@+id/dashboard_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:text="@string/dashboard"
            android:textSize="10sp" />

    </LinearLayout>

    <!-- Four more similar tabs -->

</LinearLayout

Upvotes: 1

Views: 1256

Answers (2)

Ziwei Zeng
Ziwei Zeng

Reputation: 701

I resolved the issue by replacing the android:layout_width="0dp" and android:layout_weight="1" with other way that achieve the same effect. I guess the android:layout_width="0dp" may the cause of this issue.

Upvotes: 0

To the TextView and ImageView add android:clickable="false" and android:focusable="false"

In your activity add following code.

((ImageView) findViewById(R.id.dashboard_button)).setOnClickListener(null);
((ImageView) findViewById(R.id.dashboard_button)).setFocusable(false);
((TextView) findViewById(R.id.dashboard_text)).setOnClickListener(null);
((TextView) findViewById(R.id.dashboard_text)).setFocusable(false);

Upvotes: 0

Related Questions