Different layout for portraint and landscape

2

I have the following:

package carcleo.com.cadastro;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;

public class Principal extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Configuration configuration = getResources().getConfiguration();

        if (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE){
            setContentView(R.layout.principal);
        }else{
            setContentView(R.layout.principal(land));
        }

    }
}

In%%, I check the rotation of the screen to display the correct layout .

However, in the folder if , you have 2 files main.xml

The res and principal.xml

That way, I'm not sure how to call the layout when the rotation is LANDSCAPE

LANDSCREEN

LANDcode

<?xmlversion="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"
    android:orientation="horizontal"
    android:background="@drawable/gradient"
    android:padding="16dp"
    tools:context=".Principal">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="249dp"
        android:layout_height="134dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginBottom="0dp"
        android:src="@drawable/login" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="31dp"
        android:background="#11000000"
        android:drawableLeft="@drawable/ic_action_user"
        android:ems="10"
        android:hint="Usuário"
        android:inputType="textPersonName"
        android:textSize="16dp" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_alignStart="@+id/editText2"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="165dp"
        android:background="#11000000"
        android:drawableLeft="@drawable/ic_action_senha"
        android:ems="10"
        android:hint="Senha"
        android:inputType="textPassword" />

    <android.support.v7.widget.CardView
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:layout_alignStart="@+id/imageView"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="106dp"
        app:cardBackgroundColor="@color/colorAccent"
        app:cardCornerRadius="25dp"
        app:cardElevation="10dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="Login"
                android:textColor="#FFF"
                android:textSize="18dp" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="414dp"
        android:layout_marginRight="414dp"
        android:layout_marginBottom="45dp"
        android:text="Novo Cadastro" />

</RelativeLayout>

What am I doing wrong if I have already changed

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Configuration configuration = getResources().getConfiguration();

            if (configuration.orientation == 
Configuration.ORIENTATION_LANDSCAPE){
                setContentView(R.layout.principal);
            }else{
                setContentView(R.layout.principal(land));
            }

        }

by

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Configuration configuration = getResources().getConfiguration();

        setContentView(R.layout.principal);

    }

?

On the device and LANDSCAPE the elements are getting one below the other and not on the side as in the design

See AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Principal">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Cadastro" />
        <activity android:name=".Resposta"></activity>
    </application>

</manifest>
    
asked by anonymous 19.11.2018 / 23:41

1 answer

1

You do not need to do this verification.

The Android system will automatically load the existing layout into the land folder when the device is rotated to the landscape orientation.

    
20.11.2018 / 10:46