{"id":32836,"date":"2016-02-26T10:11:04","date_gmt":"2016-02-26T04:41:04","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=32836"},"modified":"2016-03-14T10:43:59","modified_gmt":"2016-03-14T05:13:59","slug":"percentrelativelayout-in-android","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/percentrelativelayout-in-android\/","title":{"rendered":"PercentRelativeLayout in Android"},"content":{"rendered":"<p>During most of the <a title=\"Android App Development Services\" href=\"http:\/\/www.tothenew.com\/mobile-android-application-development-services\">android design and development<\/a> , we mainly use three types of layout: <em><strong>LineraLayout ,RelativeLayout, FrameLayout<\/strong><\/em>. And for creating complex view we use weight property to distribute view across screens.<\/p>\n<p>But while using weights you must have noticed that we have to add a default container view to encapsulate our child view . Now problem with this approach is it adds an additional view hierarchy to our layout, which is of no use except holding child view and holding weight sum value.<\/p>\n<blockquote><p><em><strong>Last year, android has introduced a new layout called\u00a0PercentRelativeLayout which gives us the functionality of both relativelayout and weight property together .<\/strong><\/em><\/p><\/blockquote>\n<p><em><a href=\"http:\/\/developer.android.com\/reference\/android\/support\/percent\/PercentRelativeLayout.html\"><strong>PercentRelativeLayout<\/strong><\/a>\u00a0is same as relative layout with an additional feature &#8216;percentage&#8217;. we can put percentage on view component (out of 100) in terms of width,height,margins etc.<\/em><\/p>\n<p>Or simply we can say use relative percent layout as linear layout as it has features of both along with reduced view complexity.<\/p>\n<p>To use percent relative layout add gradle path i.e :<\/p>\n<p>[sourcecode language=&#8221;java&#8221;]<br \/>\ncompile &#8216;com.android.support:percent:23.1.1&#8217; in your build.gradle file.<br \/>\n[\/sourcecode]<\/p>\n<p>And you are on.<\/p>\n<p>An Example shown below\u00a0depict&#8217;s how to use and comparison of view hierarchy of two layouts, with and without <strong>PercentRelativeLayout<\/strong><\/p>\n<p>Let&#8217;s make it using linearLayout first.<\/p>\n<p>[sourcecode language=&#8221;java&#8221;]<br \/>\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;&lt;\/pre&gt;<br \/>\n&lt;LinearLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;<br \/>\nxmlns:app=&quot;http:\/\/schemas.android.com\/apk\/res-auto&quot;<br \/>\nandroid:layout_width=&quot;match_parent&quot;<br \/>\nandroid:layout_height=&quot;match_parent&quot;<br \/>\nandroid:orientation=&quot;vertical&quot;<br \/>\nandroid:weightSum=&quot;4&quot;&gt;<\/p>\n<p>&lt;LinearLayout<br \/>\nandroid:layout_width=&quot;match_parent&quot;<br \/>\nandroid:layout_height=&quot;0dp&quot;<br \/>\nandroid:layout_weight=&quot;1.5&quot;<br \/>\nandroid:gravity=&quot;center&quot;&gt;<\/p>\n<p>&lt;TextView<br \/>\nandroid:layout_width=&quot;wrap_content&quot;<br \/>\nandroid:layout_height=&quot;wrap_content&quot;<br \/>\nandroid:text=&quot;MAIN HEADER&quot;<br \/>\nandroid:textSize=&quot;50dp&quot; \/&gt;<\/p>\n<p>&lt;\/LinearLayout&gt;<\/p>\n<p>&lt;LinearLayout<br \/>\nandroid:layout_width=&quot;match_parent&quot;<br \/>\nandroid:layout_height=&quot;0dp&quot;<br \/>\nandroid:layout_weight=&quot;1&quot;<br \/>\nandroid:orientation=&quot;vertical&quot;&gt;<\/p>\n<p>&lt;TextView<br \/>\nandroid:id=&quot;@+id\/textview_sub_header&quot;<br \/>\nandroid:layout_width=&quot;match_parent&quot;<br \/>\nandroid:layout_height=&quot;wrap_content&quot;<br \/>\nandroid:layout_marginTop=&quot;20dp&quot;<br \/>\nandroid:gravity=&quot;center&quot;<br \/>\nandroid:text=&quot;Sub Header&quot;<br \/>\nandroid:textSize=&quot;25dp&quot;\/&gt;<\/p>\n<p>&lt;\/LinearLayout&gt;<\/p>\n<p>&lt;LinearLayout<br \/>\nandroid:layout_width=&quot;match_parent&quot;<br \/>\nandroid:layout_height=&quot;0dp&quot;<br \/>\nandroid:layout_weight=&quot;0.5&quot;<br \/>\nandroid:orientation=&quot;horizontal&quot;&gt;<\/p>\n<p>&lt;Button<br \/>\nandroid:id=&quot;@+id\/button&quot;<br \/>\nandroid:layout_width=&quot;match_parent&quot;<br \/>\nandroid:layout_height=&quot;80dp&quot;<br \/>\nandroid:text=&quot;Button&quot;<br \/>\nandroid:background=&quot;@android:color\/holo_blue_light&quot;<br \/>\nandroid:textColor=&quot;@android:color\/white&quot;<br \/>\nandroid:textSize=&quot;25dp&quot;<br \/>\nandroid:textStyle=&quot;bold&quot;\/&gt;<\/p>\n<p>&lt;\/LinearLayout&gt;<\/p>\n<p>&lt;TextView<br \/>\nandroid:id=&quot;@+id\/textview_bottom_text&quot;<br \/>\nandroid:layout_width=&quot;match_parent&quot;<br \/>\nandroid:layout_height=&quot;0dp&quot;<br \/>\nandroid:layout_weight=&quot;1.0&quot;<br \/>\nandroid:gravity=&quot;center&quot;<br \/>\nandroid:text=&quot;BOTTOM TEXT&quot;<br \/>\nandroid:textSize=&quot;20dp&quot;\/&gt;<\/p>\n<p>&lt;\/LinearLayout&gt;<br \/>\n[\/sourcecode]<\/p>\n<p>and now with the percentLayout<\/p>\n<p>[sourcecode language=&#8221;java&#8221;]<br \/>\n&lt;android.support.percent.PercentRelativeLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;<br \/>\nxmlns:app=&quot;http:\/\/schemas.android.com\/apk\/res-auto&quot;<br \/>\nandroid:layout_width=&quot;match_parent&quot;<br \/>\nandroid:layout_height=&quot;match_parent&quot;&gt;<\/p>\n<p>&lt;TextView<br \/>\nandroid:id=&quot;@+id\/textview_title&quot;<br \/>\nandroid:layout_centerHorizontal=&quot;true&quot;<br \/>\napp:layout_heightPercent=&quot;10%&quot;<br \/>\nandroid:gravity=&quot;center&quot;<br \/>\napp:layout_marginTopPercent=&quot;14%&quot;<br \/>\napp:layout_widthPercent=&quot;100%&quot;<br \/>\nandroid:textSize=&quot;50dp&quot;<br \/>\nandroid:text=&quot;MAIN HEADER&quot;\/&gt;<\/p>\n<p>&lt;TextView<br \/>\nandroid:id=&quot;@+id\/textview_sub_header&quot;<br \/>\nandroid:layout_height=&quot;wrap_content&quot;<br \/>\nandroid:layout_below=&quot;@+id\/textview_title&quot;<br \/>\nandroid:gravity=&quot;center&quot;<br \/>\nandroid:text=&quot;Sub Header&quot;<br \/>\nandroid:textSize=&quot;25dp&quot;<br \/>\napp:layout_marginTopPercent=&quot;17%&quot;<br \/>\napp:layout_widthPercent=&quot;100%&quot; \/&gt;<\/p>\n<p>&lt;Button<br \/>\nandroid:id=&quot;@+id\/button_blogger&quot;<br \/>\nandroid:layout_below=&quot;@+id\/textview_sub_header&quot;<br \/>\nandroid:text=&quot;button&quot;<br \/>\nandroid:textColor=&quot;@android:color\/white&quot;<br \/>\nandroid:textSize=&quot;25dp&quot;<br \/>\nandroid:textStyle=&quot;bold&quot;<br \/>\nandroid:background=&quot;@android:color\/holo_blue_light&quot;<br \/>\napp:layout_heightPercent=&quot;13%&quot;<br \/>\napp:layout_marginTopPercent=&quot;16%&quot;<br \/>\napp:layout_widthPercent=&quot;100%&quot; \/&gt;<\/p>\n<p>&lt;TextView<br \/>\nandroid:id=&quot;@+id\/textview_bottom_text&quot;<br \/>\nandroid:layout_height=&quot;match_parent&quot;<br \/>\nandroid:layout_below=&quot;@+id\/button_blogger&quot;<br \/>\nandroid:gravity=&quot;center&quot;<br \/>\nandroid:text=&quot;BOTTOM TEXT&quot;<br \/>\nandroid:textSize=&quot;20dp&quot;<br \/>\napp:layout_widthPercent=&quot;100%&quot; \/&gt;<\/p>\n<p>&lt;\/android.support.percent.PercentRelativeLayout&gt;<br \/>\n[\/sourcecode]<\/p>\n<p>&nbsp;<\/p>\n<p>Below Images showing the preview of the above two layouts :<\/p>\n<div style=\"width: 100%; height: 100%; clear: both; overflow: hidden;\"><img decoding=\"async\" style=\"float: left; width: 49%; height: 100%;\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/02\/simple.png\" alt=\"preview1\" \/><br \/>\n<img decoding=\"async\" style=\"float: right; width: 49%; height: 100%; margin-top: 0;\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/02\/percentLayout.png\" alt=\"preview2\" \/><\/div>\n<p>And let&#8217;s see the diffrence in the view hierarchy:<\/p>\n<div style=\"width: 100%; height: 100%; clear: both; overflow: hidden;\"><img decoding=\"async\" style=\"float: left; width: 49%; height: 100%;\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/02\/heirarchy_preview2.png\" alt=\"preview1\" \/><br \/>\n<img decoding=\"async\" style=\"float: right; width: 49%; height: 100%; margin-top: 0;\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/02\/preview1_hrrarchy.png\" alt=\"heirarchy_preview2\" \/><\/div>\n<p>In this way we can reduce the view complexity using percentRelativeLayout.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>During most of the android design and development , we mainly use three types of layout: LineraLayout ,RelativeLayout, FrameLayout. And for creating complex view we use weight property to distribute view across screens. But while using weights you must have noticed that we have to add a default container view to encapsulate our child view [&hellip;]<\/p>\n","protected":false},"author":156,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":14},"categories":[518,1],"tags":[3164,3165],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/32836"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/156"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=32836"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/32836\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=32836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=32836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=32836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}