Problems with the layout of the application in several cell phones

Asked

Viewed 1,380 times

3

I have an app that I made and emuiled using my cell phone lg G2 mini, when emulo it gets all straight, the form the way I designed in its proper corner. When I install the same apk on other phones, the application loses the layout by getting a button over an image, the messy form. Someone knows what to do to make the application work as designed in my layout for all cell phones models?

erro

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:text="Endereço: Rua João Henrique da Silva, 11 - Pajuçara - "
    android:id="@+id/textView8"
    android:textSize="12dp"
    android:textAlignment="center"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:id="@+id/nome1"
    android:textColorHint="#9b9a9a"
    android:hint="*Nome:"
    android:textSize="14dp"
    android:textColorLink="#050000"
    android:backgroundTint="#020000"
    android:textColor="#050000"
    android:layout_above="@+id/email"
    android:layout_alignLeft="@+id/msg"
    android:layout_alignRight="@+id/msg"
    android:layout_alignEnd="@+id/msg" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColorHint="#9b9a9a"
    android:inputType="textPostalAddress"
    android:ems="10"
    android:id="@+id/email"
    android:hint="*E-mail:"
    android:textSize="14dp"
    android:textColorLink="#050000"
    android:backgroundTint="#020000"
    android:textColor="#050000"
    android:layout_above="@+id/msg"
    android:layout_alignLeft="@+id/msg"
    android:layout_alignStart="@+id/msg"
    android:layout_marginBottom="36dp"
    android:layout_alignRight="@+id/msg"
    android:layout_alignEnd="@+id/msg" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColorHint="#9b9a9a"
    android:inputType="phone"
    android:ems="10"
    android:id="@+id/telefone"
    android:hint="Telefone:"
    android:textSize="14dp"
    android:textColorLink="#050000"
    android:backgroundTint="#020000"
    android:textColor="#050000"
    android:layout_above="@+id/nome1"
    android:layout_alignLeft="@+id/msg"
    android:layout_alignRight="@+id/msg"
    android:layout_alignEnd="@+id/msg" />

<Button
    android:layout_width="85dp"
    android:layout_height="40dp"
    android:text="Enviar"
    android:id="@+id/send"
    android:layout_above="@+id/textView8"
    android:layout_alignRight="@+id/textView8"
    android:layout_alignEnd="@+id/textView8" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColorHint="#9b9a9a"
    android:inputType="textPersonName"
    android:ems="10"
    android:id="@+id/endereco"
    android:hint="Endereço:"
    android:textSize="14dp"
    android:textColorLink="#050000"
    android:backgroundTint="#020000"
    android:textColor="#050000"
    android:layout_above="@+id/telefone"
    android:layout_alignLeft="@+id/msg"
    android:layout_alignRight="@+id/msg"
    android:layout_alignEnd="@+id/msg" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="120dp"
    android:gravity="top"
    android:inputType="textMultiLine"
    android:lines="5"
    android:ems="10"
    android:id="@+id/msg"
    android:hint="*Mensagem"
    android:textSize="14dp"
    android:textColorHint="#9b9a9a"
    android:layout_above="@+id/send"
    android:layout_alignRight="@+id/send"
    android:layout_alignEnd="@+id/send"
    android:layout_alignLeft="@+id/textView8"
    android:layout_alignStart="@+id/textView8"
    android:backgroundTint="#020000"
    android:textColor="#050000"
    android:textColorLink="#050000" />

<ImageView
    android:layout_width="250dp"
    android:layout_height="150dp"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/lg"
    android:layout_above="@+id/spinner" />

<Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/spinner"
    android:spinnerMode="dropdown"
    android:visibility="visible"
    android:theme="@style/Animation.AppCompat.DropDownUp"
    android:background="@color/colorFABPressed"
    android:layout_above="@+id/endereco"
    android:layout_alignRight="@+id/send"
    android:layout_alignEnd="@+id/send"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Agendar para:"
    android:id="@+id/textView10"
    android:textColor="#010101"
    android:textSize="14dp"
    android:layout_above="@+id/endereco"
    android:layout_alignLeft="@+id/imageView"
    android:layout_alignStart="@+id/imageView"
    android:layout_marginLeft="53dp"
    android:layout_marginStart="53dp" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageButton2"
    android:src="@drawable/previe"
    android:background="#00000000"
    android:layout_above="@+id/msg"
    android:layout_toEndOf="@+id/imageView"
    android:layout_toRightOf="@+id/imageView" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Capturar imagem"
    android:id="@+id/textView16"
    android:textColor="#010101"
    android:textSize="14dp"
    android:layout_above="@+id/msg"
    android:layout_toLeftOf="@+id/send"
    android:layout_toStartOf="@+id/send" />

