Aligning popup window near the button clicked - android

I have a custom popup window by a layout. I have to give a x,y coordinates to appear popup window after a_btn click. This can be different locations in different phones.
But I want to show the popup window always above and touching the the a_btn
How can I implement this.Help me
My code for showing the popup window :
a_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater lInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popup_view = lInflater.inflate(R.layout.popup_a, null);
final PopupWindow popup = new PopupWindow(popup_view,FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT,true);
popup.setFocusable(true);
popup.setBackgroundDrawable(new ColorDrawable());
popup.showAtLocation(relative, Gravity.NO_GRAVITY, coordinateTop, 100);
//popup.showAsDropDown(location_popup_view, 2, 2);
}
});

Use this code will help you
a_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Rect r = locateView(v);
LayoutInflater lInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popup_view = lInflater.inflate(R.layout.popup_a, null);
final PopupWindow popup = new PopupWindow(popup_view,FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT,true);
popup.setFocusable(true);
popup.setBackgroundDrawable(new ColorDrawable());
popup.showAtLocation(layout, Gravity.TOP | Gravity.LEFT, r.right, r.bottom);
}
});
public static Rect locateView(View v) {
int[] loc_int = new int[2];
if (v == null)
return null;
try {
v.getLocationOnScreen(loc_int);
} catch (NullPointerException npe) {
return null;
}
Rect location = new Rect();
location.left = loc_int[0];
location.top = loc_int[1];
location.right = loc_int[0] + v.getWidth();
location.bottom = loc_int[1] + v.getHeight();
return location;
}

I used popup.showAsDropDown(a_btn,0,0);
instead of
popup.showAtLocation(relative, Gravity.NO_GRAVITY, coordinateTop, 100);
and gave xoff and yoff.
a_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater lInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popup_view = lInflater.inflate(R.layout.popup_a, null);
final PopupWindow popup = new PopupWindow(popup_view,200,200,true);
popup.setFocusable(true);
popup.setBackgroundDrawable(new ColorDrawable());
popup.showAsDropDown(a_btn,0,0);
}
});

Related

PopWindow always show at last in ListView...!

I working on android PopupWindow. In this application I create 1 custom ListView. Now I want to show PopupWindow when user click on TextView of custom ListView.
My problem is :
PopupWindow always show at last TextView even if I click on first or other TextView . How can I resolve this.??
TextView of ListView.
holder.end.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int[] location = new int[2];
holder.end.getLocationOnScreen(location);
p = new Point();
p.x = location[0];
p.y = location[1];
if (p != null){
showPopup(context,p);
holder.popupText.setText(holder.end.getText().toString());
}
}
});
My popupWindow function.
private void showPopup(final Activity context, Point p) {
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout)context.findViewById(R.id.layoutPopup);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
holder.popupText = (TextView) layout.findViewById(R.id.showPopUp);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
popup.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
popup.setFocusable(true);
int OFFSET_X = 30;
int OFFSET_Y = 30;
popup.setBackgroundDrawable(new BitmapDrawable());
popup.showAtLocation(layout.findViewById(R.id.showPopUp), Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
}
Tray changing your click listener with this code. It should work.
holder.end.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int[] location = new int[2];
v.getLocationOnScreen(location);
p = new Point();
p.x = location[0];
p.y = location[1];
if (p != null){
showPopup(context,p);
holder.popupText.setText(holder.end.getText().toString());
}
}
});

How to center PopupWindow?

