Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable any event on a View in Android?

My question is simple: How to disable any event on a View in Android? (including removing its focussability, like I just want it to be there visually but be inexistant on everything else) And does it work on a whole view tree? (like if I disable events on the root, all the events will be disabled for its children?).

Now, before you say anything I have tried all the following:

  • setEnabled
  • setFocusable
  • setSelected
  • setClickable
  • setActivated

And none of these methods appear to work, seriously.

I have tried them directly on a WebView, as well as on the parent layout on everything but I am still able to interact with it.

Any idea?

Thanks!

EDIT#1

The solution that consists in adding a view on top of the view that needs to be disabled doesn't work. Actually, it's still possible to click on the inner view, I have tried with a simple example:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#ff0000">
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Click Me!"
        />
    </LinearLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00000000"
    />
</FrameLayout>

Here it's still possible to click on the button.

EDIT#2

The reason why I want to do this is related to the following question that I asked weeks ago.

What I have is a ListViewacting as a navigation bar which is underneath a View that holds the content of my app. The problem with this implementation is that when I try to scroll through the ListView when there is a focusable view in the layer on top of it, well the ListView doesn't scroll and instead it's the top view that takes focus (That's the case when there is a Webview or an EditText etc.).

So yes as mentioned in one of the answers, I can disable any click events on a WebView by overriding setOnTouchListener but the view remains focussed and I think this is the reason why I am still having the same issue with my navigation bar.

like image 874
Amokrane Chentir Avatar asked May 07 '12 10:05

Amokrane Chentir


People also ask

How do I disable click listener?

This example demonstrates how to remove onClickListener for a view in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/acitivity_main. xml.

What is a ViewGroup?

ViewGroup is a collection of Views(TextView, EditText, ListView, etc..), somewhat like a container. A View object is a component of the user interface (UI) like a button or a text box, and it's also called a widget.


1 Answers

Simply put a view on top of your view. You can toggle it on off by setting view.visibility = gone/visible.

<FrameLayout>
<WebView/>
<FrameLayout This view will be on top/>
</FrameLayout>

Edit: Just stumpled upon this link: https://stackoverflow.com/a/3856199/969325 Basically disables all touch event for the webview. Tryed that?

Edit 2 reedit: Try to set the visibility to gone for the the top view below your listview.

like image 73
Warpzit Avatar answered Oct 08 '22 23:10

Warpzit