0

I have a RelativeLayout and inside this layout i have fab button:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_margin="8dp"/>

Now , in java I want to now, where is the fab position on the screen.I used this method according this post : enter link description here

private Point getPointOfView(View view) {
    int[] location = new int[2];
    view.getLocationInWindow(location);
    return new Point(location[0], location[1]);
}

and using this method like this:

fab = findViewById(R.id.fab);
for (int i = 0; i < pv.length; i++) {
    pv[i] = new Point(getPointOfView(fab).x ,getPointOfView(fab).y);
}

but getPointOfView(fab).x and getPointOfView(fab).y return 0.how could i retrieve the X & Y fab button?

azizbekian
  • 60,783
  • 13
  • 169
  • 249
Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149

3 Answers3

1

You can make use of View#post() API, then your code would be replaced with this one:

FloatingActionButton fab = findViewById(R.id.fab);
fab.post(new Runnable() {
    @Override
    public void run() {
        // this callback will be executed after view is laid out
        for (int i = 0; i < pv.length; ++i) {
            pv[i] = new Point(getPointOfView(fab).x ,getPointOfView(fab).y);
        }
    }
})
azizbekian
  • 60,783
  • 13
  • 169
  • 249
0

use

View.getLocationOnScreen()

method to get values. Note: try to use this method in handler

new Handler ().post(...... v.getLocationOnScreen(location); .....);

onCreate method is to early to get location we should have to try this in onWindowFocusChanged() method of activity when application end all the view related tasks.

reference

Nouman Ch
  • 4,023
  • 4
  • 29
  • 42
  • I have changed my method to `view.getLocationOnScreen(location);` but returned 0 for x and y again – Cyrus the Great Mar 10 '18 at 14:21
  • I have changed my method: `private Point getPointOfView(final View view) { final int[] location = new int[2]; new Handler().post(new Runnable() { @Override public void run() { view.getLocationOnScreen(location); } }); return new Point(location[0], location[1]); }` I got 0 – Cyrus the Great Mar 10 '18 at 14:34
  • activity also have this paste this code inside your activity. @Override public void onWindowFocusChanged(boolean hasFocus) { if(!hasFocus) { //do anything you want here Toast.makeText(MainActivity.this,"Activity changed",Toast.LENGTH_SHORT).show(); } } – Nouman Ch Mar 10 '18 at 14:49
  • dude, when activity is up this method not called but i want to get fab position at first time when activity is up.this method not called first.I have just one activity – Cyrus the Great Mar 10 '18 at 14:52
0

I found, how could i get fab position:

fab.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        for (int i = 0; i < pv.length; i++) {
            pv[i] = new Point(getPointOfView(fab).x, getPointOfView(fab).y);
        }
        fab.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }
});

but if i have mistake in removing GloabalLayout place please tel me.

Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149