How to set a dialog for portrait only? - android

Is there a way to showing a dialog always in portrait? I need that because of its layout.
I know that I can set that for an Activity with
android:screenOrientation="portrait|reversePortrait"
But what about a dialog?

At the time of the dialog showing, check the device's orientation.
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int orientation = display.getOrientation();
//if orientation is portrait then show, if not then don't.

Not sure these ways will fit your requirement ,
One way is showing Activity as a dialog with screen orientation set to portrait (with dialog theme)
refer DialogActivity from ApiDemos
Other way is forcing activity to change its orientation by
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
and open dialog in onConfigurationChanged() (with some logic).

Related

Landscape Dialog with Portrait Activity

I have an Activity that has android:screenOrientation="portrait" options set in AndroidManifest.xml.
At all times, I want this Activity to stay in portrait mode.
However, one of the buttons opens a dialog that has SignaturePad widget, provided by this library.
Because user has to sign with their finger/stylus, I want to make the Dialog landscape so there is more space to sign.
Is it possible to change Dialog's orientation without changing the Activity's orientation?
I've tried DialogFragment, system level dialog (TYPE_SYSTEM_ALERT) and few other things. Nothing seems to work.

How can I use a portrait layout in a Dialog Activity, when screenOrientation is set to landscape in the Manifest?

I have an activity, let's call it HomeActivity, whose screenOrientation has been set to landscape in the manifest file.
Clicking on a button in the HomeActivity presents a Dialog Activity which floats over HomeActivity.
There is a portrait version and a landscape version for the layout that is to be displayed in this dialog, but for this scenario, only the portrait version should be displayed.
In the onCreate() method of the Dialog Activity, I've requested Portrait Orientation but this causes both the dialog and the HomeActivity behind to rotate to portrait mode. I do not want this.
Is there a way to set the orientation of the top most activity without affecting the orientation of the "behind" activity?
Thanks for your help?
Orientation is system-wide property so it can only be landscape or portrait at the one time. This includes all displayed activities (including dialog), status bar and buttons.
You have to display HomeActivity and Dialog Activity with the same orientation.
Also, forcing Activity orientation in the manifest is a bad practice in general.

Spinner popup dialog not adjusting its width on screen rotation

I am using a Spinner with android:spinnerMode="dropdown". I have also set android:configChanges="orientation|keyboardHidden|screenSize" to prevent my Activity to get recreated. when I am in Landscape mode and click on Spinner it pops below the Spinner and have the width same as Spinner button, now when I rotate the screen to portrait mode the Spinner button width get reduced according to the screen size but the pop up dialog does not reduce its width accordingly.
Portrait mode screen:
LAndscape Mode Screen
I thought of closing the pop up and reopen it on screen orientation but I didn't found any way to close the pop up dialog.
On the onConfigurationChanged() method call to spinner.invalidate();
I had this issue as well. I ended up calling notifyDatasetChanged() on the spinnerAdapter in onConfigurationChanged() and it worked.

how to force landscape mode for dialog?

imagine your activity is (forced!) in portrait mode and you'll show an dialog from there, but in landscape mode (because it's containing view is larger than screen width), how will you do that?
I think the screen-orientation of a dialog depends on it's hosting activity/service.
Is there still a way? (e.g. force default orientation of dialog view over it's layout xml-file?)
If you are under DialogFragment Use
on dialog create getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
and on dialog dissmiss getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
if you don't want activity recreate add also
android:configChanges="orientation|screenSize"
other way you will have to handle activity being recreated

How to keep android search dialog on when rotating screen?

I built a search widget based on the code, Searchable Dictionary v2. Everything is working fine, but there is one problem.
If I rotate the screen from portrait to landscape or vice versa when the search dialog box is on, the text edit box and keyboard screen get disappeared and runs again.
Is there any way that I can make that it switches the edit box into appropriated mode(landscape or portrait) without exiting and re-running the dialog box?
You can check the behavior with the searchable dictionary.
I want something like the google search widget. It just switches edit box mode while it keeps the screen.
Once the screen orientation is changed, Android creates a new activity/view, so you'll may have to handle screen orientation by yourself. I used this solution in an app successfully, I assume it also applies to widgets.
See this page on how to do that:
http://www.androidpeople.com/android-how-to-handle-screen-orientation-change-issue/
Some other helpful hints (basically the same solutions) are here:
How do I handle screen orientation changes with an activity started within a tab's activity
How to handle screen orientation change when progress dialog and background thread active?
Some information concerning onPause() and onSaveInstanceState() (which may not apply as you're using a widget, but just in case :) ) is here:
How do I disable orientation change on Android?
What i did was to lock the orientation while the dialog is displayed, then unlock it when finished:
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); //locks landscape
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); //locks port
}
// do work
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

Categories

Resources