osmund sadler
osmund sadler

Reputation: 1041

How do I detect screen rotation on android?

i want to detect screen rotation via native.

for a example, when touch event occured, we can detect via /dev/input/event* event device

is this possible ?

i mean, without using java. just use native method.

WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
Display disp = wm.getDefaultDisplay();

int rotation = disp.getRotation(); // Android 2.2
Log.i( "Rotation", "rotation : " + rotation );

switch ( rotation )
{
    case Surface.ROTATION_0: 
        Log.i( "Roation", "Portrait : 0" ); 
        break;

    case Surface.ROTATION_90:
        Log.i( "Roation", "Landscape : 90" );
        break;

    case Surface.ROTATION_180:
        Log.i( "Roation", "Portrait : 180" );
        break;

    case Surface.ROTATION_270:
        Log.i( "Roation", "Landscape : 270" );
        break;
}

Upvotes: 2

Views: 2126

Answers (1)

Seva Alekseyev
Seva Alekseyev

Reputation: 61351

If you're using the NativeActivity infrastructure (it's not obvious from the question), there's a callback onConfigurationChanged in struct ANativeActivityCallbacks, which is made available to you as a part of a ANativeActivity object, which is pointed to by activity within the android_app structure, which is a parameter to your android_main.

Just reset the callback on startup to your function:

void OnConfig(ANativeActivity *ac)
{
    //...
}

state->activity->callbacks->onConfigurationChanged = OnConfig;

If you are indeed using NativeActivity, please tag the question with native-activity, not just android-ndk. There's enough difference of environment to warrant an extra tag.

Upvotes: 4

Related Questions