Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the Width of the a view before the view is displayed?

Tags:

android

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().

like image 840
Pavankumar Vijapur Avatar asked Nov 20 '11 10:11

Pavankumar Vijapur


People also ask

How do you find the dimensions of a view?

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.

How do you find the height and width of a view?

The width and height can be obtained by calling getWidth() and getHeight().

How do you find the height of a view?

You can use the window. innerHeight property to get the viewport height, and the window. innerWidth to get its width. let viewportHeight = window.

What's the measurement in Android for views?

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.


1 Answers

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 
like image 185
Dmytro Danylyk Avatar answered Sep 18 '22 07:09

Dmytro Danylyk