Apr 25, 2014

Set/get orientation programmatically

To set orientation programmatically with Java code, by calling setRequestedOrientation() method with parameter of ActivityInfo.SCREEN_ORIENTATION_PORTRAIT or ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE. To get current requested orientation, call getRequestedOrientation(), it will return either the orientation requested in its component's manifest, or the last requested orientation given to setRequestedOrientation(int)

Example:
Set orientation programmatically
Set orientation programmatically

package com.example.androidorientation;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
 
 Button btnSetPortrait, btnSetLandscape;
 TextView textOrientation;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  textOrientation = (TextView)findViewById(R.id.orientation);
  String currentOrientation = getCurrentOrientation();
  textOrientation.setText(currentOrientation);
  
  btnSetPortrait = (Button)findViewById(R.id.setPortrait);
  btnSetPortrait.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
   }});
  
  btnSetLandscape = (Button)findViewById(R.id.setLandscape);
  btnSetLandscape.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
   }});
  
  Toast.makeText(MainActivity.this, "onCreate()", Toast.LENGTH_LONG).show();
  
 }
 
 @Override
 protected void onDestroy() {
  super.onDestroy();
  Toast.makeText(MainActivity.this, "onDestroy()", Toast.LENGTH_LONG).show();
 }
 
 private String getCurrentOrientation(){
  String ori;
  
  //get the current orientation
  int currentOrientation = getRequestedOrientation();
  switch(currentOrientation){
  case ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED:
   ori = "SCREEN_ORIENTATION_UNSPECIFIED";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
   ori = "SCREEN_ORIENTATION_LANDSCAPE";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
   ori = "SCREEN_ORIENTATION_PORTRAIT";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_USER:
   ori = "SCREEN_ORIENTATION_USER";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_BEHIND:
   ori = "SCREEN_ORIENTATION_BEHIND";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_SENSOR:
   ori = "SCREEN_ORIENTATION_SENSOR";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_NOSENSOR:
   ori = "SCREEN_ORIENTATION_NOSENSOR";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE:
   ori = "SCREEN_ORIENTATION_SENSOR_LANDSCAPE";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT:
   ori = "SCREEN_ORIENTATION_SENSOR_PORTRAIT";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
   ori = "REEN_ORIENTATION_REVERSE_LANDSCAPE";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
   ori = "SCREEN_ORIENTATION_REVERSE_PORTRAIT";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR:
   ori = "SCREEN_ORIENTATION_FULL_SENSOR";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE:
   ori = "SCREEN_ORIENTATION_USER_LANDSCAPE";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT:
   ori = "SCREEN_ORIENTATION_USER_PORTRAIT";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_FULL_USER:
   ori = "SCREEN_ORIENTATION_FULL_USER";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_LOCKED:
   ori = "SCREEN_ORIENTATION_LOCKED";
   break;
  default:
   ori = "unknown!";
  }

  return ori;
 }

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidorientation.MainActivity"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />
    <Button
        android:id="@+id/setPortrait"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Portrait" />
    <Button
        android:id="@+id/setLandscape"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Landscape" />
    <TextView
        android:id="@+id/orientation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Apr 18, 2014

Three Flaws in Software Design

In the video series, Jeremy Walker & Max Kanat-Alexander discuss the Three Flaw of Software Design, from Max's book Code Simplicity: The Fundamentals of Software.




Code Simplicity: The Fundamentals of Software

Good software development results in simple code. Unfortunately, much of the code existing in the world today is far too complex. This concise guide helps you understand the fundamentals of good software development through universal laws--principles you can apply to any programming language or project from here to eternity.

Whether you're a junior programmer, senior software engineer, or non-technical manager, you'll learn how to create a sound plan for your software project, and make better decisions about the pattern and structure of your system.

  • Learn what differentiates great programmers from poor programmers
  • Understand the ultimate purpose of software and the goals of good software design
  • Determine the value of your decisions now and in the future
  • Examine real-world examples that demonstrate how a system changes over time
  • Learn to allow for the most change in the environment with the least change in the software
  • Make easier changes in the future by keeping your code simpler now
  • Understand why to write tests and what tests to write

About the Author
Max Kanat-Alexander, Chief Architect of the open-source Bugzilla Project, Google Software Engineer, and writer, has been fixing computers since he was eight years old and writing software since he was fourteen. He is the author of http://www.codesimplicity.com/ and http://www.fedorafaq.com, and is currently living in Northern California.

Apr 15, 2014

Android JavaFX NFC demo

This video shows a JavaFX Android application using NFC.

Apr 8, 2014

Android 4.4: Step Sensors

Walk through two ways to track steps in Android Kit Kat with the new hardware step sensors.

Sample code: http://developer.android.com/samples/BatchStepSensor


Android 4.4: Step Sensors

Infolinks In Text Ads