0

Recently I had to create a transparent activity with a toolbar where the user will type content to be queried. The rest of the activity should be transparent and allow user interactions. The I've found so for for doing that is:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);  

As indicated in this question: link. I've also tried the solution

getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);

As said here: link

The problem with the first link is that the top activity (which contains the toolbar) no longer receives click events. The problem with the second one is that the bottom activity does not receives the click event. How can I make top activity receive click events and pass it to the bottom activity when click is made in the transparent activity?

This is my top activity layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/root_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:clickable="true"
            android:elevation="2dp"
            android:minHeight="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dip"
                android:backgroundTint="#FFF"
                android:hint="@string/act_search_toolbar_hint"
                android:textColor="#757575"
                android:textColorHint="#757575"/>

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="#FFF"/>

</LinearLayout>

How it looks like:

enter image description here

Community
  • 1
  • 1
E. Fernandes
  • 3,889
  • 4
  • 30
  • 48
  • What you want is not supported, AFAIK. The foreground activity gets input events; the activity viewable through the transparent regions is not in the foreground. – CommonsWare Feb 05 '16 at 22:44
  • Using this snippet getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); in the foreground activity I'm able to pass the click throug, but it is not able to handle the events in both activities – E. Fernandes Feb 05 '16 at 22:50
  • Correct. That is by design. – CommonsWare Feb 05 '16 at 23:03

2 Answers2

4

You can't do this with a transparent activity. WHen you put another activity up, the original is paused and is non-interactive. You want to do this with a fragment instead.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • 1
    Note that the fragment approach assumes that both activities are part of the same app, and therefore could possibly be combined into one activity. That doesn't hold true if the objective is to allow input events to pass through to another app's activity, that the foreground activity happens to be on top of. – CommonsWare Feb 05 '16 at 22:46
  • I've only used fragments in the context of viewpagers till now, could you point some link where I can check how to follow this approach or provide some code please? – E. Fernandes Feb 05 '16 at 22:49
0

Handle touch events in top activity and pass it to second activity using some method-something like backgroundActivity.handleEvent(event).

rupesh jain
  • 3,410
  • 1
  • 14
  • 22
  • If I do that I will loose some important UI features as the ripple effect for instance – E. Fernandes Feb 05 '16 at 23:05
  • No not necessarily.You use the native android views too inside a custom view group. You pass event to the buttons and they will react the same way. – rupesh jain Feb 05 '16 at 23:11