How Get Button Coordinate? August 21, 2024 Post a Comment I have a layout activity_main.xml: Solution 1: Inside OnCreate() method define this Buttonbutton= (Button) findViewById(R.id.button); Pointpoint= getPointOfView(button); Log.d(TAG, "view point x,y (" + point.x + ", " + point.y + ")"); Copyand create this to get location of button.private Point getPointOfView(View view){ int[] location = newint[2]; view.getLocationInWindow(location); returnnewPoint(location[0], location[1]); } CopySolution 2: This happens because you are getting the coordinates of your view before it's actually shown. You can use the View.post() method, which executes some code after your View is properly initalized.mButton.post( newRunnable(){ @Overridepublicvoidrun(){ x = mButton.getX(); y = mButton.getY(); } } ); CopySolution 3: A few things to consider when you deal with view coordinates:getLocationInWindow() returns the position within a window, which usually has a greater height than your root layout, because the former includes the notifications bar / appbar / tabs.getX(), getY(), getTop(), getLeft() all return the position within your view's parent, which may be irrelevant, if your view is not a direct child of the root layout.If your view's position depends on the height of another view (e.g. it is located below another view, who's height if set as wrap_content), then you will be able to know the final position only after the global layout is finished (use OnGlobalLayoutListener).Solution 4: publicclassMainActivityextendsActivity { Button b1, b2, b3, b4; int b1x1, b1x2, b1y1, b1y2; private TextView xcordview; private TextView ycordview; private TextView buttonIndicator; private RelativeLayout touchview; privatestaticint defaultStates[]; private Button mLastButton; privatefinalstaticint[] STATE_PRESSED = { android.R.attr.state_pressed, android.R.attr.state_focused | android.R.attr.state_enabled }; @OverrideprotectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); xcordview = (TextView) findViewById(R.id.textView4); ycordview = (TextView) findViewById(R.id.textView3); buttonIndicator = (TextView) findViewById(R.id.button_indicator); touchview = (RelativeLayout) findViewById(R.id.relativelayout); b1 = (Button) findViewById(R.id.button1); b2 = (Button) findViewById(R.id.button2); b3 = (Button) findViewById(R.id.button3); b4 = (Button) findViewById(R.id.button4); defaultStates = b1.getBackground().getState(); } @OverrideprotectedvoidonResume() { // TODO Auto-generated method stubsuper.onResume(); touchview.setOnTouchListener(newView.OnTouchListener() { privatebooleanisInside=false; @OverridepublicbooleanonTouch(View v, MotionEvent event) { intx= (int) event.getX(); inty= (int) event.getY(); xcordview.setText(String.valueOf(x)); ycordview.setText(String.valueOf(y)); for (inti=0; i < touchview.getChildCount(); i++) { Viewcurrent= touchview.getChildAt(i); if (current instanceof Button) { Buttonb= (Button) current; if (!isPointWithin(x, y, b.getLeft(), b.getRight(), b.getTop(), b.getBottom())) { b.getBackground().setState(defaultStates); } if (isPointWithin(x, y, b.getLeft(), b.getRight(), b.getTop(), b.getBottom())) { b.getBackground().setState(STATE_PRESSED); if (b != mLastButton) { mLastButton = b; buttonIndicator.setText(mLastButton.getText()); } } } } returntrue; } }); } @OverridepublicvoidonWindowFocusChanged(boolean hasFocus) { // TODO Auto-generated method stubsuper.onWindowFocusChanged(hasFocus); } staticbooleanisPointWithin(int x, int y, int x1, int x2, int y1, int y2) { return (x <= x2 && x >= x1 && y <= y2 && y >= y1); } } Copy Share Post a Comment for "How Get Button Coordinate?"
Post a Comment for "How Get Button Coordinate?"