This tutorial will help you to give percentage based layout width and height. As now the Android have introduce percentage support library. It will helps to run your applications for multiple screen sizes.
So far this support library support for some following layout:-
1- With RelativeLayout ---> android.support.percent.PercentRelativeLayout
2- With FrameLayout ---> android.support.percent.PercentFrameLayout
How to use it:
First, We will include support library at build.grade(Module: app)
dependencies { compile 'com.android.support:percent:23.3.0' }
Now, We will go to our xml layout and add following snippet of code
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:text="Button"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
app:layout_widthPercent="30%"/>
<Button
android:id="@+id/button2"
android:text="Button 2"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button1"
app:layout_widthPercent="70%"/>
<Button
android:id="@+id/button3"
android:text="Button 3"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
app:layout_widthPercent="40%"/>
</android.support.percent.PercentRelativeLayout>
0 Comment(s)