I design my xmls already checking how it would look on each screen type. As image below inserir a descrição da imagem aqui

and here is normal

  • You could show how you designed your layout?

  • 1

    @Carlos, see if this link helps you: https://developer.android.com/training/multiscreen/screensizes.html

  • this occurs due to the different screen sizes you need to emulate in different cell phone sizes https://developer.android.com/studio/index.html on the above web site you will find how to do this

  • I realized that even the photo taking function did not work on some phones.. It closes the apk when the photo is taken. Does anyone have a more accurate solution on this? It is necessary to emulate for various types of sizes?

  • Julio my xml are all as relative and with wrap_content but still ta bugada

  • @Carlosdiego edit the question and put the layout so we can check slowly?

  • Matthew as the image above, my apk is running on some phones, as example my lg2mini and lg4 ran all good layouts, now in mobile phones with lower resolution get all distorted, messy form, buttons out of the corner, hidden text. I would like a more direct solution, because the ones that will appear left me floating a little.

  • 2

    @Carlosdiego is running on some phones correctly does not mean that the layout is well designed (not, of course). Without the layout file it is difficult to help you. It does not have a direct answer. Android is laborious. The nut solution would be you create a layout for ldpi, another for mdpi, hdpi etc.

  • Matthew I attached my xml file. It is possible to talk via chat?

  • @Carlosdiego, I’ll take my time. But, I looked at something and did not like: never use t layout_toLeftOf along with android:layout_toStartOf same thing for the for Right and End. You will have compatibility problems. My suggestion: use the latest: start and end only. Take the left and right and test to see. Your Android Lint Android Studio did not complain about it?

  • I’ll do it, I haven’t installed the lint yet

Show 6 more comments

1 answer

6


First Option - What you will have to do, is create a screen for each type of orientation Portrait(Portrait - Standing! ) and one for Land(Landscape - From the side).

Example:

In your directory /res/ you can have two folders, example:

  • /res/layout -> Screen in Standard Mode "Portrait".
  • /res/layout-land -> Canvas in Landscape Mode.

