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

46 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. Great Article
      android based projects





      Java Training in Chennai

      Project Center in Chennai

      Java Training in Chennai

      projects for cse



      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training Project Centers in Chennai

      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
  19. OMGGG...I am surprised to see thik horse riding tales hack thanks for the effort you ppl take to get this resources.

    ReplyDelete
  20. My engagement ring is platinum and I couldn't be happier. It is so shiny however the downside is that it's pretty expensive and costs more to size than gold. There are scratches although not very noticeable seaport hack

    ReplyDelete
  21. They seem to be easy but infact requires a lot of time and hardwork . Great work world of tanks blitz hack

    ReplyDelete
  22. The article was up to the point and described the information very effectively. Thanks to blog author for wonderful and informative post.
    Gastroenterologist

    ReplyDelete
  23. Your jewelry is amazing. These could be used for embellishments for other craft items as well. Great ideas. horse riding tales hack

    ReplyDelete
  24. The article was up to the point and described the information very effectively. Thanks to blog author for wonderful and informative post.
    tell the bell

    ReplyDelete
  25. The article was up to the point and described the information very effectively. Thanks to blog author for wonderful and informative post.
    krogerfeedback

    ReplyDelete
  26. Greatpeople made an Online Employee Portal named Feed Kroger Login Portal or Website and in order to perform the sign in or log in, you need to open the official website URL which is Feed.Kroger.com on any of your favorite browser.

    ReplyDelete
  27. Buy Modafinil Online at your door step from one & only trusted pharma in this ERA. Buying Modafinil 100mg with Credit card.

    ReplyDelete
  28. For AC Repair Dubai, ac maintenance, ac service in dubai, give us a call and our team with fully anti corona precautions will be at your place.

    ReplyDelete
  29. Handyman Dubai offers you a diverse range of repair and home maintenance services in Dubai. A handyman is more efficient than you think if you hire a professional from us. Whether it is your apartment, villa, office, factory, or warehouse we have extended our reach anywhere in Dubai.

    ReplyDelete
  30. HiTech FM is a home maintenance company in Dubai that offers services across all trade disciplines, including Cleaning, Plumbing, Electrical, Carpentry, Painting, Tiling, Air Conditioning, AC Servicing and more. We employ only expert tradespeople, ensuring the highest quality service. matter what your maintenance requirements. We are the best in Home & Office Maintenance Services in UAE.

    ReplyDelete
  31. The article was absolutely fantastic! Lot of great information which can be helpful in some or the other way. Keep updating the blog, looking forward for more contents.
    by cognex is the AWS Training in chennai

    ReplyDelete
  32. Dr. Vivek Galani is a leading expert in skin and hair. At hair transplant clinic in Surat Skin Care, Cosmetic Laser, Hair Transplant & Slimming Center, Dr. Galani offers the most advanced cosmetic and dermatologic care treatments. The clinic uses advanced FUE methods to produce high-quality hair transplants.

    ReplyDelete
  33. Shreeja Health Care is leading manufacturer of Oil Making Machine. Shreeja Oil Extraction Machine is able to extractoil from various seeds like peanuts, Coconut, Sesame, Soybean, macadamia nuts, walnuts, sunflower seeds, vegetable seeds flaxseed et.

    ReplyDelete
  34. I'm really impressed and explained simply with lost of content bowling crew hack

    ReplyDelete
  35. These are some great tools that i definitely use for SEO work. This is a great list to use in the future..
    mcdvoicee.site

    ReplyDelete
  36. By sending refreshes about proposals on your business applications, you can permit clients to make buys effectively and quicker, at whenever and from anyplace. TutuApp web

    ReplyDelete
  37. I have joined up with the feed and also enjoy searching for a lot more of one's fantastic submit. Furthermore, I have contributed the website within my social support systems! https://tipandroid.com/

    ReplyDelete
  38. https://www.coderconsole.com/2015/08/android-percent-support-lib-sample.html

    ReplyDelete
  39. his is very useful and it is is a great share!! Brawlhalla Mobile Hack

    ReplyDelete

Your comments are valuable for us !!!