Thursday, January 3, 2013

Orientation of the application


In this article we will discuss how to develop applications for different orientations of the device. I will also show an easy way to identify the type of device, the tablet or smartphone.



Create a new project. Add string resources in the following lines:
<string name="txt_portrait">PORTRAIT</string>
       <string name="txt_landscape">LANDSCAPE</string>
Xml-file for our Activity looks like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/txt_portrait"
        /> 
</RelativeLayout>
When you run the application, we will see that the layout is displayed in portrait orientation. Turning the device, the application will change the interface, adjusting to the current orientation of the device. If you add in the method onCreate() for the current Activity information log, you will see that when you change the orientation of our Activity restarts.

To separate layouts for portrait and landscape interface, add a folder layout-land in resources. «-land» shows that these resources will be used only in landscape orientation.
Copy the layout file in the folder layout in layout-land and replace the text output for the TextView on android:text="@string/txt_landscape".
After launching the application, we will see that the application is using the correct layout for the respective orientations.

It remains to deny the application constantly restart Activity at each change of orientation.
To do this, add the following line in the manifest for the current Activity: android:configChanges="orientation|screenSize".
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.snowpard.projects.ten"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:name="org.snowpard.projects.ten.MainActivity"
            android:label="@string/app_name"
            android:configChanges="orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
And override onConfigurationChanged () in Activity the following way:
@Override
public void onConfigurationChanged(Configuration newConfig) {  
 super.onConfigurationChanged(newConfig);
 setContentView(R.layout.main);  
}
Now, after a change of orientation, we used the desired layout and does not restart Activity.

There are cases when you need to make an application that runs on smartphones only in portrait mode, and the tablets in both orientations.
This is implemented in two steps:
1. Necessary to determine the type of device: smartphone or tablet.
2. Do not allow to change the orientation of the smartphone application.
To determine the type of device, we proceed as follows. You must create in the folder res/values ​​ file defaults.xml.
<resources>   
    <bool name="isTablet">false</bool>
</resources>
Then, create a folder: values-large (for 7 "tablet) and values-xlarge (10" tablets). Copy the file defaults.xml to the new location and replace the value of the variable isTablet to true.
<resources>  
    <bool name="isTablet">true</bool>
</resources>
In our Activity add the attribute:
private boolean isTablet;
In the method onCreate() will get the value from the resources:
this.isTablet = getResources().getBoolean(R.bool.isTablet);
Create a new method that will determine whether you need to change the orientation for the application or not:
private void wasPortrait()
{
 if (!this.isTablet)
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
 else 
  setContentView(R.layout.main);
}
Method setRequestedOrientation() sets the desired orientation (in this case the portrait).
Added a call method wasPortrait() at the end of method onCreate() and in the onConfigurationChanged().
Full listing:
public class MainActivity extends Activity {

 private boolean isTablet;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  
  Log.e("MainActivity", "onCreate");
  
  this.isTablet = getResources().getBoolean(R.bool.isTablet);
  wasPortrait();
 }

 @Override
 public void onConfigurationChanged(Configuration newConfig) {  
  super.onConfigurationChanged(newConfig);
  wasPortrait();  
 }
 
 private void wasPortrait()
 {
  if (!this.isTablet)
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  else 
   setContentView(R.layout.main);
 }
}

Links

  • The source codes of this project can be downloaded here: zip

No comments:

Post a Comment