Like Us on Facebook

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:
 
  • JDK 6.0 or above
  • Android Studio



Steps to follow:  
 
  1. Create a New Android Studio project name PopupDemo.
  2. Select minimum  API level 15 so it will run Maximum of Android device that are Active on google Play then click next.
  3. Select Blank Activity and your Activity name is PopUpDemo click next > click finish.
  4. 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();
}
};
}


 
 5. main.xml file
 
<?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>



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>
 

Posted By UandBlog

UandBlog is a Global Leading source of Finance, Health, Lifestyle, Technology, Gaming and Programing and other information, it is a fastest growing blog for all , for latest post or information like us on facebook , follow on Twitter, Google+ and Pinterest.

Comments ( 1 )

Prince Dube:
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?

    Leave a comment..

    Click to login.

    Subscribe Latest Information

    Most Popular Posts

    You May Like Also