How to Make a PopUp Window in Android
Hello everyone , as per specific demand I am going to share How to Make a Popup window in Android , it give you basic oncept how to make a Popup window in Android . Later in upcomming post I will explain how to Customize Popup window in your Android Application.
Prerequisites:
Steps to follow:
Prerequisites:
- JDK 6.0 or above
- Android Studio
Steps to follow:
- Create a New Android Studio project name PopupDemo.
- Select minimum API level 15 so it will run Maximum of Android device that are Active on google Play then click next.
- Select Blank Activity and your Activity name is PopUpDemo click next > click finish.
- Inside PopupDemo Class write the following code;
public class PopUpDemo extends Activity {
Button Close;
Button Create;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Create = (Button) findViewById(R.id.button1);
Create.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showPopup();
}
});
}
private PopupWindow pw;
private void showPopup() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) PopUpDemo.this
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup,
(ViewGroup) findViewById(R.id.popup_1));
pw = new PopupWindow(layout, 300, 370, true);
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
Close = (Button) layout.findViewById(R.id.close_popup);
Close.setOnClickListener(cancel_button);
} catch (Exception e) {
e.printStackTrace();
}
}
private OnClickListener cancel_button = new OnClickListener() {
public void onClick(View v) {
pw.dismiss();
}
};
}
Button Close;
Button Create;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Create = (Button) findViewById(R.id.button1);
Create.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showPopup();
}
});
}
private PopupWindow pw;
private void showPopup() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) PopUpDemo.this
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup,
(ViewGroup) findViewById(R.id.popup_1));
pw = new PopupWindow(layout, 300, 370, true);
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
Close = (Button) layout.findViewById(R.id.close_popup);
Close.setOnClickListener(cancel_button);
} catch (Exception e) {
e.printStackTrace();
}
}
private OnClickListener cancel_button = new OnClickListener() {
public void onClick(View v) {
pw.dismiss();
}
};
}
<?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="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<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="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
6. popup.xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_1"
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="5sp"
android:text="Hello!" />
<Button
android:id="@+id/close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close"/>
</LinearLayout>
android:id="@+id/popup_1"
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="5sp"
android:text="Hello!" />
<Button
android:id="@+id/close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close"/>
</LinearLayout>

Comments ( 1 )

Prince Dube:
error try without catch finally or resource declarations. how do i solve this. email me at princedube02@gmail.com
error try without catch finally or resource declarations. how do i solve this. email me at princedube02@gmail.com

Admin:
can u show me the snapsot?
can u show me the snapsot?
Subscribe Latest Information
Categories
Most Popular Posts
How to Withdraw Money from ATM Machine 7steps 1178778 Views
How to Create Chat Application in Android Studio 151633 Views
How to Create a Shopping Cart Application in Android 114496 Views
You May Like Also
Android Material Design Snackbar tutorial 4847 Views
How to Use GoogleMap API v3 in Android 10104 Views
How to Create Overflow Menus in Android 14115 Views