2
I got 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 the if
, verify the rotation of the screen to display the layout correct.
However, in the folder res
, has 2 files main.xml
The principal.xml
and the principal.xml(land)
That way, I’m not sure how to call the layout when the rotation is LANDSCAPE
Code of 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>
What I’m doing wrong if I’ve 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));
}
}
for
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Configuration configuration = getResources().getConfiguration();
setContentView(R.layout.principal);
}
?
In the device and in LANDSCAPE the elements are getting one below the other and not beside as in the tab of design
Look at the 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>
Then just do setContentView(R.layout.principal); and it works?
– Carlos Rocha
I added more details at the end of the question as the elements still show wrong on (is only on) device
– Carlos Rocha
I must be doing something wrong because I cleaned up the entire LANDSCAPE layout. I recompiled and launched on the device Yes, deleted it before reinstalling, and while rotating the screen it still displays the items as if just turning the PORT layout
– Carlos Rocha
Then just do setContentView(R.layout.principal); and it works? - Yes. That’s enough. When referring to "device", what type of device are you referring to? Note that in preview editor select a Nexus 4.
– ramaral
ah yes. The device I mean is physical. A Moto G 4 Play. I’m not using emulator no, I’m playing straight on the mobile device. It seems that the screen is not being called Landscape. It seems that what happens is that the PORT is being rotated in the LAND
– Carlos Rocha
Are the screen features of the Moto G 4 the same as the Nexus 4? If they’re not, it’s natural that what you see on the Moto G 4 is different from what you see on preview of the editor.
– ramaral
Well, let me get this straight: On the Android Studio screen, it’s Nexus 4. But when it comes to compiling and opening AVD, then I choose my Moto G. Isn’t that so? Need choose bike g in Android Studio also?
– Carlos Rocha
No. The preview shows how your layout is presented in a Nexus 4, if the screen of the Moto G has different features the layout can be presented differently, See this reply
– ramaral
So. I did the following. I created a new project with an Activity. And in it, I put the 2 layout’s with only one button. In the portrait, I placed PORTRAIT in the caption of the Button and in the landscape, LANDSCAPE in the caption of the buttom. It worked correctly.However, by making these modifications to the old design, it just displays the PORT layout. It’s not calling the layout-land. However, how your explanation worked in a new and empty project. So I’ll give it as the solution! Thank you!
– Carlos Rocha