I created an Activity in which i add a button that throws a popup when is clicked. Here is the code of showPopup() method:
private void showPopup() {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_element), false);
final PopupWindow pwindo = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, true);
Button btnAgree = (Button) layout.findViewById(R.id.ok);
btnAgree.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
});
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
}
I would center it both vertically and orizzontally. I tried several ways that i see here on SO but none worked. Why i always get the popup window at the top of the screen?
you can use setHorizontalOffset:
ListPopupWindow popupWindow = new ListPopupWindow(this);
// Position
popupWindow.setAnchorView(btn);
popupWindow.setPromptPosition (ListPopupWindow.POSITION_PROMPT_ABOVE);
popupWindow.setHorizontalOffset((btn.layoutParams.width - popupWindow.getWidth())/2);
this code will show perfect popup in center of you specific view
// click event of specific view
#Override
public void onClick(View v) {
super.onClick(v);
if (v == lin_filterActionbar) {
PopupWindow popupwindow_obj; // create object
popupwindow_obj = dialog_AllergiesSelect(RR_HomeActivity.this,lin_filterActionbar);
popupwindow_obj.showAsDropDown(lin_filterActionbar);
}
}
private PopupWindow dialog_AllergiesSelect(Context context,LinearLayout lin) {
final PopupWindow dialog_AllergiesSelect = new PopupWindow(this);
View v = View.inflate(context, R.layout.dialog_filter_actionbar, null);
dialog_AllergiesSelect.setContentView(v);
dialog_AllergiesSelect.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
dialog_AllergiesSelect.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
dialog_AllergiesSelect.setFocusable(true);
// TextView lis_testSelection = (TextView) v.findViewById(R.id.dialogfilter_txt_filter);
// LinearLayout.LayoutParams lin_laLayoutParams = new LinearLayout.LayoutParams((int) (sunApplication.getDeviceWidth() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_spinner_width)), (int) (sunApplication.getDeviceHeight() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_height)));
// lis_testSelection.setLayoutParams(lin_laLayoutParams);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 0;
int OFFSET_Y = 0;
// Clear the default translucent background
dialog_AllergiesSelect.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
//dialog_AllergiesSelect.showAtLocation(v, Gravity.C, p.x + OFFSET_X, p.y + OFFSET_Y);
Rect location = locateView(lin);
dialog_AllergiesSelect.showAtLocation(v, Gravity.TOP|Gravity.CENTER, 0, location.bottom);
// Getting a reference to Close button, and close the popup when clicked.
return dialog_AllergiesSelect;
}
public static Rect locateView(View v)
{
int[] loc_int = new int[2];
if (v == null) return null;
try
{
v.getLocationOnScreen(loc_int);
} catch (NullPointerException npe)
{
//Happens when the view doesn't exist on screen anymore.
return null;
}
Rect location = new Rect();
location.left = loc_int[0];
location.top = loc_int[1];
location.right = location.left + v.getWidth();
location.bottom = location.top + v.getHeight();
return location;
}

Popup showAtLocation is not working

I'm using pop-window to show some text-view while clicking the edit-text . but pop-window is not showing at particular location ,it always showing at left top corner , here is my code ,what is wrong in that
private void showPopup(Context context,final LinearLayout Parent) {
LayoutInflater layoutInflater = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = layoutInflater.inflate(R.layout.popup,MainActivity.parent,true);
// Creating the PopupWindow
final PopupWindow popupWindow = new PopupWindow(
layout,700,700);
popupWindow.setContentView(layout);
popupWindow.setHeight(500);
new Runnable(){
#Override
public void run() {
popupWindow.showAtLocation(layout, Gravity.CENTER,300,150);
}
};
}
Try Following Code:
public static Rect locateView(View v)
{
int[] loc_int = new int[2];
if (v == null) return null;
try
{
v.getLocationOnScreen(loc_int);
} catch (NullPointerException e)
{
//Happens when the view doesn't exist on screen anymore.
return null;
}
Rect location = new Rect();
location.left = loc_int[0];
location.top = loc_int[1];
location.right = location.left + v.getWidth();
location.bottom = location.top + v.getHeight();
return location;
}
And then use
popup.showAtLocation(parent, Gravity.TOP|Gravity.LEFT, location.left, location.bottom);
Hope it helps...

OnClick Event not detected on parentView in AChartEngine

