Percentage width in a RelativeLayout
You are looking for the android:layout_weight
attribute. It will allow you to use percentages to define your layout.
In the following example, the left button uses 70% of the space, and the right button 30%.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:text="left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".70" />
<Button
android:text="right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".30" />
</LinearLayout>
It works the same with any kind of View, you can replace the buttons with some EditText to fit your needs.
Be sure to set the layout_width
to 0dp
or your views may not be scaled properly.
Note that the weight sum doesn't have to equal 1, I just find it easier to read like this. You can set the first weight to 7 and the second to 3 and it will give the same result.
Percentage width for a relative layout
for this you can use LinearLayout with weightSum and layout_wright property
so try following code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="8" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
<!-- 1/8 % of Screen to left of your RelativeLayour -->
</LinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="6"
android:background="@color/white"
android:orientation="vertical" >
</RelativeLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
<!-- 1/8 % of Screen to right of your RelativeLayour -->
</LinearLayout>
</LinearLayout>
set width and height as percentage in relative layout
Try the below code and see whether it works,
This code will get the layout what you have attached
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/dialog_instruction_pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/theme_blue" >
</android.support.v4.view.ViewPager>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:background="@drawable/ic_launcher" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:background="@drawable/ic_launcher" />
</RelativeLayout>
How to adjust width of relative layouts in percent?
Add weightSum
to parent linear layout.
Check this answer
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="4">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3">
</RelativeLayout>
Defining a percentage width for a LinearLayout?
You have to set the weight property of your elements. Create three RelativeLayouts as children to your LinearLayout and set weights 0.15, 0.70, 0.15. Then add your buttons to the second RelativeLayout(the one with weight 0.70).
Like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/layoutContainer" android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="0.15">
</RelativeLayout>
<RelativeLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="0.7">
<!-- This is the part that's 70% of the total width. I'm inserting a LinearLayout and buttons.-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<Button
android:text="Button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="Button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="Button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
<!-- 70% Width End-->
</RelativeLayout>
<RelativeLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="0.15">
</RelativeLayout>
</LinearLayout>
Why are the weights 0.15, 0.7 and 0.15? Because the total weight is 1 and 0.7 is 70% of the total.
Result:
Edit: Thanks to @SimonVeloper for pointing out that the orientation should be horizontal and not vertical and to @Andrew for pointing out that weights can be decimals instead of integers.
Android - set width percentage to an editText inside a relativeLayout programatically
PercentRelativeLayout
is recommended for your purpose.
But if you still want to use RelativeLayout
and change the percentage in code, you can compute the width and set it to your EditText
like this:
editText.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
editText.getViewTreeObserver().removeOnGlobalLayoutListener(this);
ViewGroup.LayoutParams params = editText.getLayoutParams();
params.width = (int) (relativeLayout.getWidth() * 0.7 + 0.5);
editText.setLayoutParams(params);
}
});
Hope this can help you.
Android percentage width layout
You should set view's width to 0dip
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp">
<LinearLayout
android:id="@+id/stupid_android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<View
android:layout_width="0dip"
android:layout_height="match_parent"
android:background="#FF0000"
android:layout_weight="1" />
<View
android:layout_width="0dip"
android:layout_height="match_parent"
android:background="#0000FF"
android:layout_weight="2" />
<View
android:layout_width="0dip"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
How to set a layout width percentage of one third on ConstraintLayout?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" >
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="3"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="3"/>
</LinearLayout>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="3"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="5"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="6"/>
</LinearLayout>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="3"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="7"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="8"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="9"/>
</LinearLayout>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="3"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="10"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="11"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="12"/>
</LinearLayout>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="3"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="13"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="14"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="15"/>
</LinearLayout>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="3"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="16"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="17"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="18"/>
</LinearLayout>
</LinearLayout>
Related Topics
How to Change Font Face of Webview in Android
Need to Handle Uncaught Exception and Send Log File
How to Delete an Sms in Android Before It Reaches the Inbox
Android Material and Appcompat Manifest Merger Failed
Android Get Real Path by Uri.Getpath()
Android- Error:Execution Failed for Task ':App:Transformclasseswithdexforrelease'
How to Store(Bitmap Image) and Retrieve Image from SQLite Database in Android
Using Build Flavors - Structuring Source Folders and Build.Gradle Correctly
Singleton Object Becomes Null After App Is Resumed
Remove Fragment Page from Viewpager in Android
Android and Facebook Share Intent
How to Get the Absolute Coordinates of a View
Show Compose Sms View in Android
How to Prevent Screen Capture in Android