Android Popup Window

                       A popup window is used to show a window which is pop up while a used selected or clicked on a particular components like button or a list.In this tutorial we are going to learn how pop up window is used in android.

 

STEP BY STEP

1.Create a Android Project

Refer:-How to create an android project

2.Add a button to the design layout

In this demo we have a button,when we click on the button a pop up window will show the  message.So we need to add a button into the layout.

Refer:Android UI Form Widget

The main.xml code is shown bellow

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Androidituts" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" />

</LinearLayout>

3.Create popup window layout design

Now we need to create a new .xml file to create popup window design layout

Open res—>layout—>(Right Click)—>new—>Android XML File

The code is  shown bellow

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#444444"
android:orientation="vertical"
android:padding="10sp" >

<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="65px"
android:layout_marginLeft="90px"
android:text="Learn from ANDROIDITUTS!" />

<Button
android:id="@+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15px"
android:layout_gravity="center"
android:text="Close" />
</LinearLayout>

4.Add popup functionality in Activity class file

The code is shown bellow

package com.androidituts.popup;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;

public class PopupActivity extends Activity {
Button btnClosePopup;
Button btnCreatePopup;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnCreatePopup = (Button) findViewById(R.id.button1);
btnCreatePopup.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
initiatePopupWindow();
}
});

}

private PopupWindow pwindo;

private void initiatePopupWindow() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) PopupActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup,(ViewGroup)

findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, 350, 350, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);

} catch (Exception e) {
e.printStackTrace();
}
}

private OnClickListener cancel_button_click_listener = new OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();

}
};

}

5.Run the application

 

6.Output is shown bellow.

popup1

 

popup2