Search code examples
androidandroid-layoutandroid-percentrelativelayout

How to make Percent dynamic in PercentRelativeLayout?


I am using PercentRelativeLayout from Design Support Library and i want to set different Percentage for 7 inch and 10 inch tablet.

For example if i have ImageView like below.

<ImageView
     android:id="@+id/contactDoc"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     app:layout_widthPercent="70%"
     app:layout_heightPercent="70%"
     android:layout_centerHorizontal="true"
     android:clickable="true"
     android:src="@drawable/dashboard_contactdoctor" />

now if i want to set 70% for 7 inch tablet and 60% for 10inch tablet without making different layout folder like sw720dp . Can i do it? Help is appreciated.


Solution

  • Those percentages are fraction resources. You should be able to set up res/values/fractions.xml and res/values-sw720dp/fractions.xml, where you define the values for the fractions. Then, use @fraction/whatever_you_called_it in the layout.