Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - get Height of the parent layout

Tags:

android

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

  • layout.getHeight()
  • layout.getRootView().getHeight()

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.

like image 832
Robin Avatar asked Sep 07 '10 06:09

Robin


People also ask

How to get Android device screen height and width?

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

How to add child view in Android Studio using 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 −

How to get the width of the parent of a view?

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.

What do I need to learn about Android layout?

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.


1 Answers

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?

like image 125
Robin Avatar answered Sep 19 '22 21:09

Robin