This is the file fragment_test.xml of the briefcase res/layout and is for standard guidance Portrait(Portrait), and serves for devices with lower resolution -> Smartphone:

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".testFragment">

        <ScrollView
            android:background="@android:color/white"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <FrameLayout
            android:background="@android:color/white"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="08dp"
            android:layout_marginRight="08dp"
            android:paddingTop="08dp"
            android:paddingBottom="08dp">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/nome1"
            android:textColorHint="#9b9a9a"
            android:hint="*Nome:"
            android:textSize="14sp"
            android:textColorLink="#050000"
            android:backgroundTint="#020000"
            android:textColor="#050000"
            android:layout_gravity="top|left"
            android:layout_marginTop="170dp" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton2"
            android:src="@drawable/previe"
            android:background="#00000000"
            android:layout_gravity="top|center|right"
            android:layout_marginTop="390dp"
            android:layout_marginRight="60dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Agendar para:"
            android:id="@+id/textView10"
            android:textColor="#010101"
            android:textSize="14sp"
            android:layout_marginLeft="20dp"
            android:layout_gravity="top|left|center_vertical"
            android:layout_marginTop="120dp" />

            <EditText
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:textColorHint="#9b9a9a"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/endereco"
            android:hint="Endereço:"
            android:textSize="14sp"
            android:textColorLink="#050000"
            android:backgroundTint="#020000"
            android:textColor="#050000"
            android:layout_above="@+id/telefone"
            android:layout_alignLeft="@+id/msg"
            android:layout_alignRight="@+id/msg"
            android:layout_alignEnd="@+id/msg"
            android:layout_gravity="top|left|center_vertical"
            android:layout_marginTop="320dp" />

        <Button
            android:layout_width="85dp"
            android:layout_height="40dp"
            android:text="Enviar"
            android:id="@+id/send"
            android:layout_gravity="top|bottom|right"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="500dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black"
            android:text="Endereço: Rua , Numero - Localidade"
            android:id="@+id/textView8"
            android:textSize="12sp"
            android:textAlignment="center"
            android:layout_gravity="top|bottom|center"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="550dp" />

            <EditText
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:textColorHint="#9b9a9a"
            android:inputType="phone"
            android:ems="10"
            android:id="@+id/telefone"
            android:hint="Telefone:"
            android:textSize="14sp"
            android:textColorLink="#050000"
            android:backgroundTint="#020000"
            android:textColor="#050000"
            android:layout_gravity="top|left|center_vertical"
            android:layout_marginTop="270dp" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="82dp"
                android:gravity="top"
                android:inputType="textMultiLine"
                android:lines="5"
                android:ems="10"
                android:id="@+id/msg"
                android:hint="*Mensagem"
                android:textSize="14sp"
                android:textColorHint="#9b9a9a"
                android:backgroundTint="#020000"
                android:textColor="#050000"
                android:textColorLink="#050000"
                android:layout_gravity="top|center"
                android:layout_marginTop="410dp" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:textColorHint="#9b9a9a"
                android:inputType="textPostalAddress"
                android:ems="10"
                android:id="@+id/email"
                android:hint="*E-mail:"
                android:textSize="14sp"
                android:textColorLink="#050000"
                android:backgroundTint="#020000"
                android:textColor="#050000"
                android:layout_gravity="left|top"
                android:layout_marginTop="220dp" />

            <Spinner
                android:layout_width="190dp"
                android:layout_height="wrap_content"
                android:id="@+id/spinner"
                android:spinnerMode="dropdown"
                android:visibility="visible"
                android:theme="@style/Animation.AppCompat.DropDownUp"
                android:background="@color/colorFABPressed"
                android:layout_above="@+id/endereco"
                android:layout_alignRight="@+id/send"
                android:layout_alignEnd="@+id/send"
                android:layout_gravity="center_horizontal|top"
                android:layout_marginTop="101dp"
                android:layout_marginLeft="80dp" />

            <ImageView
                android:layout_width="410dp"
                android:layout_height="100dp"
                android:id="@+id/imageView"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:src="@drawable/lg"
                android:layout_above="@+id/spinner"
                android:layout_gravity="center_horizontal|top" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Capturar imagem"
                android:id="@+id/textView16"
                android:textColor="#010101"
                android:textSize="14sp"
                android:layout_gravity="top|center|right"
                android:layout_marginTop="390dp"
                android:layout_marginRight="120dp" />

        </FrameLayout>
     </ScrollView>
   </RelativeLayout>

Obs: Your texts are not externalized, ex: android:text="Capturar imagem", what is not recommended by Google, outsource them through the String Name in the directory /values/ in strings.xml, in that way:

<resources>
       <string name="capturar">Capturar imagem</string>
</resources>

And on your screen you change the code snippet to android:text="@string/capturar".

Upshot:

inserir a descrição da imagem aqui


