Get the location and size of the components (views) on the screen with the Java

Asked 2 years ago, Updated 2 years ago, 23 views

I thought I could find the size and location of the views on the screen through methods such as getX The values are all zeroes Can you tell me why?

The test is After creating one project, we created it with Basic Activity and tested it with FAB with basic code.

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


ViewGroup.LayoutParams params = fab.getLayoutParams();

        Log.d("test", "width : " + params.width);
        Log.d("test", "height : " + params.height);

        Log.d("test", "X : " + fab.getX());
        Log.d("test", "Y : " + fab.getY());
        Log.d("test", "Bottom : " + fab.getBottom());
        Log.d("test", "Left : " + fab.getLeft());
        Log.d("test", "Right : " + fab.getRight());
        Log.d("test", "Top : " + fab.getTop());
        Log.d("test", "TransX : " + fab.getTranslationX());
        Log.d("test", "TransY : " + fab.getTranslationY()); 
        Log.d("test", "Width : " + fab.getWidth());
        Log.d("test", "Height : " + fab.getHeight());

I tested it like this, and the result came out like below.

05-21 01:26:37.098 2782-2782/comjongchanlee.github.testanything D/test: width : -2
05-21 01:26:37.099 2782-2782/comjongchanlee.github.testanything D/test: height : -2
05-21 01:26:37.138 2782-2782/comjongchanlee.github.testanything D/test: X : 0.0
05-21 01:26:37.138 2782-2782/comjongchanlee.github.testanything D/test: Y : 0.0
05-21 01:26:37.138 2782-2782/comjongchanlee.github.testanything D/test: Bottom : 0
05-21 01:26:37.138 2782-2782/comjongchanlee.github.testanything D/test: Left : 0
05-21 01:26:37.138 2782-2782/comjongchanlee.github.testanything D/test: Right : 0
05-21 01:26:37.138 2782-2782/comjongchanlee.github.testanything D/test: Top : 0
05-21 01:26:37.138 2782-2782/comjongchanlee.github.testanything D/test: TransX : 0.0
05-21 01:26:37.138 2782-2782/comjongchanlee.github.testanything D/test: TransY : 0.0
05-21 01:26:37.138 2782-2782/comjongchanlee.github.testanything D/test: Width : 0
05-21 01:26:37.138 2782-2782/comjongchanlee.github.testanything D/test: Height : 0

I thought it would be width or height, but... It's a weird price The rest are all zeroes....

Why is it like this?crying Did I use it wrong?

android

2022-09-22 12:42

2 Answers

Whoo..........I reply....

You can delete the text, but I leave it just in case other people can see it!

I got it in onCreate

OnCreate is called before views are drawn on the screen, so the size of the object cannot be obtained at this time..

So

You can get it from inside the onWindowFocusChanged (boolean hasFocus)...

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        ViewGroup.LayoutParams params = fab.getLayoutParams();

        Log.d("test", "width : " + params.width);
        Log.d("test", "height : " + params.height);

        Log.d("test", "X : " + fab.getX());
        Log.d("test", "Y : " + fab.getY());
        Log.d("test", "Bottom : " + fab.getBottom());
        Log.d("test", "Left : " + fab.getLeft());
        Log.d("test", "Right : " + fab.getRight());
        Log.d("test", "Top : " + fab.getTop());
        Log.d("test", "TransX : " + fab.getTranslationX());
        Log.d("test", "TransY : " + fab.getTranslationY());
        Log.d("test", "Width : " + fab.getWidth());
        Log.d("test", "Height : " + fab.getHeight());
    }

I did it like this

05-21 01:46:09.581 16524-16524/comjongchanlee.github.testanything D/test: width : -2
05-21 01:46:09.581 16524-16524/comjongchanlee.github.testanything D/test: height : -2
05-21 01:46:09.581 16524-16524/comjongchanlee.github.testanything D/test: X : 864.0
05-21 01:46:09.581 16524-16524/comjongchanlee.github.testanything D/test: Y : 1560.0
05-21 01:46:09.582 16524-16524/comjongchanlee.github.testanything D/test: Bottom : 1728
05-21 01:46:09.582 16524-16524/comjongchanlee.github.testanything D/test: Left : 864
05-21 01:46:09.582 16524-16524/comjongchanlee.github.testanything D/test: Right : 1032
05-21 01:46:09.582 16524-16524/comjongchanlee.github.testanything D/test: Top : 1560
05-21 01:46:09.585 16524-16524/comjongchanlee.github.testanything D/test: TransX : 0.0
05-21 01:46:09.585 16524-16524/comjongchanlee.github.testanything D/test: TransY : 0.0
05-21 01:46:09.585 16524-16524/comjongchanlee.github.testanything D/test: Width : 168
05-21 01:46:09.585 16524-16524/comjongchanlee.github.testanything D/test: Height : 168

It came out like this~


2022-09-22 12:42

There's a more general way~

You can put a callback on the process of configuring View on Android.

In the above case

fab.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                fab.getViewTreeObserver().removeOnPreDrawListener(this);
                //Get the size of the view here. 
                return true;
            }
        });

This allows you to get the full view size just before it is actually drawn.

In the case of onWindowFocusChanged, it's not called that I think I might be called several times unintentionally!~


2022-09-22 12:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.