Kunu
Kunu

Reputation: 5134

How to stop click event through a semi transparent background?

I am trying to set a semi-transparent view over another view when user click on a certain button. And at the same time I want my background view not to be clickable. So basically I am setting alpha as 0.9 to my foreground view.

Code

    <RelativeLayout
    android:id="@+id/rl_fg_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#231564"
    android:alpha="0.9"
    android:visibility="visible"/>

and below this view there is a listview. So when user click on a button this view will pop up but at the same time background view (i.e. ListView) also clickable at the same time.

Upvotes: 15

Views: 8273

Answers (4)

Sunny Jha
Sunny Jha

Reputation: 121

Make the foreground clickable. Add these 2 properties in the semi transparent view xml, in parent Layout:

android:clickable="true"
android:focusable="true"

Example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f2354454"
    android:clickable="true"
    android:focusable="true"
    android:orientation="vertical">

    <TextView .... />
</LinearLayout>

Upvotes: 0

useless dinosaur
useless dinosaur

Reputation: 658

Set android:clickable="true" in xml to your foreground view.

Upvotes: 35

Zeusox
Zeusox

Reputation: 8468

If somebody needs to do this with a cardView widget, then try:

android:foreground="?android:selectableItemBackground"
android:clickable="true

Upvotes: 2

Yus
Yus

Reputation: 48

Try setting the attribute android:filterTouchesWhenObscured to true. That should work.

Upvotes: 1

Related Questions