Tenho o seguinte:
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));
}
}
}
No if
, verifico a rotação da tela para exibir o layout correto.
Porém, na pasta res
, tem 2 arquivos principal.xml
O principal.xml
e o principal.xml(land)
Dessa forma, não estou sabendo como chamar o layout quando a rotação for LANDSCAPE
Código da LAND
<?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"
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>
O que estou fazendo de errado se já alterei
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));
}
}
por
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Configuration configuration = getResources().getConfiguration();
setContentView(R.layout.principal);
}
?
No dispositivo e em LANDSCAPE os elementos estão ficando um abaixo do outro e não ao lado como na aba de design
Vejam o 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>