Create /res/layout/popup.xml to define the view of the PopupWindow.
<?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"
android:background="@android:color/background_light">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="1dp"
android:background="@android:color/darker_gray">
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="20dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="It's a PopupWindow" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/dismiss"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dismiss" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Main activity Java code to handle the PopupWindow
package com.exercise.AndroidPopupWindow;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
public class AndroidPopupWindowActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button btnOpenPopup = (Button)findViewById(R.id.openpopup);
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
}});
}
}
main.xml
<?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/openpopup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open Popup Window" />
</LinearLayout>
updated@2015-01-30, with demo video:
Next:
- Disable outside PopupWindow, by setFocusable(true)
Related:
- PopupMenu, for Android 3.0(API Level 11)
More examples of using PopupWindow:
- Disable outside PopupWindow, by setFocusable(true)
- Display Spinner inside PopupWindow
- PopupWindow with transparent background
- Create background of Popup Window with custom shape
- Implement drag-and-drop movable PopupWindow
- Interactive resizable PopupWindow
- Popup Window with dynamic content
- PopupWindow with AnalogClock and DigitalClock
what does the
ReplyDeletepopupWindow.showAsDropDown(rredButton, 50, -30);
code do?
What is 50 and -30?
it's the x, y offset of the popupWindow. Refer Here
ReplyDeleteOk. i used that in my code and understood how it works ... BUT, i want my pop-up window to be in the CENTER of the screen and not tagged to my call Button. Is that possible?
ReplyDeleteRight now, if i have 4 buttons on screen, every pop-up window shows up in different places in the screen depending on the position of the button that calls it. I want ALL pop-up windows to be shown only in the center of the screen.
Any help is appreciated.
I think you can use Dialog
ReplyDeletehi there, is it possible to place a fragment in a popupWindow?
ReplyDeletepopupWindow.showAtLocation(anyViewOnlyNeededForWindowToken, Gravity.CENTER, 0, 0);
ReplyDeleteto make the window center.
How to add animation with pop up window ..??
ReplyDeleteHello, I have a problem with android Spinner when I put it in popup.xml( but only in DropDown mode) I get the folowing exception :
ReplyDeleteandroid.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@42026de8 is not valid; is your activity running?
any help will be apreciated, thanks in advance.
hello JROUNDI Mohamed,
ReplyDeleteI can do it in android:spinnerMode="dialog" only. refer: Display Spinner inside PopupWindow.
Great article. Thank you for sharing it.
ReplyDeletePopupWindow is great alternative to embedding the view into layout and switching its visibility between GONE and VISIBLE.
how to set background transparent in popup window
ReplyDeleteTry this..
ReplyDeletedialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
hello Murugeswari Marimuthu,
ReplyDeleteplease read PopupWindow with transparent background
Sorry I didn't mean the popup background. Actually i need to make the page elements besides the popup semi transparent like light box.
ReplyDeletehello Murugeswari Marimuthu,
ReplyDeletedo you means the main layout? you can also apply android:background="@android:color/transparent" on your layout, depends how you arrange your elements.
Sorry,I tried your code but still not working.
ReplyDeleteIn android,How to Insert Auto complete text view within creating popup
ReplyDeleteHello, I tried this and its working for me . but i want to know how to add text dynamically to the text view instead of adding text using xml.
ReplyDeleteTo update TextView dynamically, please read Popup Window with dynamic content.
ReplyDeletehello sir, its not my problem,my problem how to integrate autocomplete textview function inside popup window. Its Possible??
ReplyDeletehello Murugeswari Marimuthu,
ReplyDeletejust tried to implement AutoCompleteTextView on PopupWindow, but fail!
yes eric how to fix this.
ReplyDeleteI FAIL to implement AutoCompleteTextView on PopupWindow!
ReplyDeleteError of "android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@4205c160 is not valid; is your activity running?" when the AutoCompleteTextView try to display the dropdown list.
Hi Eric how to listview onitem click listener in json android
ReplyDeletewhat you means "json android"?
ReplyDeleteSorry Eric, Json Parsing
ReplyDeleteHi velmurugan murugesan, How to fix this? briefbrief explanation please.
ReplyDeleteHi,
ReplyDeletei have been trying this but not working.
when i am trying to inflate the layout my popup.xml is not recognised and it brings error that i should field popup or contant popup...
What should I do?
Help!
Great tutorial. It was easy to follow and gives you the basics so you having something to build upon.
ReplyDeleteGreat tutorial. Gives you the basics so you have something to build upon. Exactly what I was looking for.
ReplyDeletehow to use edittext replace of textView with keyboard to be edit ! ??
ReplyDeleteThanks author, i search lot for creating text popup box for my app.
ReplyDeletefinally i found the solution via your post.
How to add animation to popup window?
ReplyDeleteHi you.You are used to program androi. I use androi studio version new. I dont open it and build.
ReplyDeleteLet's help me !
Thanks
Hi,
ReplyDeleteused this and it works nice! anyway, if i use this popupwindow inside more then 4 activity, my app start crashing. What could causing this?
Hey! Great tutorial... Thank you very much.
ReplyDeleteI have a question:
I'm develop an app which has a nav drawer on the left. It's happening that if i open the drawer, the popup shows in front of the menu list. Instead of that, i want it on the back of the drawer. Is it possible?
I have been trying so many options and i still have not solve the problem...
Thank you! Very Helpfull!
ReplyDeleteHello guys, please help me to set a background color when popup window is active and when dismissed, return default color of an activity. I have followed this tutorial, everything works fine. Only need to change background of an activity.
ReplyDelete