开发者

How to show an Activity as pop-up on other Activity?

I have an Activity A, and there is a button B in the view. If somebody presses B then I want a pop-up which can take some part of screen making th开发者_运维技巧e A invisible in that area but rest of A is visible but not active. How can I achieve this?


If you want to do this using an Activity instead of a Dialog, you can do this by setting the activity's theme to android:theme="@android:style/Theme.Dialog" in the manifest - this will make the activity appear like a dialog (floating on top of whatever was underneath it).


For AppCompat, add

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

to the activity in AndroidManifest


The Dialog class is perfect to do that. you can find easy examples here.


Just to add on oli's answer, make sure to use the Dialog from the theme you are using in your application.

In my case I did android:theme="@android:style/Theme.Holo.Light.Dialog"


For appcompat this can be used in the manifest

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


Setting theme to android:theme="@android:style/android:Theme.Holo.Panel" worked for me.

Steps - 1. Set theme for the activity in manifest file to android:theme="@android:style/android:Theme.Holo.Panel" (This has to be changed to whatever theme is being used). Ex:

<activity
      android:name=".EditActivity"
      android:theme="@android:style/android:Theme.Holo.Panel"
      android:label="@string/title_activity_edit" >
</activity>
  1. In the activity resource xml set appropriate padding and width on the root layout. I have set it to 0 and added a child layout at the beginning with alpha to show some part of the previous activity.


if you are working with Material Design you should use @android:style/Theme.Material.Dialog.NoActionBar


You can do it programicly

Create Class MyDialog

    import android.app.Activity;
    import android.app.Dialog;
    import android.view.Window;
    import android.widget.TextView;

    public class MyDialoge{
        Activity activity;
        TextView txt_Message;
        Dialog dialog;
        public ViewDialog(Activity activity) {

            this.activity = activity;

        }

       public void showDialog(String message){
        dialog = new Dialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.custom_progress_dialog);


         txt_Message = dialog.findViewById(R.id.txt_message);
         txt_Message.setText(message);


       //if you want to dimiss the dialog
       //dialog.dimiss()


        dialog.show();

    }
        public void dimiss(){

            try {
                dialog.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }


        }



    }

After that crate the layout -> call it my_dialog

<?xml version="1.0" encoding="utf-8"?> 
      <RelativeLayout 
       xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_gravity="center"

        >

       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_centerInParent="true"
           android:text="Hello PopUp Message"/>


    </RelativeLayout>

In your Activity

MyDialog myDialog = new MyDialog(MainActivity.this);

        myDialog.showDialog("Say Hello to Me");

To dimiss

 myDialog.dimiss();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