activity as dialog in android

2019-03-11 07:41发布

I want to use an activity as dialog and i made the theme of the activity as dialog & i succeed. but i have the problem here is when i click outside of the activity its automatically get closed & the previous activity get started..

i tried a thing to make transparent parent layout it does not look like a dialog..

i want to use this activity to create new account in my application as it has only 3 fields so in tablet it looks large space unused... so i want to use activity as dialog.....

thenx in advance...!!! examples will be appreciated..!!!!!

6条回答
欢心
2楼-- · 2019-03-11 08:01

Using Same Activity in Mobile devices and Tablets.

Mobile :-

  1. Goto res -> values.
  2. Open styles.xml and add following theme settings.

styles.xml :-

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

Tablet :- Launch activity as dialog.

  1. Goto "res".
  2. Create new folder "values-sw720dp".
  3. Create new styles.xml and add following theme settings.

styles.xml :-

<style name="AppTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">@color/primary_color</item>
    <item name="colorPrimaryDark">@color/primary_dark_color</item>
    <item name="android:textColorPrimary">@color/white</item>
    <item name="colorAccent">@color/material_green_800</item>
    <item name="colorButtonNormal">@color/material_green_800</item>
    <item name="windowFixedHeightMajor">800dp</item>
    <item name="windowFixedHeightMinor">800dp</item>
</style>

Manifest.xml :-

    <activity
    android:name=".LogInActivity"
    android:label="@string/title_activity_log_in"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme"            //set the theme activity
    android:windowSoftInputMode="adjustPan" >
    </activity>
查看更多
一纸荒年 Trace。
3楼-- · 2019-03-11 08:02

For the issue of avoiding closing the activity when clicking outside window from API 11 as mentioned by Vivek use this.setFinishOnTouchOutside(false);

but for prior APIs use this code:

@Override
public boolean onTouchEvent(MotionEvent event) {
    if ( event.getAction() == MotionEvent.ACTION_DOWN &&  isOutOfBounds(this, event)){
        return true;
    }
    return super.onTouchEvent(event);
}

private boolean isOutOfBounds(Activity context, MotionEvent event) {
    final int x = (int) event.getX();
    final int y = (int) event.getY();
    final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
    final View decorView = context.getWindow().getDecorView();
    return (x < -slop) || (y < -slop)
            || (x > (decorView.getWidth()+slop))
            || (y > (decorView.getHeight()+slop));
}
查看更多
Explosion°爆炸
4楼-- · 2019-03-11 08:05

Make change in code as per your need.

Thanks

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/transparent"
        android:orientation="vertical"
        android:paddingBottom="20dp" >
        <RelativeLayout
            android:id="@+id/RlayMain"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="30dp"
            android:layout_marginTop="120dp"
            android:background="#FFFFFF"
            android:padding="10dp" >
            <TextView
                android:id="@+id/txtsignin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:text="SIGN IN"
                android:textColor="#000000"
                android:textSize="25sp" />
            <EditText
                android:id="@+id/edtUserName"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/txtsignin"
                android:layout_marginTop="10dp"
                android:layout_toRightOf="@+id/txtuser"
                android:hint="USERNAME" />
            <EditText
                android:id="@+id/edtPassword"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/edtUserName"
                android:layout_marginTop="10dp"
                android:hint="PASSWORD"
                android:inputType="textPassword" />
            <Button
                android:id="@+id/btnSignIn"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/edtPassword"
                android:layout_marginTop="10dp"
                android:paddingBottom="10dp"
                android:paddingTop="10dp"
                android:text="Sign In" >
            </Button>
            <Button
                android:id="@+id/btnSignUp"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/btnSignIn"
                android:layout_marginTop="10dp"
                android:paddingBottom="10dp"
                android:paddingTop="10dp"
                android:text="Sign Up For Free!" >
            </Button>
        </RelativeLayout>
    </RelativeLayout>
查看更多
Luminary・发光体
5楼-- · 2019-03-11 08:06

if you haven't already tried it, then this is the way to achieve activity as dialog: in your manifest file, add to your activity the following attribute:

     <activity
        android:name=".MyActivityName"
        android:theme="@android:style/Theme.Dialog" />
查看更多
我想做一个坏孩纸
6楼-- · 2019-03-11 08:17

to Start activity as dialog I defined:

<activity android:theme="@android:style/Theme.Dialog" />

Now when I startActivity() it displays like a dialog and parent activity displays behind it. I want a Button which when clicked Dialog gets dismissed and parent activity should display without refreshing the page.

Create an activity as we usually create it.

Also check CustomDialogActivity.java on android.com

I think you should create activity as a dialog, it helps.

This way, you can set style and theme for your activity.

查看更多
我想做一个坏孩纸
7楼-- · 2019-03-11 08:19

try with following property

    this.setFinishOnTouchOutside(false);
查看更多
登录 后发表回答