How to find the Width of the a view before the view is displayed? I do not want to add a custom view and tap the dimensions in the OnChanged().
So here is a simple solution to get the dimensions of a view in android. Step 1 − Create a new project in Android Studio,go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
The width and height can be obtained by calling getWidth() and getHeight().
You can use the window. innerHeight property to get the viewport height, and the window. innerWidth to get its width. let viewportHeight = window.
Each View has two different types of sizes: the measured width and height, which is basically how big it wants to be, and the width and height that are the actual dimensions they have after being laid out. The desired size cannot always be provided, e.g. a view could request a width larger than its parent's width.
Display display = getWindowManager().getDefaultDisplay(); View view = findViewById(R.id.YOUR_VIEW_ID); view.measure(display.getWidth(), display.getHeight()); view.getMeasuredWidth(); // view width view.getMeasuredHeight(); //view height
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With