I want to determine the available height and width of the parent layout to which I have to add my view.
I have used many methods on layout like
All these methods return 0(zero) as the result.
My main requirement is to be able to give width to a view that is some % of the width of the layout .
Also I dont want to use tag for this. I want to do it through code.
Thanks in advance.
This example demonstrates how do I get android device screen height, width. 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. Step 3 − Add the following code to src/MainActivity.java
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. In the above example we don't have any view, we have created a root view /parent view. we can add child view in java as shown below −
You could solve this by creating a custom View and override the onMeasure () method. If you always use "fill_parent" for the layout_width in your xml then the widthMeasureSpec parameter that is passed into the onMeasusre () method should contain the width of the parent.
Learn about Android's layout width and layout height attributes - possible values that can be assigned to these attributes, the difference between match parent and wrap content. Also, learn about dp (density independent pixel), sp (scale independent pixel) , and px (pixel density) in detail.
Ok. I found a solution to it. The way we can get hieght of the screen in activity is
getApplicationContext().getResources().getDisplayMetrics().heightPixels
But I still dont know how to get the pixel dimentions of parent layout?
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