Using Popup Window in Android

By | April 2, 2017

The popup windows is a floating container that appears on top of the current activity.

Android Popup Window

Layout for PopUp

Below is the layout for the popup window.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_blue_bright"
    android:orientation="vertical"
    android:padding="20dp">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="This is a PopupWindow" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:src="@drawable/android" />

    <Button
        android:id="@+id/dismiss"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Dismiss" />
</LinearLayout>

Layout for Activity

Create another layout for the activity activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.coderzheaven.popupdemo.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv1"
        android:text="Pop Up Window Demo in Android" />

    <Button
        android:padding="20dp"
        android:layout_below="@+id/tv1"
        android:id="@+id/open_popup"
        android:layout_width="match_parent"
        android:text="Open PopUp"
        android:layout_height="wrap_content" />
</RelativeLayout>

Implementation

The activity which implements the popup.

package com.coderzheaven.popupdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.view.ViewGroup.LayoutParams;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button btnOpenPopup;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnOpenPopup = (Button) findViewById(R.id.open_popup);
        btnOpenPopup.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        LayoutInflater layoutInflater
                = (LayoutInflater) getBaseContext()
                .getSystemService(LAYOUT_INFLATER_SERVICE);
        View popupView = layoutInflater.inflate(R.layout.popup_window, null);
        final PopupWindow popupWindow = new PopupWindow(
                popupView,
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);

        Button btnDismiss = (Button) popupView.findViewById(R.id.dismiss);
        btnDismiss.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        popupWindow.showAsDropDown(btnOpenPopup, 20, 90);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *