Android Alert Modal Popup window using Activity

The following steps will get you a basic set up for a Alert Window, or a Modal or a Popup window which ever you call it, but it’s a screen that shows on top of the previous activity.

1. This is a layout file in the drawable folder, optional but it will make the modal’s corner round. dialog_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#ffffff" />
    <stroke
        android:width="2dp"
        android:color="@color/floating_button" />
    <corners android:radius="20dp"/>
</shape>

2. Define the layout file for the activity class which will be used for the popup window. Feel free to modify it accordingly to your needs, it’s the same as any other layout file you would define for an Activity. activity_about.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/dialog_shape"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:text="@string/about"
        android:textStyle="bold"
        android:gravity="center"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_title"
        android:layout_margin="20dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/btn_no"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:clickable="true"
            android:text="No"
            android:textColor="#f25a55"
            android:textStyle="bold" />
        <Button
            android:id="@+id/btn_yes"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:text="Yes"
            android:textColor="#35e664"
            android:textStyle="bold" />
    </LinearLayout>

</RelativeLayout>

3. Create the Activity class. AboutActivity.java

public class AboutActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }
}

4. Register the Activity class in the manifest file with the configurations for using the theme Theme.AppCompat.Dialog.Alert to make the activity as a Dialog.

<activity android:name=".views.AboutActivity"
    android:screenOrientation="portrait"
    android:theme="@style/Theme.AppCompat.Dialog.Alert"
    android:parentActivityName=".views.main.MainActivity"/>

Search within Codexpedia

Custom Search

Search the entire web

Custom Search