Hello, In this article we will learn about the popup window and how to implement in android. the popup window is not a new activity but its just like a dialogue.we can use popup window as various way e.g.Alert dialog box.
The package android.widget.PopupWindow; is allow to display an arbitrary view.The popup windows its a floating container that means it will appears on top of the current activity.
CREATE ANDROID PROJECT
What we need:
1) activity_main.xml
2) popupscreen.xml
3) MainActivity.java
Step 1: Write code into activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Click me" />
</RelativeLayout>
Step 2: Write code into popupscreen.xml
<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" >
<Button
android:id="@+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close" />
</LinearLayout>
Step 3: Write code into MainActivity.java
package dev.androidapplink.popupapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
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 MainActivity extends Activity {
Button btClosePup;
Button btOpenPup;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btOpenPup = (Button) findViewById(R.id.button1);
btOpenPup.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
initiatePopupWindow();
}
});
}
private PopupWindow popwindo;
private void initiatePopupWindow() {
try {
// Create instance of LayoutInflater
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popupscreen,
(ViewGroup) findViewById(R.id.popup_element));
popwindo = new PopupWindow(layout, 300, 400, true);
popwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
btClosePup = (Button) layout.findViewById(R.id.btn_close_popup);
btClosePup.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
}
private OnClickListener cancel_button_click_listener = new OnClickListener() {
public void onClick(View v) {
popwindo.dismiss();
}
};
}
The package android.widget.PopupWindow; is allow to display an arbitrary view.The popup windows its a floating container that means it will appears on top of the current activity.
CREATE ANDROID PROJECT
What we need:
1) activity_main.xml
2) popupscreen.xml
3) MainActivity.java
Step 1: Write code into activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Click me" />
</RelativeLayout>
Step 2: Write code into popupscreen.xml
<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" >
<Button
android:id="@+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close" />
</LinearLayout>
Step 3: Write code into MainActivity.java
package dev.androidapplink.popupapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
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 MainActivity extends Activity {
Button btClosePup;
Button btOpenPup;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btOpenPup = (Button) findViewById(R.id.button1);
btOpenPup.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
initiatePopupWindow();
}
});
}
private PopupWindow popwindo;
private void initiatePopupWindow() {
try {
// Create instance of LayoutInflater
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popupscreen,
(ViewGroup) findViewById(R.id.popup_element));
popwindo = new PopupWindow(layout, 300, 400, true);
popwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
btClosePup = (Button) layout.findViewById(R.id.btn_close_popup);
btClosePup.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
}
private OnClickListener cancel_button_click_listener = new OnClickListener() {
public void onClick(View v) {
popwindo.dismiss();
}
};
}
NOTE:No need and permission in AndroidManifest.xml
Step 4: Now Run Your Project:
No comments :
Post a Comment