Popup window is very tricky at least for beginners in android development, best example you can find the implementation in calendar module and you can get overall view if you can check the source code of calendar module week view in the class called "calendar view" and analyze methods "init" and "updateEventDetails" after searching for hours i finally found the major solution in the android calendar source code which is open source....
below is the shrinked sample snippet which worked for me of the popup window in android, write below code in keydown event or any method or tailor according to your needs and between we have to use viewinflate method for popup window
private PopupWindow Popup;
//how much time your popup window should appear
private static final int POPUP_DISMISS_DELAY = 4000
private DismissPopup mDismissPopup = new DismissPopup();
Popup = new PopupWindow(this.getViewInflate()
.inflate(R.layout.main1,null,null),0,0);
Popup.setOutsideTouchable(false);
Popup.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
//your code when touched on the event
return false;
}
});
Popup.showAtLocation(this.findViewById(R.id.main2),Gravity.BOTTOM, 20, 20);
youractivity.postDelayed(mDismissPopup, POPUP_DISMISS_DELAY);
Create a class which helps in dismissing the window automatically after the specified time
class DismissPopup implements Runnable {
public void run() {
// Protect against null-pointer exceptions
if (mPopup != null) {
mPopup.dismiss();
}
}
}
Though the popup window is simple to implement but it needs few tries to make it work according to your needs, if there is any error in above snippet or you can improve the code then suggestions are appreciated
11 comments:
Hi..
Thank you for this tutorial...
I've created a popup window..which has a button on it.. I wanna know how do I set a listeneer for this button???
I tried..
Button btn= (Button) findViewById(R.id.btnOK);
btn.setOnClickListener(someListener);
But i get the value of 'btn' as null.... :(
Can u plzzz help me wid dis??
Thank you..
Thank you. But.. i don't undertand.
what is 'R.layout.main1' and 'R.layout.main2'?
showAtLocation() is spaced contentView at strange position.
please!
débrouilles toi
wat is mpopup in d code above? is t an object of class DismissPopUp? sorry if d question s silly:-(
débrouille toi (verbe du premier groupe, pas de S, cretin!)
@Zeba
Crucially you need a reference to the popup's layout to avoid NullPointers.
Try this...
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_example, (LinearLayout)findViewById(R.id.popup_layout));
pw = new PopupWindow(
layout,
100,
100,
true);
pw.showAtLocation(v, Gravity.CENTER, 10, 0);
TextView kill = (TextView)layout.findViewById(R.id.killer); //nulled
kill.setOnTouchListener(new PL());
return false;
}
what are all the packages we need to add????
How can I use Popupwindow class to creat an circular menu?
after popupwindow showing ,
when i pressed back button ,
it ll stay their only , doesn't go back..?
How to resolve this ??
I made this out of it:
PopupWindow popup = new PopupWindow(blockview,200,100);
popup.setContentView(ShowThisInPopUp);
popup.showAtLocation(view, 0, x, y);
Thread test = new Thread(new DismissPopup(popup));
test.start();
class DismissPopup implements Runnable {
private PopupWindow mPopup;
public DismissPopup(PopupWindow mPopup){
this.mPopup = mPopup;
}
public void run() {
try {
synchronized(this){
this.wait(500L);}
} catch (InterruptedException e) {
e.printStackTrace();
}
if (mPopup != null) {
mPopup.dismiss();
}
}
Or eventually like this:
class DismissPopup implements Runnable {
private PopupWindow mPopup;
public DismissPopup(View view, String text){
mPopup = new PopupWindow(view,200,100);
TextView msglab = new TextView(view.getContext());
msglab.setText(text);
mPopup.setContentView(msglab);
mPopup.showAtLocation(view, 0, (view.getWidth()/2)-100, (view.getHeight()/2)-50);
Thread test = new Thread(this);
test.start();
}
public void run() {
System.out.println("started");
try {
synchronized(this){
this.wait(500L);}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("stopped");
if (mPopup != null) {
mPopup.dismiss();
try {
this.finalize();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Post a Comment