简体   繁体   中英

Android dialog activity remove margin and finish when touched outside

I have a dialog themed activity declared like this:

AndroidManifest:

<activity android:name=".DialogActivity" android:theme="@style/mydialog"></activity>

Style:

<style name="mydialog" parent="@style/Theme.AppCompat.Light.Dialog">
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

OnCreate of DialogActivity:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);

int width = (int)(getResources().getDisplayMetrics().widthPixels*0.95);
int height = (int)(getResources().getDisplayMetrics().heightPixels*0.80);

getWindow().setLayout(width, height);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...

The problem is the activity does not close when I touch inside the red rectangular area shown in this image:

在此输入图像描述

So my question is how to remove this extra space, so that the activity will finish when touched just outside its actual shape?

The activity finishes fine if I touch outside the red rectangle. Already tried this , couldn't remove the extra space.

I was searching for the same and end up with this solution:

Style.xml:

<style name="CustomTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:statusBarColor">@color/black</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowBackground">@null</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

Manifest.xml:

    <activity android:name=".CustomActivity"
        android:theme="@style/CustomTheme" // this is important
        android:launchMode="singleTask"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustResize" />

activity_custom.xml:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="#FFFFFF"
    android:theme="@style/CustomTheme" // this is important
    android:excludeFromRecents="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello"
        android:textColor="#000000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

CustomActivity.kt:

class CustomActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_custom)

        // this is important
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
    }
}

The only thing I didn't find the solution of how to close the activity by the click outside.

Will update the answer if so.

This worked for me

<style name="DialogThemeActivity" parent="@style/Theme.AppCompat.Light.Dialog">
    <item name="windowNoTitle">true</item><!-- if required you can remove title also -->
    <item name="android:windowCloseOnTouchOutside">false</item>
</style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM