Skip to content Skip to sidebar Skip to footer

How Do I Create A Popup Overlay View In An Activity Without Fragment?

I'd like to show a popup onto my Activity when a button is pressed. I was inspired by this question. So I use the 'merge' control in the content xml of the activity and put in it t

Solution 1:

Ok so this might seem like a lot of code, but it's really easy.

First, you will create the dialog layout you want in XML. (not in the same XML as the activity view) Here's an example.

custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="52dp"android:text="New Text"android:id="@+id/txtTitle" /><ListViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/listView"android:layout_weight="1" /><LinearLayoutandroid:orientation="horizontal"android:layout_width="match_parent"android:layout_height="52dp"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="New Button"android:id="@+id/btnBtmLeft"android:layout_weight="1" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="New Button"android:id="@+id/btnBtmRight"android:layout_weight="1" /></LinearLayout></LinearLayout>

Then, in your Activity do the following:

privatevoidshowMyDialog(Context context) {
    finalDialogdialog=newDialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.custom_dialog);
    dialog.setCanceledOnTouchOutside(false);
    dialog.setCancelable(true);

    TextViewtextView= (TextView) dialog.findViewById(R.id.txtTitle);
    ListViewlistView= (ListView) dialog.findViewById(R.id.listView);
    ButtonbtnBtmLeft= (Button) dialog.findViewById(R.id.btnBtmLeft);
    ButtonbtnBtmRight= (Button) dialog.findViewById(R.id.btnBtmRight);

    btnBtmLeft.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
            dialog.dismiss();
        }
    });

    btnBtmRight.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
            // do whatever you want here
        }
    });

    /**
     * if you want the dialog to be specific size, do the following
     * this will cover 85% of the screen (85% width and 85% height)
     */DisplayMetricsdisplayMetrics= context.getResources().getDisplayMetrics();
    intdialogWidth= (int)(displayMetrics.widthPixels * 0.85);
    intdialogHeight= (int)(displayMetrics.heightPixels * 0.85);
    dialog.getWindow().setLayout(dialogWidth, dialogHeight);

    dialog.show();
}

And finally, in onCreate of your activity, call that method

myButton.setOnClickListener(newView.OnClickListener() {
    @OverridepublicvoidonClick(View v) {
        showMyDialog(context);
    }
});

Hope this helps!

Post a Comment for "How Do I Create A Popup Overlay View In An Activity Without Fragment?"