Last Updated: August 30, 2015

Android percent support lib sample

Update: 

From API Level 26, percent support library is deprecated. Please use updated code and samples from Github project.

GitHub project HERE

In this post I'm gonna show you guys a demo of percent layout of android from support library. Since many a time in our awesome RelativeLayout we insert LinearLayout just to get the property of layout_weight for accessing.

Now android has a new support library to remove this dependencies.

Before beginning some pre-requestic (at the time for development):

  • Android SDK v22
  • Android Build Tools v22.0.1
  • Android Percent Support Repository v23.0.0
  • Android Support appcompat-v7:22.2.1
So let's get started:




Step 1: Add below line into apps build.gradle.

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

Step 2:  Add any one of below as the parent of the layout.Its similar to our RelativeLayout or FrameLayout.

<android.support.percent.PercentRelativeLayout> 
or
<android.support.percent.PercentFrameLayout>

Step 3: Now we gonna use layout_heightPercent and layout_widthPercent  property's to specify our height and width in percentage.

Consider this simple layout.

percent_layout.xml


















Lets make it :-)




<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/fifty_fifty_tv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="center_horizontal"
        android:background="#ffff8800"
        android:text="50% - 50%"
        android:textColor="@android:color/white"
        android:textSize="25sp"
        app:layout_heightPercent="50%"
        app:layout_widthPercent="50%"
         />
    <TextView
        android:id="@+id/twenty_fifty_tv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="center_horizontal"
        android:layout_toRightOf="@id/fifty_fifty_tv"
        android:background="#ffff5566"
        android:text="20%-50%"
        android:textSize="25sp"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="50%"
        />

    <TextView
        android:id="@+id/thirty_fifty_tv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_below="@id/twenty_fifty_tv"
        android:layout_gravity="center_horizontal"
        android:layout_toRightOf="@id/fifty_fifty_tv"
        android:background="#aa3628cc"
        android:text="30%-50%"
        android:textSize="25sp"
        app:layout_heightPercent="30%"
        app:layout_widthPercent="50%"
        />
    <TextView
        android:id="@+id/century_50_tv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_below="@id/fifty_fifty_tv"
        android:layout_gravity="center_horizontal"
        android:background="#aacacc46"
        android:text="50%-100%"
        android:textSize="25sp"
        app:layout_heightPercent="50%"
        app:layout_widthPercent="100%"
        />
</android.support.percent.PercentRelativeLayout>




Thus by using the android percent support library we have made our layout simple by removing boilerplate layout.

GitHub project HERE

Really awesome :-)

23 comments :

  1. hello,if i am using relative layout inside relative layout it is not working with percent
    here is my code -:







    ReplyDelete
    Replies
    1. @Abhishek Shrivastava I guess your code snippet got truncated. Please paste again.

      Delete
  2. Its open source nature makes it easier for device manufacturers to customize Android according to their specific needs. check here

    ReplyDelete
  3. Thanks for the nice blog. It was useful for me. I'm happy I found this blog. Thank you for sharing with us,I too always learn something new from your post.
    Kroger experience

    ReplyDelete
  4. I like this post,And I guess that having fun to read this post,they shall take a good site to make a information,thanks for sharing it to me.
    https://tellthebelll.us

    ReplyDelete
  5. Thanks for providing recent updates regarding the concern, I look to read more.
    Kroger customer survey

    ReplyDelete
  6. The blog and data is excellent and informative as well

    showboxappguide.com

    ReplyDelete
  7. dubsmash apk
    Hi! This is my first visit to your blog! We are a team of volunteers and new initiatives in the same niche. Blog gave us useful information to work. You have done an amazing job!

    ReplyDelete
  8. i really like this article please keep it up.
    check here

    ReplyDelete
  9. I really loved reading your blog. It was very well authored easy to undertand. Unlike additional blogs I have read which are really not tht good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he ejoyed it as well!
    https://www-expresshr.us/

    ReplyDelete
  10. I was surfing net and fortunately across this site and found very interesting stuff here. Its really fun to read. I enjoyed a lot. Thanks for sharing this wonderful information.
    https://wvw-greatpeople.me/

    ReplyDelete
  11. All the contents you mentioned in is too good and can be very useful. I will keep it in mind, thanks for sharing the information keep updating, looking forward for more posts.Thanks
    UPS employee portal

    ReplyDelete
  12. This is my first t e i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work
    seo tools

    ReplyDelete
  13. This is the news I have been waiting for. My wife has been trying to get me to quit smoking for years. There is no way that I will be able to go cold turkey. If e-cigarettes are as harmless as puffing air, I don't think she will be opposed to a conversion.
    survey monkey usa

    ReplyDelete
  14. I really thank you for the valuable info on this great subject and look forward to more great posts data loggers

    ReplyDelete
  15. this is a very good site and also visit this site watch latest movies and tv shows online for free without any credit card.
    showbox.apk

    ReplyDelete
  16. i see you got really very useful topics, i will be always checking your blog thanks. mobdro all streams offline

    ReplyDelete
  17. A great services provider with quality and low prices, without any visit you can take your services just by contact us.
    ac maintenance in dubai
    ac service in dubai

    ReplyDelete
  18. If you are looking for AC maintenance in dubai then we are the best maintenance services provider. For AC maintenance Dubai, there are many companies regarding the maintenance and repair of Air conditioners.

    ReplyDelete

Your comments are valuable for us !!!