I use the following code to draw GraphView on LinearLayout,whose parent is a RelativeLayout
the code for drawing GraphView on LinearLayout
mLinearView = (LinearLayout) findViewById(R.id.LinearView);//ChildLayout
mRelativeView = (RelativeLayout) findViewById(R.id.RelativeLay);//ParentLayout
YearXYGraph mYearGraph = new YearXYGraph();
final GraphicalView mGraphView = mYearGraph.execution(
MainActivity.this, mGirlHeightYearFirstGrade,
mGirlHeightYearSecondGrade,
mGirlHeightYearThirdGrade,
mGirlHeightYearFourthGrade,
mGirlHeightYearFifthGrade, mGirlWeightYearFirstGrade,
mGirlWeightYearSecondGrade, mGirlWeightYearThirdGrade,
mGirlWeightYearFourthGrade, mGirlWeightYearFifthGrade);
mLinearView.addView(mGraphView);
onclicking LinearLayout I'm Showing a popup,code follows
mGraphView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
double[] xy = mGraphView.toRealPoint(0);
p = new Point();
p.x = (int) xy[0];
p.y = (int) xy[1];
if (p != null){
showPopup(MainActivity.this, p);
}
}
});
Im also using onClick event on ParentLayout to get some coordinate
mRelativeView.setOnTouchListener(new RelativeLayout.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("in--");
p1 = new Point();
p1.x = (int) event.getX();
p1.y = (int) event.getY();
System.out.println("x--"+p1.x);
return false;
}
});
code for Showing popup
// The method that displays the popup.
private void showPopup(final Activity context, Point p) {
int popupWidth = 75;
int popupHeight = 40;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
Rect location = new Rect();
location.left =p1.x;
location.top = p1.y;
location.right = location.left + popup.getWidth();
location.bottom = location.top + popup.getHeight();
/* // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 30;
int OFFSET_Y = 30;*/
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
// popup.showAtLocation(layout, Gravity.TOP|Gravity.LEFT, location.left , location.bottom );
popup.showAtLocation(layout, Gravity.TOP|Gravity.LEFT, location.left , location.bottom );
// Getting a reference to Close button, and close the popup when clicked.
TextView firstView = (TextView) layout.findViewById(R.id.textView1);
firstView.setText(p.x + " , " +p.y);
firstView.setTextColor(Color.WHITE);
}
problem is onClickEvent not detected on ParentLayout,,Child Layout detects OnClick,please provide some suggestions

popup dismiss android

I am implementing a popup in android. I can dismiss it when I click outside of the popup, but I want to dismiss it when I click on same imageview/button as well.
1.on click on button1 it opens pop up
2.If I click outside it closes pop up
3.If I click on button1(when pop up is open), it closes pop and reopens it again
what I want is
If I click on button1(when pop up is open), it just closes pop and donot reopen unless second click.
Is this possible?
NOTE: I donot want to have a close button inside popup window.
pop_one.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
pop_one.getLocationOnScreen(location);
p = new Point();
p.x = location[0];
p.y = location[1];
popupshow(one_one_text,pop_one);
}
});
And the method is:
public void popupshow(String pop_text, ImageView pop_one) {
int width = display.getWidth(); // deprecated
System.out.println("jsfjsfjnsdf"+width);
int new_width = width-(width/6) ;
LayoutInflater layoutInflater = (LayoutInflater) getActivity()
.getBaseContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(
R.layout.career_options_popup_short, null);
final PopupWindow popupWindow = new PopupWindow(popupView, new_width,
LayoutParams.WRAP_CONTENT);
TextView popup_text = (TextView) popupView
.findViewById(R.id.popup_text);
popup_text.setMinimumWidth(300);
popup_text.setText(pop_text);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(popupView,p.x/10, p.y +p.x/8);
System.out.println("value of x" + p.x+" "+p.y);
//System.out.println("value of new width " + new_width+" get width "+popupWindow.getWidth()+" text box width "+popup_text.getWidth()+"pop view"+popupView.getWidth());
popupWindow.setFocusable(true);
}
Regards,
Asmi
I think this will solve your problem call this on button click
popupWindow.dismiss();
You need to set the property setOutsideTouchable(false) of popup window
what you need is a boolean isShowing set this boolean to be true in the function popupshow
like this
public void popupshow(String pop_text, ImageView pop_one) {
isShowing=true;
int width = display.getWidth(); // deprecated
System.out.println("jsfjsfjnsdf"+width);
int new_width = width-(width/6) ;
LayoutInflater layoutInflater = (LayoutInflater) getActivity()
.getBaseContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(
R.layout.career_options_popup_short, null);
final PopupWindow popupWindow = new PopupWindow(popupView, new_width,
LayoutParams.WRAP_CONTENT);
TextView popup_text = (TextView) popupView
.findViewById(R.id.popup_text);
popup_text.setMinimumWidth(300);
popup_text.setText(pop_text);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(popupView,p.x/10, p.y +p.x/8);
System.out.println("value of x" + p.x+" "+p.y);
//System.out.println("value of new width " + new_width+" get width "+popupWindow.getWidth()+" text box width "+popup_text.getWidth()+"pop view"+popupView.getWidth());
popupWindow.setFocusable(true);
}
now when you click on button1 just check for this boolean
pop_one.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
if(isShowing)
{
popupWindow.dismiss();
isShowing=false;
}
else
{
pop_one.getLocationOnScreen(location);
p = new Point();
p.x = location[0];
p.y = location[1];
popupshow(one_one_text,pop_one);
}
}
});

Categories

Resources