Skip to content

Instantly share code, notes, and snippets.

@linroid
Last active November 9, 2016 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linroid/0c8086db0bcdf0abc7c1220cac4eb7da to your computer and use it in GitHub Desktop.
Save linroid/0c8086db0bcdf0abc7c1220cac4eb7da to your computer and use it in GitHub Desktop.
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