13

I need to detect an orientation of an device, for that I have written a code below. But I am unable get orientation.This is my oncreate() in MainActivity... If i need to change anything please let me know

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    mOrientationListener = new OrientationEventListener(getApplicationContext()) 
    {
        @Override
        public void onOrientationChanged(int orientation) 
        {
            if(orientation == 0)
            {
                setContentView(R.layout.activity_main_portrait);
            }
            else
            {
                setContentView(R.layout.activity_main_landscape);
            }
        }
    };
    if (mOrientationListener.canDetectOrientation()) 
    {          
        mOrientationListener.enable();
    }

    mImgPreview = (ImageView) findViewById(R.id.imgpreview);
    mVideoPreview = (ImageView) findViewById(R.id.videopreview);
    mAudioPreview = (ImageView) findViewById(R.id.audiopreview);

    /*-------Intent to view Image Gallery---------*/
    mImgPreview.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            Intent intent = new Intent(getApplicationContext(), ImageList.class);
            startActivity(intent);
        }
    });

    /*-------Intent to view Video Gallery---------*/
    mVideoPreview.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            Intent intent = new Intent(getApplicationContext(), Video.class);
            startActivity(intent);
        }
    });

    /*--------Intent to view Audio Gallery--------*/
    mAudioPreview.setOnClickListener(new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
            Intent intent = new Intent(getApplicationContext(), AudioPlayer.class);
            startActivity(intent);
        }
    });
}

//Thanks in Advance

Narendra Kumar
  • 551
  • 5
  • 16

2 Answers2

20

You need to enable the listener to make it work.

OrientationEventListener mOrientationListener = new OrientationEventListener(
                getApplicationContext()) {
    @Override
    public void onOrientationChanged(int orientation) {
        if (orientation == 0 || orientation == 180) {
            Toast.makeText(getApplicationContext(), "portrait",
                            Toast.LENGTH_LONG).show();
        } else if (orientation == 90 || orientation == 270) {
            Toast.makeText(getApplicationContext(), "landscape",
                            Toast.LENGTH_LONG).show();
        }
    }
};

if (mOrientationListener.canDetectOrientation()) {          
    mOrientationListener.enable();
}

This is working fine for me.

Hope it helps.

Abandoned Cart
  • 4,512
  • 1
  • 34
  • 41
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • I have defined my portrait and landscape layout inside if condition... When i run application it shows Application have been stopped..... – Narendra Kumar Oct 07 '14 at 12:20
  • @NarendraKumar see the logcat. There is a crash – MysticMagicϡ Oct 07 '14 at 12:20
  • When i run application it wont check orientation. After changing device orientation only orientationeventlistener will execute... i don't no where is the problem – Narendra Kumar Oct 07 '14 at 12:24
  • If you need to check once in onCreate only, try with other answer if that works @NarendraKumar – MysticMagicϡ Oct 07 '14 at 12:29
  • 1
    orientaiton can take 0 to 359, not 1 or 0. Code above would always show "landscape". https://developer.android.com/reference/android/view/OrientationEventListener.html#onOrientationChanged(int) – Kazuki Jan 15 '18 at 02:41
  • Do not to forget to call mOrientationListener.disabel() when in not required. In other case it keeps alive (and saves link to the context) until application be killed. – Siarhei Jun 22 '20 at 17:38
-3

use this....

 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  Configuration config = getResources().getConfiguration();

  if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
             // setContentView 1 here
  }else{
             // setContentView 2 here
  }

}

write this code in the onCreate() function coz the Activity starts its cycle from starting during change in the orientation..

mithileshssingh
  • 275
  • 3
  • 16
  • 1
    First, this wasn't the question. He wanted to do it using sensors (OrientationEventListener). Second, your code is not standard; you shouldn't manually check orientation for layouts, that's the purpose of /res/layout and /res/layout-land. – afollestad Jul 21 '15 at 01:38
  • 1
    When setting android:configChanges="orientation|screenLayout|screenSize" in the manifest the onCreate of the activity is not triggered. – Sagi Shchori Sep 23 '21 at 08:15