Popup Window Is Not Work In My Activity
I have implemented Popup Window in one Application. I have sample code of This. If I implement this code in my activity than it's not Working. so i cant found my Error. can any one
Solution 1:
I have use your code. But it seems ok.
Full code snippet of Activity
publicclassTestPopupActivityextendsActivityimplementsOnClickListener {
//The "x" and "y" position of the "Show Button" on screen.
Point p;
private LinearLayout holder;
private LayoutInflater inflater;
private View myView;
private Button btn_show;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
holder = (LinearLayout) findViewById(R.id.holder);
inflater = (LayoutInflater) getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
myView = inflater.inflate(R.layout.inflated_layout, null);
btn_show = (Button) myView.findViewById(R.id.show_popup);
btn_show.setOnClickListener(this);
holder.addView(myView);
}
// Get the x and y position after the button is draw on screen// (It's important to note that we can't get the position in the onCreate(),// because at that stage most probably the view isn't drawn yet, so it will return (0, 0))@OverridepublicvoidonWindowFocusChanged(boolean hasFocus) {
int[] location = newint[2];
/* Button button = (Button) myView.findViewById(R.id.show_popup);*/// Get the x, y location and store it in the location[] array// location[0] = x, location[1] = y.
btn_show.getLocationOnScreen(location);
//Initialize the Point with x, and y positions
p = newPoint();
p.x = location[0];
p.y = location[1];
}
// The method that displays the popup.privatevoidshowPopup(Activity context, Point p) {
intpopupWidth=200;
intpopupHeight=150;
// Inflate the popup_layout.xmlLinearLayoutviewGroup= (LinearLayout) context.findViewById(R.id.popup);
LayoutInflaterlayoutInflater= (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Viewlayout= layoutInflater.inflate(R.layout.popup_layout, viewGroup);
// Creating the PopupWindowfinalPopupWindowpopup=newPopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.intOFFSET_X=30;
intOFFSET_Y=30;
// Clear the default translucent background
popup.setBackgroundDrawable(newBitmapDrawable());
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
// Getting a reference to Close button, and close the popup when clicked.Buttonclose= (Button) layout.findViewById(R.id.close);
close.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
popup.dismiss();
}
});
}
@OverridepublicvoidonClick(View v) {
// TODO Auto-generated method stubswitch (v.getId()) {
case R.id.show_popup:
if (p != null)
showPopup(TestPopupActivity.this, p);
break;
default:
break;
}
}
}
Screenshot
Post a Comment for "Popup Window Is Not Work In My Activity"