Assign Width to Half Available Screen Width Declaratively

Assign width to half available screen width declaratively

If your widget is a Button:

<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<Button android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="somebutton"/>

<TextView android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>

I'm assuming you want your widget to take up one half, and another widget to take up the other half. The trick is using a LinearLayout, setting layout_width="fill_parent" on both widgets, and setting layout_weight to the same value on both widgets as well. If there are two widgets, both with the same weight, the LinearLayout will split the width between the two widgets.

Button half width of screen

Yep, the solution is very similar to that question, but you also want to set the weightSum of the parent LinearLayout:

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:padding="6dp"
android:weightSum="2">

<Button
android:id="@+id/buttonCollect"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/hello"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:layout_weight="1" />

</LinearLayout>

Android: 2 relative layout divided in half screen

You can put these 2 RelativeLayouts inside a LinearLayout with horizontal orientation, then use the weight for both RelativeLayouts. This will divide the LinearLayout in 2 equal parts.

<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>

Make image appear half of the screen

Use LinearyLayout to wrap your ImageView and another invisible View, and set layout_weight both to 0.5, then the ImageView should be half the size.

Here is an example for you showing how to set the image half the screen size

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:background="#ff0000"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/your_image"
android:adjustViewBounds="true"
/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:background="#00ff00"
/>
</LinearLayout>

And this is the result

Sample Image

If you want to put your image in center of the screen, you can use the same idea and set proper weight to achieve your goal. Good luck!

Android make button 1/3 of screen width and height in relative layout

you can do this easily in java code.

write this code in oncreate.

    Button btn = (Button) findViewById(R.id.button);
int width = getResources().getDisplayMetrics().widthPixels/3;
int height = getResources().getDisplayMetrics().heightPixels/3;
btn.setLayoutParams(new RelativeLayout.LayoutParams(width, heigth));

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

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>


Related Topics



Leave a reply



Submit