Carlos J
Carlos J

Reputation: 3045

Make layout clickable

In my app I have several fragments with different ListViews, but for all them I want to use the same empty view, so I created a separate layout with the empty view and added it to the other layouts using

<include
    android:id="@+id/empty"
    layout="@layout/empty_view"/>

Everything works as expected but now I'm trying to make that view clickable so I can reload ListView when the user taps on the empty view, however I can't make it work. Any idea on how to do it?

This is what I have tried so far:

(empty_view.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:gravity="center">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="center"
        android:src="@drawable/error_icon"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#929292"
        android:textSize="24sp"
        android:text="@string/empty_list_error"/>

</LinearLayout>

fragment_list.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true">

    <Spinner
        android:id="@+id/spinner_filter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="@dimen/activity_vertical_margin"
        android:layout_marginLeft="@dimen/activity_vertical_margin"
        android:layout_gravity="center_horizontal"
        android:visibility="gone"/>

    <include
        android:id="@+id/empty"
        layout="@layout/empty_view"/>

    <ProgressBar
        android:id="@+id/loading_indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:indeterminate="true"
        style="@android:style/Widget.Holo.ProgressBar.Large"/>

    <LinearLayout
        android:id="@+id/list_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary_color"
        android:padding="@dimen/activity_vertical_margin"
        android:layout_below="@id/spinner_filter"
        android:visibility="gone">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="5"
            android:layout_height="wrap_content"
            android:textColor="@color/white_90pct"
            android:textAllCaps="true"
            android:text="@string/subject_indicator"/>

        <View
            android:layout_width="1dp"
            android:layout_height="15dp"
            android:layout_gravity="center_vertical"
            android:background="@color/light_color"/>

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:gravity="end"
            android:textColor="@color/white_90pct"
            android:textAllCaps="true"
            android:text="@string/grade_indicator"/>

    </LinearLayout>

    <ListView
        android:id="@+id/semesters_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/list_header"/>

</RelativeLayout>

Java class (onCreateView)

LinearLayout emptyView = (LinearLayout) view.findViewById(R.id.empty);
emptyView.setOnClickListener(this);

I also tried adding the next attributes to the xml but still not working:

android:focusableInTouchMode="true"
android:focusable="true"
android:descendantFocusability="blocksDescendants"

Upvotes: 1

Views: 3289

Answers (3)

Dave T.
Dave T.

Reputation: 1408

Try emptyView.setOnTouchListener()

Are you including empty_view into another view that blocks descendant focusability?

Since your edit

Your ListView is on top of the empty_view and blocking clickability.

Upvotes: 0

Michael Krause
Michael Krause

Reputation: 4849

Since your empty view fills its parent, make sure your include tag for your empty view is the last in the other layout so it will be top-most in the z-order.

Upvotes: 0

IAmGroot
IAmGroot

Reputation: 13855

Since you have already specified the linearlayout to be clickable, you just need to specify the method for the onClick event.

So add you your linearLayouts xml, the onClick attribute as below: (focusable may be needed too)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:clickable="true"
  android:onClick="layoutClicked"
  android:focusable="true"
  android:gravity="center">

Then in your code have the method to go with it

 public void layoutClicked(View v)
 {
       // Your code here
 }

Upvotes: 2

Related Questions