Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Orentation Changes

I have a simple Activity called SingleTouchTest to make sense of screen touches. What is strange is that SingleTouchTest starts in whatever orientation I'm in but rotating the device does not result in screen rotation.

My test device is an Acer A100 running Android 4.0.3.

The main Activity contains a ListView that navigates to my test Activityes, including SingleTouchTest. I can run SingleTouchTest (full code below) without problems except rotation. In AndroidManifest.xml (full code below) I have tried every combination of

android:configChanges="keyboard|keyboardHidden|orientation" android:screenOrientation="unspecified"

and it does not auto rotate. I even removed the onConfigurationChanged() method from SingleTouchTest and nothing happens.

Full code of AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest 
    package="com.Bespoke.AndroidBasics"
    android:versionCode="1"
    android:versionName="1.0"
    android:installLocation="preferExternal" xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="Android Basics">
        <activity
            android:name=".AndroidBasicsStarter"
            android:label="Android Basics"
            android:screenOrientation="unspecified">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".LifeCycleTest"
            android:label="Life Cycle Test"
            android:configChanges="keyboard|keyboardHidden|orientation" android:screenOrientation="unspecified"/>
        <activity
            android:name=".SingleTouchTest"
            android:label="Single Touch Test"
            android:configChanges="keyboard|keyboardHidden|orientation" android:screenOrientation="unspecified"/>
        <activity
            android:name=".MultiTouchTest"
            android:label="Single Touch Test"
            android:configChanges="keyboard|keyboardHidden|orientation" android:screenOrientation="unspecified"/>
        <activity
            android:name=".KeyTest"
            android:label="Key Test" android:screenOrientation="unspecified"/>
    </application>

</manifest>

Full code of SingleTouchTest:

package com.Bespoke.AndroidBasics;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class SingleTouchTest extends Activity
                             implements OnTouchListener {

    StringBuilder builder = new StringBuilder();
    TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        textView = new TextView(this);
        textView.setText("Touch and drag (one finger only!)");
        textView.setOnTouchListener(this);
        this.setContentView(textView);
    }

    public boolean onTouch(View v, MotionEvent event) {
        builder.setLength(0);  // clear the builder
        switch(event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            builder.append("down, ");
            break;
        case MotionEvent.ACTION_MOVE:
            builder.append("move, ");
            break;
        case MotionEvent.ACTION_CANCEL:
            builder.append("cancel, ");
            break;
        case MotionEvent.ACTION_UP:
            builder.append("up, ");
            break;
        }
        builder.append(event.getX());
        builder.append(", ");
        builder.append(event.getY());
        String text = builder.toString();
        Log.d("TouchTest", text);
        textView.setText(text);
        return true;  // Consume the event, if false super.onTouch superceeds us
    }

     @Override
     public void onConfigurationChanged(Configuration  newConfig) {
       super.onConfigurationChanged(newConfig);

     }

}
like image 645
uMinded Avatar asked Jun 11 '12 02:06

uMinded


3 Answers

since you are using

 android:screenOrientation="unspecified"

for each activity and it is defined according to google as

The default value. The system chooses the orientation. The policy it uses, and therefore the choices made in specific contexts, may differ from device to device.

Which makes me think that the device is declaring the desired orientation for some reason. Maybe try switching the screenorientation to

android:screenOrientation="fullSensor"

that should take it out of the specific devices hands.

like image 56
MikeIsrael Avatar answered Oct 01 '22 09:10

MikeIsrael


Try this:

android:screenOrientation="fullSensor"

The orientation is determined by the device orientation sensor. The orientation of the display depends on how the user is holding the device. This should be what you want. Also for more options check out this link-Activity Element.

like image 44
user1092042 Avatar answered Oct 01 '22 09:10

user1092042


In your AndroidManifest, get rid of |orientation. The reason for this is as follows:

android:configChanges allows you to handle specific configuration changes manually. Since you are not telling your app to rotate manually (via code inside onConfigurationChanged()), it is not doing anything at all. To allow your application to use rotation automatically, you must tell the Manifest that you are not handling orientation manually.

The documentation explaining this is here at the Android Developers Site.

like image 34
Fuzzical Logic Avatar answered Oct 01 '22 11:10

Fuzzical Logic