This is the file fragment_test.xml(land) of the briefcase res/layout-land and is for guidance Land(Landscape), and that adapts best to devices 7, 8 and 10 inches -> Tablets:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".testFragment">

    <ScrollView
        android:background="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <FrameLayout
        android:background="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="08dp"
        android:layout_marginRight="08dp"
        android:paddingTop="08dp"
        android:paddingBottom="08dp">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/nome1"
            android:textColorHint="#9b9a9a"
            android:hint="*Nome:"
            android:textSize="14sp"
            android:textColorLink="#050000"
            android:backgroundTint="#020000"
            android:textColor="#050000"
            android:layout_gravity="top|left"
            android:layout_marginTop="170dp" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton2"
            android:src="@drawable/previe"
            android:background="#00000000"
            android:layout_gravity="top|center|right"
            android:layout_marginTop="390dp"
            android:layout_marginRight="60dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Agendar para:"
            android:id="@+id/textView10"
            android:textColor="#010101"
            android:textSize="14sp"
            android:layout_marginLeft="60dp"
            android:layout_gravity="top|left|center_vertical"
            android:layout_marginTop="120dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:textColorHint="#9b9a9a"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/endereco"
            android:hint="Endereço:"
            android:textSize="14sp"
            android:textColorLink="#050000"
            android:backgroundTint="#020000"
            android:textColor="#050000"
            android:layout_above="@+id/telefone"
            android:layout_alignLeft="@+id/msg"
            android:layout_alignRight="@+id/msg"
            android:layout_alignEnd="@+id/msg"
            android:layout_gravity="top|left|center_vertical"
            android:layout_marginTop="320dp" />

        <Button
            android:layout_width="85dp"
            android:layout_height="40dp"
            android:text="Enviar"
            android:id="@+id/send"
            android:layout_gravity="top|bottom|right"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="500dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black"
            android:text="Endereço: Rua , Numero - Localidade"
            android:id="@+id/textView8"
            android:textSize="12sp"
            android:textAlignment="center"
            android:layout_gravity="top|bottom|center"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="550dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:textColorHint="#9b9a9a"
            android:inputType="phone"
            android:ems="10"
            android:id="@+id/telefone"
            android:hint="Telefone:"
            android:textSize="14sp"
            android:textColorLink="#050000"
            android:backgroundTint="#020000"
            android:textColor="#050000"
            android:layout_gravity="top|left|center_vertical"
            android:layout_marginTop="270dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="82dp"
            android:gravity="top"
            android:inputType="textMultiLine"
            android:lines="5"
            android:ems="10"
            android:id="@+id/msg"
            android:hint="*Mensagem"
            android:textSize="14sp"
            android:textColorHint="#9b9a9a"
            android:backgroundTint="#020000"
            android:textColor="#050000"
            android:textColorLink="#050000"
            android:layout_gravity="top|center"
            android:layout_marginTop="410dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:textColorHint="#9b9a9a"
            android:inputType="textPostalAddress"
            android:ems="10"
            android:id="@+id/email"
            android:hint="*E-mail:"
            android:textSize="14sp"
            android:textColorLink="#050000"
            android:backgroundTint="#020000"
            android:textColor="#050000"
            android:layout_gravity="left|top"
            android:layout_marginTop="220dp" />

        <Spinner
            android:layout_width="290dp"
            android:layout_height="wrap_content"
            android:id="@+id/spinner"
            android:spinnerMode="dropdown"
            android:visibility="visible"
            android:theme="@style/Animation.AppCompat.DropDownUp"
            android:background="@color/colorFABPressed"
            android:layout_above="@+id/endereco"
            android:layout_alignRight="@+id/send"
            android:layout_alignEnd="@+id/send"
            android:layout_gravity="center_horizontal|top"
            android:layout_marginTop="101dp"
            android:layout_marginLeft="80dp" />

        <ImageView
            android:layout_width="410dp"
            android:layout_height="100dp"
            android:id="@+id/imageView"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/lg"
            android:layout_above="@+id/spinner"
            android:layout_gravity="center_horizontal|top" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Capturar imagem"
            android:id="@+id/textView16"
            android:textColor="#010101"
            android:textSize="14sp"
            android:layout_gravity="top|center|right"
            android:layout_marginTop="390dp"
            android:layout_marginRight="120dp" />

    </FrameLayout>
    </ScrollView>
</RelativeLayout>

Obs: Your texts are not externalized, ex: android:text="Capturar imagem", what is not recommended by Google, outsource them through the String Name in the directory /values/ in strings.xml, in that way:

<resources>
       <string name="capturar">Capturar imagem</string>
</resources>

And on your screen you change the code snippet to android:text="@string/capturar".

Upshot:

inserir a descrição da imagem aqui

Note: I recommend that whenever possible test your applications on real devices with different resolutions or in virtual machines with different settings and resolutions, to see if everything is correct, thus ensuring the quality of your applications.


Second Option - If you don’t know how to create a screen for each type of orientation, you can make your Activities work only with guidance Portrait in the archive Androidmanifest.xml, this way your layout will always be "correct", since your view will always be in orientation Portrait(Portrait), but doing so will not be allowed to turn the screen of your application, which is not a good practice, but at least solves the problem, as in this example:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="br.com.novoapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:screenOrientation="portrait"
            android:name=".SplashScreen"
            android:label="@string/app_name" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
        </activity>
    </application>
</manifest>

See above the Activity(activity) -> .SplashScreen is started in Portrait orientation(android:screenOrientation="portrait"), logically because it is a Splashscreen screen, now you can also make your .MainActivity work with in orienteering portrait and stay that way, not allowing you to turn the screen when the user turns the phone sideways, which is not good practice, but solves.

Just add to your .MainActivity the stretch android:screenOrientation="portrait", in that way:

            <activity
                android:screenOrientation="portrait"
                android:name=".MainActivity"
                android:label="@string/app_name"
                android:theme="@style/AppTheme.NoActionBar">
            </activity>

Obs: Remembering that your screen activity_main.xml that is part of their Activity -> .MainActivity who is in orientation Portrait shouldn’t be bursting Layout on no device, so that the result in orientation Portrait(Portrait) Stay Standard on All Devices.


I hope I’ve helped!

Browser other questions tagged

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