Did you know about Percent Support Library

Yes, we can finally get rid of hardcoded values or nested layouts used in order to take advantage of layout_weight. 

Definition

Percent Support Library is a new library introduced by Google, used to set the width, height and margins for a view, using percent (%). 

Usage

In order to use this library you have to:

1. Import the library in the build.gradle file

 compile 'com.android.support:percent:23.0.1'

2. Use your views inside PercentFrameLayout or PercentRelativeLayout.

3. Set the percent attributes using these attributes:

app:layout_widthPercent="70%"
app:layout_heightPercent="20%"
app:layout_marginStartPercent="5%"
app:layout_marginEndPercent="5%"

Example

<?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"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_dark"
        app:layout_widthPercent="70%"
        android:text="Hello World!" />
</android.support.percent.PercentRelativeLayout>

References

https://plus.google.com/+AndroidDevelopers/posts/C8oaLunpEEj

https://developer.android.com/tools/support-library/features.html?linkId=16439965#percent

POLL

Did you know about Percent Support Library?
Scroll to Top