How to adapt an Android app to various screen sizes?

Asked

Viewed 4,746 times

2

For example, for example... I want the app on Android to be presented equally both on screens from 5" to 3"... And how do I apply a "scrolling" layout, how to add a scroll to the Android layout?

  • And like this... I’m running the app on a device with a screen of 5", and in my Activity I have certain elements with a marginTop value... And when I go to run this same Activity in a device of 3" it ends up exceeding the limits of the screen... You can set a specific Activity for devices with this screen size?

2 answers

3

Use wrap_content and match_parent:

To ensure that your layout is flexible and adapts to different screen sizes, you should use "wrap_content" and "match_parent" for the width and height of some display components. If you use "wrap_content", the view width or height will be set to the minimum size needed to adjust to the content inside, while "match_parent" (also known as "fill_parent" prior to API 8) causes the component to expand to match the view size(Parent or parent).

Example of the use of "wrap_content" and "match_parent":

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout android:layout_width="match_parent" 
                  android:id="@+id/linearLayout1"  
                  android:gravity="center"
                  android:layout_height="50dp">
        <ImageView android:id="@+id/imageView1" 
                   android:layout_height="wrap_content"
                   android:layout_width="wrap_content"
                   android:src="@drawable/logo"
                   android:paddingRight="30dp"
                   android:layout_gravity="left"
                   android:layout_weight="0" />
        <View android:layout_height="wrap_content" 
              android:id="@+id/view1"
              android:layout_width="wrap_content"
              android:layout_weight="1" />
        <Button android:id="@+id/categorybutton"
                android:background="@drawable/button_bg"
                android:layout_height="match_parent"
                android:layout_weight="0"
                android:layout_width="120dp"
                style="@style/CategoryButtonStyle"/>
    </LinearLayout>

    <fragment android:id="@+id/headlines" 
              android:layout_height="fill_parent"
              android:name="com.example.android.newsreader.HeadlinesFragment"
              android:layout_width="match_parent" />
</LinearLayout> 

Upshot:

inserir a descrição da imagem aqui

Read more...

  • Great answer, but it wouldn’t be nice to talk about "match_constraint" too?

1

I believe that in most cases declare the XML elements using wrap_content and match_parent works well.

However, to handle different situations with greater freedom, Android allows the declaration of elements based on screen size, density and orientation. That sure can make all the difference.

To use several different files, simply declare them with an identifier. For example: if you declare res/layout/main_activity.xml this feature will be the default, but when including the file res/layout-sw600dp/main_activity.xml, the system will load this layout if the shortest width (smallest width) is 600dp.

This principle is maintained for any other qualifier, such as res/layout-land/main_activity.xml which will be charged if the apparatus is in landscape mode.

A list of qualifiers can be found here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.