How to leave responsive components in various screen sizes

2

Hello, I wanted to know how to leave the components responsive to various screen sizes, eg: I have an imageview but it gets a size on the tablet, and the same size on a small cell phone, ie on the small cell phone it will get great. I would like to know how to readjust this size to become a standard on all devices.

xml of what I want to leave responsive:

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="250dp"
    android:layout_height="match_parent"
    android:layout_weight ="3"

    android:src="@drawable/splashhhh2" />

    
asked by anonymous 24.07.2014 / 07:44

2 answers

3

For this you can use the dimens.xml files in the values folders.

For example, you have an ImageView.

<ImageView
    android:layout_width="@dimen/imageview_width"
    android:layout_height="@dimen/imageview_height"
/>

In the values-mdpi folder, for example, you create the dimension.xml file and place:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="imageview_width">30dp</dimen>
    <dimen name="imageview_height">30dp</dimen>
</resources>

Then you create in the values-xlarge folder, the same file, but put other values:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="imageview_width">60dp</dimen>
    <dimen name="imageview_height">60dp</dimen>
</resources>

You can create the dimension files in various folder combinations, for example values-normal-hdpi.

Another option would be to use the weight field of the LinearLayout component. For example:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="10" >

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="7" />

</LinearLayout>

So components will always occupy the same percentages of the screen, regardless of their size.

    
24.07.2014 / 13:39
0

To make your layout responsive you will have to create different resources for the different settings you want. You can create the folders within the folder by following the following qualifiers (the most common ones):

  • Smallest Width : Android uses the smallest screen value between height and width to define the features it will use. name of the folder: layout-swdp where N and dimension a in dp. Example: layout-sw320dp, layout-sw720dp.
  • Width available : Same as the previous qualifier, but this checks only the width value of the device. folder name: layout-wdp where N is the width value of width. Example: layout-w320dp
  • Height available : Qualifier similar to the previous one, so that instead of the width of the device, the height is taken into account. folder name: layout-hdp. Example: layout-h720dp
  • Screen size : Using this qualifier, android checks the screen size and this can be small- approximately 320x426dp , normal-approximately 320x470dp large-approximately 480x640dp, xlarge-approximately 720x960dp .example: layout-normal
  • Device Orientation : This qualifier selects the folder with the required features according to the orientation of the device. port-portrait and land-landscape . Example: layout-land.
  • Screen Pixel Density (dpi) : This qualifier selects the folder with the right features after identifying the pixel density of the screen. these densities can be: ldpi-Low density-120dpi , mdpi-medium density-160dpi hdpi-high density-240dpi , xhdpi - extra high density-320dpi .

After choosing which of the qualifiers will best describe how you want to define the screens that will support you, you should create values folders using the above qualifiers and place in each of them the file dimens.xml with the dimensions that you will use within the layout files to define the width and height .

    
24.07.2014 / 21:39