Reputation: 581
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
Upvotes: 15
Views: 9063
Reputation: 11
check your AndroidManifest, your main activity(entrance of your app) should be set with
android:exported="true"
this is not necessary with sdk 28, but somehow the listener will not work if exported is not set ; Also you can check your logcat while running app, in my condition
"Tried enabling a sensor (lsm6dso Accelerometer Non-wakeup) without hold"
can be found in logcat everytime running the activity with orientationEventListener.
Upvotes: 1
Reputation: 28823
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.
Upvotes: 24
Reputation: 275
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..
Upvotes: -3