7

What I am trying to achieve is best explained by the scheme I made with mspaint:

enter image description here

I have tried to set FLAG_NOT_TOUCH_MODAL which by the description is supposed to be exactly what I want, but it simply does not work. My activity consumes ALL touch events, even outside its borders.

If I set FLAG_NOT_FOCUSABLE then of course the native controls under the activity are touchable, but then the activity is completely not even when touching inside its borders.

I have tried setting isFloatingWindow=true in the manifest, but it didn't seem to make any difference.

Can anyone achieve this? I would really appreciate a small demo activity that works this way so I can take it and work from there. I have tried numerous permutations for WindowManager and Intent flags and nothing seems to work exactly as I need.

Thanks in advance.

UPDATE:

I have tried your suggestion, but it still did not work as desired.

This is my activity layout xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="385dp"
android:layout_height="300dp"
android:theme="@android:style/Theme.Dialog"
tools:context="com.ui.activities.TestActivity"
android:id="@+id/testLayout"
android:visibility="visible"
android:background="@drawable/abc_ab_solid_light_holo"
android:clickable="true">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="35dp"
    android:clickable="true"
    android:enabled="true"
    android:onClick="onClick" />

And this is the Activity class:

public class TestActivity extends Activity implements View.OnClickListener {

private String TAG = TestActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    setWindowParams();
}

private void setWindowParams() {
    WindowManager.LayoutParams wlp = getWindow().getAttributes();
    wlp.dimAmount = 0;
    wlp.flags = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
    getWindow().setAttributes(wlp);
}

And unfortunately, this is the result:

Does not look like a dialog

What am I missing?

Thanks.

Nom1fan
  • 763
  • 1
  • 10
  • 23

2 Answers2

9

Set a Dialog theme on the Activity in your manifest. For example:

android:theme="@android:style/Theme.Dialog"

Then set the following Window parameters in onCreate():

public void setWindowParams() {
    WindowManager.LayoutParams wlp = getWindow().getAttributes();
    wlp.dimAmount = 0;            
    wlp.flags = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
    getWindow().setAttributes(wlp);     
}
Mike M.
  • 37,502
  • 8
  • 98
  • 92
  • 1
    Thanks a lot for the reply! I will definitely try this first chance I get and report back. – Nom1fan Nov 22 '15 at 10:10
  • I edited my question with the updates. Waiting for your suggestion, thanks! – Nom1fan Nov 23 '15 at 16:37
  • 1
    `android:theme="@android:style/Theme.Dialog"` goes in the `` tag in your manifest, not in the layout. – Mike M. Nov 23 '15 at 16:40
  • 1
    Honest mistake =] The definitions look the same everywhere and also no warning/error when I set it. Thanks now it seems to work nicely! – Nom1fan Nov 23 '15 at 16:44
  • 1
    Yeah, the way Views are initialized, anything that doesn't pertain to them is ignored, so it wouldn't crash from that. And what with custom XML namespaces and attributes being possible, your IDE isn't smart enough to know that that doesn't go there. Cool. Glad you got it working. Cheers! – Mike M. Nov 23 '15 at 16:49
  • 1
    Oh, I just realized, you probably don't need the `FLAG_LAYOUT_NO_LIMITS`, unless you want your Activity to be able to extend off-screen. I copied that code from one of my projects, and neglected to remove that flag. It won't do any harm, though, if you leave it. – Mike M. Nov 23 '15 at 16:54
  • Ok got it. Thanks a lot for your help! – Nom1fan Nov 23 '15 at 17:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95995/discussion-between-nom1fan-and-mike-m). – Nom1fan Nov 24 '15 at 06:52
0

You can use activity with special theme in your AndroidManifest file:

<style name="Theme.Transparent">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowContentOverlay">@null</item>
        <!--<item name="android:backgroundDimEnabled">false</item>--> // show/hide background 
        <item name="android:windowIsFloating">true</item>
</style>

And also don't forget to set mach_parent attribute in Activity like:

override fun onStart() {
        super.onStart()
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
    }
Djek-Grif
  • 1,231
  • 12
  • 17