Vikas Gupta
Vikas Gupta

Reputation: 1530

Android: How to set header's height or width in terms of screen

I am trying to make an application. It contains multiple activities which all have same header and footer. I want that these header and footer should look exactly same on every screen in terms of height. So i defined android:layout_height="20dp" in each Header and Footer. But it is not looking same in each activity, when number of entities (button) are more in field, size of header is reduced and when there is nothing in the field size works fine. So i thought that i should define Height or Width in terms of screen. So can anyone tell me how to define size of entities in terms of screen of device. I know how to get screen size

Display displayScreen = ((WindowManager) getSystemService(HomeActivity.WINDOW_SERVICE)).getDefaultDisplay();
        int layoutWidth = displayScreen.getWidth();
        int layoutHeight = displayScreen.getHeight();

and this is code of header

<LinearLayout android:id="@+id/llheader" android:layout_width="fill_parent" android:layout_height="20dp"
    android:layout_weight="1">
    <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:layout_gravity="center"> <!--header -->
        <TextView android:id="@+id/txt_header" android:layout_width="wrap_content" android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" android:layout_height="wrap_content" />
    </RelativeLayout>
</LinearLayout>

But don't know how to use that in defining height or width from java file. So please tell me the formatting of java code how to define size of anything with respect to screen.

Upvotes: 1

Views: 2667

Answers (2)

Shahzad Imam
Shahzad Imam

Reputation: 2018

Try it...

    Display display =((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

    int width = display.getWidth();

    int height=display.getHeight();
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width/10, height/10);

    todostextview.setLayoutParams(layoutParams);

Give desired value in layoutparams

Upvotes: 1

gandharva
gandharva

Reputation: 691

<RelativeLayout> <header> <body> <!-- set layout_above and layout_below to footer's and header's id, respectively --> <footer> </RelativeLayout>

Upvotes: 0

Related Questions