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:
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:
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!
You could show how you designed your layout?
– Thiago Luiz Domacoski
@Carlos, see if this link helps you: https://developer.android.com/training/multiscreen/screensizes.html
– Júlio Neto
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
– Alan Darold
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?
– Carlos Diego
Julio my xml are all as relative and with wrap_content but still ta bugada
– Carlos Diego
@Carlosdiego edit the question and put the layout so we can check slowly?
– Mateus
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.
– Carlos Diego
@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.
– Mateus
Matthew I attached my xml file. It is possible to talk via chat?
– Carlos Diego
@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?
– Mateus
I’ll do it, I haven’t installed the lint yet
– Carlos Diego