Artūrs Eimanis
Artūrs Eimanis

Reputation: 596

How to disable all touches for a Layout in Android

I am trying to implement a bottom sheet in Xamarin.Android and I want to disable all touches on everything that is not the bottom sheet (i.e. main layout), when the bottom sheet is active.

Main.axml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main_layout">
<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

<!-- Main Layou -->
<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/main_content"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >
    <Button
        android:id="@+id/btn_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/logout"
        android:layout_gravity="center" />
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@drawable/ic_plus" />
</FrameLayout>

<FrameLayout
    android:id="@+id/tint"
    android:background="#000000"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- Bottom sheet -->
<FrameLayout
    android:id="@+id/bottom_fragment_container"
    android:layout_width="match_parent"
    android:layout_height="160dp"
    android:background="#E0E0E0"
    android:layout_gravity="bottom"
    android:elevation="16dp"
    android:translationY="160dp" />

I tried iterating over all the children and disabling them, but that changes the appearance of the button and I want to avoid that. I also tried setting the Enabled property of the layout to false, but the button was still touchable.

I think i need to somehow intercept all touches before they reach the button (and everything else that will be there later), but I don't know how to do that. By intercepting all touches I could also implement hiding of the bottom sheet after those touches.

Upvotes: 1

Views: 1620

Answers (1)

William Barbosa
William Barbosa

Reputation: 4995

Create a transparent RelativeLayout that covers the entire screen and use the android:clickable="true" property in it to make sure it intercepts all the click events.

Then add android:layout_alignParentBottom to your bottom sheet and put it inside this newly created RelativeLayout. This way it'll appear at the bottom and everything else won't be clickable.

<FrameLayout>

  <!-- Main Layout -->


  <RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0000"
    android:clickable="true">

     <FrameLayout
       android:id="@+id/bottom_fragment_container"
       android:layout_width="match_parent"
       android:layout_height="160dp"
       android:background="#E0E0E0"
       android:layout_alignParentBottom="true"
       android:elevation="16dp"
       android:translationY="160dp" />

  </RelativeLayout>

</FrameLayout>

Upvotes: 2

Related Questions