Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Screen rotation, ACCELEROMETER_ROTATION

am trying to make my Android application rotate as soon as it run and fix the rotation, i have tried a wide range of method like : Settings.System.ACCELEROMETER_ROTATION it doesn't make it rotate, but it just enable the rotate feature, i need your help thank you in advance (^_^). here is my code :

`

TableRow tableRow;
Time HourProgram;
RotateAnimation r;
int H = 12000, M = 1200;
int averageConsultationTime = 30;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.table);
    setAutoOrientationEnabled(this, true);

}

public static void setAutoOrientationEnabled(Context context,
        boolean enabled) {
    Log.i("OK", "setAutoOrientationEnabledOK");
    Settings.System.putInt(context.getContentResolver(),
            Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}

public static boolean getRotationScreenFromSettingsIsEnabled(Context context) {

    int result = 0;
    try {
        result = Settings.System.getInt(context.getContentResolver(),
                Settings.System.ACCELEROMETER_ROTATION);
    } catch (Settings.SettingNotFoundException e) {
        e.printStackTrace();
    }

    return result == 1;
}

`

like image 518
Abderazak Amiar Avatar asked Nov 04 '25 20:11

Abderazak Amiar


2 Answers

add this code in onCreate in main Activity :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
  }
like image 172
mehd azizi Avatar answered Nov 06 '25 21:11

mehd azizi


You can just add this attribute to your activity in the AndroidManifest:

android:screenOrientation="landscape"

You can find more information about Android activities attributes here.

like image 26
Esdras Lopez Avatar answered Nov 06 '25 22:11

Esdras Lopez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!