Sniper
Sniper

Reputation: 2452

Check Device Orientation in Android

I want to create a new activity on the orientation change of the device without changing the current activity orientation. Is there any way to do this?

Additional Info:

My total applications default orientation is Portrate. I have to open second activity when the device is in Landscape. But the first activity should be in Portrate .

Upvotes: 0

Views: 675

Answers (5)

Sniper
Sniper

Reputation: 2452

Thanks all for your help. Finally I done this by using SensorEventListener.

OnCreate()

    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);

Listener

    private SensorEventListener mySensorEventListener = new SensorEventListener() {

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    public void onSensorChanged(SensorEvent event) {
        // angle between the magnetic north directio
        // 0=North, 90=East, 180=South, 270=West
        float azimuth = event.values[1];
        // compassView.updateData(azimuth);

        if ((azimuth < 100 && azimuth > 60)
                || (azimuth > -100 && azimuth < -60)) {
            if (EasterVal != 0) {
                if (!Modules.LoanType.equalsIgnoreCase("Ballcash")) {
                    Intent in = new Intent(Buyeroutput.this, Esteregg.class);
                    startActivity(in);
                    EasterVal = 0;
                }
            }
        } else {
            EasterVal = 1;
        }
    }
};

protected void onDestroy() {
    super.onDestroy();
    if (sensor != null) {
        sensorManager.unregisterListener(mySensorEventListener);
    }
}

Upvotes: 0

Marcin S.
Marcin S.

Reputation: 11191

You could also programmatically detect the current orientation of your activity and run an intent to your second activity whenever your device is in landscape mode. Here is a code to accomplish it:

WindowManager wm = getWindowManager();
Display d = wm.getDefaultDisplay();
if(d.getWidth() > d.getHeight())
{
    // landscape mode
    Intent i = new Intent(FirstActivity.this, SecondActivity.class);
    startActivity(i);
}

Upvotes: 1

Parag Chauhan
Parag Chauhan

Reputation: 35936

write in Menifest

for first Activity

  <activity  android:name="firstActivity" android:screenOrientation="portrait"></activity>

for second Activity

   <activity  android:name="secondActivity" android:screenOrientation="landscape"></activity>

Upvotes: 1

Philip Sheard
Philip Sheard

Reputation: 5815

Well of course you can. You just declare in your manifest that your first activity will handle orientation changes. Then add an onConfigurationChanged function that starts your second activity.

Upvotes: 2

Akshay
Akshay

Reputation: 2534

You can use android:screenOrienttation="Landscape/Portrate" in your AndroidMenifest.xml for the perticular activity.So that rest of activity will remain in there default view and the perticure activity will force to display in specific view as you mension.

Hope this help you.

Upvotes: 2

Related Questions