Friday 27 September 2013

POPUP window in android

How to Display Popup Window in Android?


Friends, Lets learn how to display a popup window in android.
Popup is different from Dialog Box. Dialog has the priority to display in only center of the screen but popup can be displayed anywhere in the screen.

Now let's start with the
1.MainActivity.java

package com.sri.popup;

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

public class MainActivity extends Activity {
private Button btnShowPopUp;
private PopupWindow mpopup;

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

btnShowPopUp = (Button) findViewById(R.id.btnShowPopUp);

btnShowPopUp.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
View popUpView = getLayoutInflater().inflate(R.layout.popup,
null); // inflating popup layout
mpopup = new PopupWindow(popUpView, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT, true); // Creation of popup
mpopup.setAnimationStyle(android.R.style.Animation_Dialog);
mpopup.showAtLocation(popUpView, Gravity.BOTTOM, 20, 20); // Displaying
// popup

Button btnOk = (Button) popUpView.findViewById(R.id.btnOk);
btnOk.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mpopup.dismiss(); // dismissing the popup
}
});

Button btnCancel = (Button) popUpView
.findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mpopup.dismiss(); // dismissing the popup
}
});
}
});
}
}

2.activity_main.xml

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

    <Button
        android:id="@+id/btnShowPopUp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show PopUp" >
    </Button>

</LinearLayout>



3.Add the new xml file in the res/layout folder as popup.xml
popup.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#90FFFFFF"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="PopUp Window"
        android:textColor="#000000"
        android:textStyle="bold" >
    </TextView>

    <Button
        android:id="@+id/btnOk"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Ok" >
    </Button>

    <Button
        android:id="@+id/btnCancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Cancel" >
    </Button>

</LinearLayout>

4.That's it now you can easily display popup window in your application.

The full source code is here

No comments:

Post a Comment