43

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 the A invisible in that area but rest of A is visible but not active. How can I achieve this?

bhups
  • 14,345
  • 8
  • 49
  • 57

8 Answers8

73

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).

oli
  • 4,894
  • 1
  • 17
  • 11
24

For AppCompat, add

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

to the activity in AndroidManifest

Actiwitty
  • 1,232
  • 2
  • 12
  • 20
6

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

Sephy
  • 50,022
  • 30
  • 123
  • 131
3

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"

Caio Faustino
  • 165
  • 1
  • 15
3

For appcompat this can be used in the manifest

<activity android:theme="@style/Theme.Base.AppCompat.Dialog.FixedSize" >
</activity>
Arslan Mehboob
  • 1,012
  • 1
  • 9
  • 21
1

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.
Vikram Rao
  • 514
  • 3
  • 16
0

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

Mirza Asad
  • 606
  • 1
  • 7
  • 18
0

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();
Abanoub Hany
  • 557
  • 4
  • 7