I am new to android and i want to fix a pop up dialogue box relative to another button. I am inflating the popup window using code. How can i get current position of the button so that i can use these values for inflating the pop up window.
This is the code I use to get the current x/y coordinate of any View Object:
/**
* #return the absolute x and y coordinates of the given view
*/
public Point currentPosition(View view)
{
int[] loc = new int[2];
view.getLocationOnScreen(loc);
return new Point(loc[0], loc[1]);
}
Related
I have multiple views that are all selectable. When one of them gets selected I want my new view to be first positioned to the same location as the selected one and also move it to the top of my screen. How can I do that? The position of selected item varies.
// Selected item
int[] location = new int[2];
item.getLocationOnScreen(location);
int positionY = location[1];
// Move the view I want to move to the position of selected item
viewToMove.setTranslationY(positionY);
// Create and start animation
Animation slideUp = new TranslateAnimation(viewToMove.getTranslationX(), viewToMove.getTranslationX(), positionY, -positionY);
slideUp.setDuration(1000);
slideUp.setFillEnabled(true);
slideUp.setFillAfter(true);
viewToMove.startAnimation(slideUp);
With ViewPropertyAnimator its easy :
viewToMove.animate().translationX(...).translationY(0).setDuration(1000);
translationY(0) is the top of the Y axis, and
at translationX(...) you fill in the coordinates where you want the View to move to on the X axis.
I am using Achart Engine Line chart to display values, i am using point style CIRCLE.
I am trying to implementing the below :
1) Increase the point style width - i mean the circle should be bit bigger.
2) On click of each point i want a popup to be displayed on top of that which will display the values of X and Y.
I have done some research on this but was not able to find the solution.
I found this code but how to display the small popup with values ?
final LineChart chart = new LineChart(buildDataset(mTitles, data), mRenderer);
final GraphicalView view = new GraphicalView(mContext, chart);
view.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
double[] xy = chart.toScreenPoint(view.toRealPoint(0));
int[] location = new int[] {(int) xy[0], (int) xy[1]};
SeriesSelection seriesSelection = view.getCurrentSeriesAndPoint();
if (seriesSelection != null) {
final Data d = mModel.getDiaryAt(seriesSelection.getSeriesIndex(),
seriesSelection.getPointIndex());
//show popup at xy[0] xy[1]
}
}
});
It might have been not given in the SDK but still if anyone has tried this as was successful.
** EDIT **
Link 1 : achartengine toScreenPoint(double) always returns nullPointerException
In the image you can see a popup i want to implement like that.
You can see an example here on how to display the clicked point in your chart, using AChartEngine.
I have one image of remote on screen,i want to when i click on any particular button at that time any particular event occurs,means image have some buttons when i exact touch on button held on in this image at that time occurs any events,i how to get position this button held on only image,and screen point x and y also change in different screen. i know getX() and `getY()' used to find touch position but how to get any particular position inside the image ,please help me quickly,thanks in advance.
see in the image ,when i touch on middle of image at that time any event ocurrs.
Try this:
anchorView.getLocationOnScreen(location);
location is an array:
int location[] = new int[2];
anchorView is your view.
So your function might look like:
public getLocation(View anchorView){
int location[] = new int[2];
anchorView.getLocationOnScreen(location);
}
And if you want to display a different view at that location just do:
yourView.showAtLocation(anchorView, Gravity.NO_GRAVITY,
location[0], location[1] + anchorView.getHeight());
Also please note that you can manipulate the values in location[0] and location[1]. Good Luck :)
Edit
Here is a complete example, I use it to display a fragment below a button, this process takes place as soon as the button is clicked:
public void showPopup(View anchorView) {
View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null);
popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button proceed = (Button)popupView.findViewById(R.id.button1);
Button cancel = (Button)popupView.findViewById(R.id.button2);
cb = (CheckBox)popupView.findViewById(R.id.checkBox1);
proceed.setOnClickListener(onProceed);
cancel.setOnClickListener(onCan);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable());
int location[] = new int[2];
anchorView.getLocationOnScreen(location);
popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY,
location[0], location[1] + anchorView.getHeight());
}
The above will result in a popup, just beneath the view clicked.
popup_layout is the XML layout file which will be inflated.
I want to show a small popup at the point where user clicks on the graph.I have read the answer given for the question
Showing popup on clicking a point in graph AChartEngine
it suggests toast as the answer but I want user to select between two options in the popup.I
tried using of alertdialog and even created a custom dialog but they cover the whole screen area whereas I want just a small popup at the place where user clicks on the graph.
Here is my onotuch listener where I get the x,y coordinate
mChart.setOnTouchListener(new OnTouchListener() {
if(event.getAction() == MotionEvent.ACTION_UP)
{
int xd= (int) event.getX();
int yd = (int) event.getY();
}
The code for repositioning the dialog
AlertDialog alertDialog = builder.create();
alertDialog.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.x= xd;
lp.y= yd;
alertDialog.getWindow().setAttributes(lp);
Here is the code to resize ur dialog box
dialog.show();
Display display =((WindowManager)getSystemService(context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height=display.getHeight();
Log.v("width", width+"");
dialog.getWindow().setLayout((6*width)/7,(4*height)/5);
UPDATED
The MotionEvent class has methods getX() to get X touch point corresponding to that view and getRawX() to get X touch point corresponding to screen.
So to get X touch point corresponding to parent you can get that by a simple calculation:
view.getLeft() + motionEvent.getX()
The getLeft() returns Left position of this view relative to its parent
I have an activity with a couple of fragments inside. I'm using Frame Layout for each of the fragment and have Relative Layout as parent.
I've been having headaches on getting the exact location of a fragment in parent. But seems like I am only able to relative values.
So, is there anyway to get an exact location of a fragment within an activity?
I haven't done this with Fragments but with the contents of fragments.
If you use findById() on the activity that will find the id of anything inflated in that activity (including fragment contents etc).
You can then use something like View.getLocationOnScreen(int[]) this should give you the x and y of the View on screen. Never tried this on a fragment, but if you fragment is wrapped by a Layout then should be no problem.
Hope this helps?
Actually this might help, might need changing to take in two views as this method is inside a custom RelativeLayout I made:
/**
* Will return the X,Y of the Anchor view relative to this relative layout.. So It doesn't
* matter if the anchor view is nested, or any any other layout, this WILL find its location
* relative too this {#link HelpRelativeLayout}.
*
* #author chris.jenkins
* #param anchor
* #return
*/
private int[] calculateAnchorViewPosition(View anchor)
{
int[] loc = new int[2];
int[] conLoc = new int[2];
// getLocationInWindow(conLoc);
getLocationOnScreen(conLoc);
// anchor.getLocationInWindow(loc);
anchor.getLocationOnScreen(loc);
// get correct top left pos + half the h/w of the anchor
loc[0] = (loc[0] - conLoc[0]);
loc[1] = (loc[1] - conLoc[1]);
MyLog.d(loc[0] + " " + loc[1]);
return loc;
}