Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Android Orientation Test
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="portrait"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="landscape"/>
</LinearLayout>
package com.linroid.androidorientation;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import hugo.weaving.DebugLog;
import timber.log.Timber;
public class MainActivity extends Activity {
@DebugLog
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree());
}
setContentView(R.layout.activity_main);
printOrientation();
}
@DebugLog
@Override
protected void onStart() {
super.onStart();
printOrientation();
}
@DebugLog
@Override
protected void onResume() {
super.onResume();
printOrientation();
}
@DebugLog
@Override
protected void onPause() {
super.onPause();
printOrientation();
}
@DebugLog
@Override
protected void onStop() {
super.onStop();
printOrientation();
}
@DebugLog
@Override
protected void onDestroy() {
super.onDestroy();
printOrientation();
}
private void printOrientation() {
Timber.d("getRequestedOrientation: %s", desOrientation(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT));
Timber.d("Configuration.orientation: %s", desOrientation(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT));
Timber.d("Layout View: %s", desOrientation(LayoutInflater.from(this).inflate(R.layout.layout_info, null)));
}
private String desOrientation(boolean isPort) {
if (isPort) {
return "portrait";
} else {
return "landscape";
}
}
private String desOrientation(View view) {
TextView label = (TextView) view.findViewById(R.id.label);
return label.getText().toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment