Responsive design for all Android devices

I use two different methods to achieve responsive design on Android devices.

Method 1. I used different dens.xml in different folders, for example

values-hdpi values-ldpi values-mdpi values-xhdpi values-xxhdpi 

Method 2. I copied all my layouts into

 layout layout-large layout-small layout-xlarge 

and given different heights, widths and others

When I use the first method, I do not get the right responsive design, and when I use the second method, I get the right design, but it increases the size of the application.

So, please tell me the best process to achieve a 100% responsive design different from my two methods.

+5
source share
2 answers

Usually I use the folders values , values-large , ... and put the files in the dimens.xml where I specify the sizes for each category of screen size. values is "fallback", it contains the default value if I do not specify any for a certain category of sizes. In layouts, I use it as follows:

 android:width="@dimen/width_for_this_view" 

Defined in values/dimens.xml :

 <dimen name="width_for_this_view">30dp</dimen> 

You can define different sizes for different values (large, ordinary, small, ..) folders.

+2
source

You can use the google library PercentRelativeLayout with this library, you can set the width , height and margin of your views to a percentage , which is great because they look the same on the whole screen, and, of course, it's easy to encode it. Here is an example:

 <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"> <ImageView app:layout_widthPercent="50%" app:layout_heightPercent="50%" app:layout_marginTopPercent="25%" app:layout_marginLeftPercent="25%"/> </android.support.percent.PercentRelativeLayout> 

you should add this line to your build.gradle

 dependencies { compile 'com.android.support:percent:23.2.0' } 

The official documentation from Google https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html

Hope this help is for your business!

0
source

Source: https://habr.com/ru/post/1245911/


All Articles