Android Popup Window example source code exact

Android show popup window as menu for your application

Popup window is useful when you want some option for particular UI or there need some settings you can use new popup window UI. Below is complete source code for android pop up window.

Android popup window MainActivity source code. Just copy and paste this code into your
MainActivity. You can also use floating pop up window which you can move in display.

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

public class MainActivit extends AppCompatActivity {

    Button exit_popupWin;
    Button showWin;

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

        showWin = (Button) findViewById(R.id.showPopUp);
        showWin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showPopupWin();
            }
        });
    }
//Java code to show popup window.
    private PopupWindow popWin;
    private void showPopupWin() {
        try {
            LayoutInflater inflater = (LayoutInflater) MainActivit.this getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.popup_window, (ViewGroup) findViewById(R.id.popup));

            popWin = new PopupWindow(layout, 290, 360, true);
            popWin.showAtLocation(layout, Gravity.CENTER, 0, 0);
            exit_popupWin = (Button) layout.findViewById(R.id.exit_popup);
            exit_popupWin.setOnClickListener(cancel_button);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    private View.OnClickListener cancel_button = new View.OnClickListener() {
        public void onClick(View v) {
            popWin.dismiss();
        }
    };
}

MainActivity source code popup window activity_main.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="match_parent"
 android:background="#ff929c"
 >

 <TextView
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:textColor="#f53241"
 android:textSize="31sp"
 android:textStyle="bold"
 android:text="POPUP WINDOW EXAMPLE"
 android:layout_alignParentTop="true"
 android:layout_alignParentStart="true"
 android:layout_marginTop="61dp" />

 <Button
 android:id="@+id/showPopUp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="open Popup"
 android:textSize="25sp"
 android:textColor="#f53241"
 android:textStyle="bold"
 android:layout_alignParentBottom="true"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="221dp" />

</RelativeLayout>

popup window source code

below xml code is for pop up window layout. Get more latest android code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#7b7777"
    android:orientation="vertical"
    android:padding="12sp"
    >

    <TextView
        android:id="@+id/txtView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5sp"
        android:text="Hello!" />

    <Button
        android:id="@+id/exit_popup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Close PopUp"/>

</LinearLayout>

Add a Comment

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