How does it work to adapt to different resolutions on Android?

Asked

Viewed 5,722 times

5

In html for example, objects can (sometimes without any special code) adapt to the size of the window, for example by placing two images of fixed size, if you shrink the window one goes to the bottom line.

But what about an Android App? Are the elements fixed? Does it vary? Do I need to make a different app for each resolution or screen size?

I do not program for Android yet, but I would like to know what the concept of Apps in this sense.

2 answers

5


Answer:

Adaptation to different resolutions of the consists of: Values for a LinearLayout that would be the Pesos(android:layout_weight).

Explanation:

You can assign the property android:layout_weight in its XML elements, it would be as if it were a weight(well to say percentage) that is distributed according to the value of this property in all the elements that are relative to its LinearLayout, would be a sum of all the Weight'which are declared in Elementos within your LinearLayout that in itself has a WeightSum which is a property that says what is the maximum value(100%) allowed to their children. By default the value of WeightSum is 1.

The property itself of WeightSum which is the total weight standard of LinearLayout would be android:weightSum=x where x would be the value of Weight (which can be decimal).

The property itself of Layout Weight which is the value of the weight of a child element of LinearLayout would be android:layout_weight=x where x would be the value of Weight (which can be decimal).

Example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:weightSum="1" <!-- aqui está o weightSum -->
    android:orientation="vertical" >
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/to" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/subject" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" <!-- aqui está o weight -->
        android:gravity="top"
        android:hint="@string/message" />
    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="@string/send" />
</LinearLayout>

Upshot:

exemplo weight

Remarks:

In the EditText of the Message you have no values in dp absolute for height(height), you only have android:layout_weight="1" that would be the weight of such an element.

If you look at all the child elements of LinearLayout, will realize that the only element that contains Weight(weight) would be the EditText of the Message, so the sum of all the Weight’s would be 1. But if there was another element with 1 of Weight as well, the total Weight would be 2 and would exceed the standard WeightSum of LinearLayout that would be 1.

But if you have one WeightSum of 10, for example, you could have 10 child elements containing android:layout_weight=1 each one, so each element would have 10% full-size.

Very important remark:

You may be wondering: Okay, but where do I let you know if the Weight is for height or width??

The answer is: This is strictly tied to Orientation of your LinearLayout that would be this property:

android:orientation="vertical"

From which you may notice that it is stated in LinearLayout example above, but if he were horizontal Weight’s would be applied horizontally only.

Reference

  • Then, using these properties, the elements could adapt to any screen size?

  • 1

    Yes. They can adapt, but an image for example cannot, because you would have to have different files for each "type" of resolution, okay? there’s another thing.

  • Her answer made me understand what I wanted, but she’s not very objective about the questions that were asked, so I’m not going to mark it as an answer yet. I believe that if you improve, it will be easier to understand to other users interested in the question.

  • 1

    Now it’s better @Joaopaulo ? :)

  • Yes, but I don’t mean to be boring and already being, your answer is more technical, and that’s great. But as I said I do not program for Android yet and was in search of the concept. What I interpreted is that there are elements that can have properties that adapt the resolution of the cell phone and others do not. And that there is no need to make several layouts for different screen sizes.

  • Exactly, there is no need to make several layouts, only one using weight works the same in all resolutions of all cell phones :) but if you do without weight will not work the same on all phones.

Show 1 more comment

0

Using Linearlayout. An observation Of how to use images on android caught already enough with this now I use this concept, does not put the size you want the component is on the screen, example. places the user field in the vertical and horizontal center. from Ae place the distance it should keep from the right or left corner of the cell phone. or how it is to establish its size according to the right and left margin of the screen and as the size of the screen changes and changes the length of the fields keeping the aesthetic example if I specify the image in size 220x90 it will have that length in all screen size,with that the android comes with 4 folder for these images, just put the images with the same name in each folder ae android specifies which is the best resolution for the screen being opened drawable -hdpi dimension 0.75 drawable-mdpi 1
drawable-xhdpi 1.5 drawable-xxhdpi 2.0

Imagebutton Imageview use the property android:src="@drawable/filename"

Browser other questions tagged

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