varghesekutty
varghesekutty

Reputation: 1005

How to arrange width and height of relative layout to be half of the screen

I am developing an android application and I want to set my relative layout's width and height as half of my screen width and height.

Thanks

Upvotes: 5

Views: 14639

Answers (4)

dipali
dipali

Reputation: 11188

*@SuppressLint("NewApi")
public static int getDeviceWidth(Activity activity) {
    int deviceWidth = 0;

    Point size = new Point();
    WindowManager windowManager = activity.getWindowManager();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        windowManager.getDefaultDisplay().getSize(size);
        deviceWidth = size.x;
    } else {
        Display display = windowManager.getDefaultDisplay();
        deviceWidth = display.getWidth();
    }
    return deviceWidth;
}

@SuppressLint("NewApi")
public static int getDeviceHeight(Activity activity) {
    int deviceHeight = 0;

    Point size = new Point();
    WindowManager windowManager = activity.getWindowManager();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        windowManager.getDefaultDisplay().getSize(size);
        deviceHeight = size.y;
    } else {
        Display display = windowManager.getDefaultDisplay();
        deviceHeight = display.getHeight();
    }
    return deviceHeight;
}*

above two function is get device height and width.

Use on this function, you can get half of the size of screen.
I hope its useful to you.

Upvotes: 2

Lal
Lal

Reputation: 14810

To Divide a Relative layout into two equal relative layouts

    <RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false">
<RelativeLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1">
</RelativeLayout>
<RelativeLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1">
</RelativeLayout>
</RelativeLayout>

and to Divide a linear layout into two equal relative layouts

    <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">
<RelativeLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1">
</RelativeLayout>
<RelativeLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1">
</RelativeLayout>
</LinearLayout>

Upvotes: 2

ilomambo
ilomambo

Reputation: 8350

You need to put the RelativeLayout inside a LinearLayout and use android:weightsum=2 and android:layout_weight=1 to get the desired proportion

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="2" >

    <RelativeLayout
        ....
        android:layout_weight=1
        ....>
    </RelativeLAyout>

</LinearLayout>

Upvotes: 1

Jagadesh Seeram
Jagadesh Seeram

Reputation: 2664

Use Parent as the LinearLayout and use weightSum and weight attributes

Sample

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100" >

  <RelativeLayout
      android:id="@+id/button1"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="50" >

      <!-- Your RelativeLayout Items -->

  </RelativeLayout>

</LinearLayout>

Upvotes: 10

Related Questions