Rotate Dialog and AlertDialog other than fixed device orientation - android

I tried using search but didn't find a solution for this problem.
I have a camera application with fixed landscape orientation. An orientationEventListener properly rotates all my buttons. There are, however, some buttons that shall show a dialog or alertdialog and these dialogs are not rotated. Here is the relevant shortened code in MainActivity
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(CameraActivity.this);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.setContentView(R.layout.layout_dialog_mode);
// set up texts in layout_dialog_mode
and
private void createMenu() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final String[] modeS = {getString(R.string.mode_1), getString(R.string.mode_2), getString(R.string.mode_3), getString(R.string.mode_4), getString(R.string.mode_5)};
builder.setTitle(getString(R.string.mode_title));
builder.setSingleChoiceItems(modeS, mode, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
// set things as chosen by user
}
dialog.dismiss(); /* close menu after selection */
}
});
builder.show();
}
layout_dialog_mode is a linearLayout containing TextViews and ImageViews.
The rotation of buttons works like this:
private void initialiseOEListener() {
oEListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
#Override
public void onOrientationChanged(int orientation) {
try {
if (orientation == ORIENTATION_UNKNOWN) return;
orientation = (orientation + 45) / 90 * 90;
switch (orientation) {
// set params and orientation
}
ImageButton ibu = (ImageButton) findViewById(R.id.button0);
ibu.setRotation(orientation);
ibu = (ImageButton) findViewById(R.id.button1);
ibu.setRotation(orientation);
// and so on
} catch (Exception e) {
}
}
};
Now my question: Is it possible to rotate the dialogs in same manner as the buttons? At the moment, they are displayed in landscape since I set device orientation for this Activity to landscape. When user for example rotates the phone to portrait and clicks button1, dialog will be shown wrong because it is not rotated.
Many thanks

Ok so I resolved the issue by creating separate Activity for each dialog. The problem is now, when the new foreground dialog Activity is rotated, the background Activity will rotate as well! Can I somehow set the background Activity orientation when I rotate foreground Activity?

Related

Restoring a dialog after screen rotation

I am experiencing what is more of a minor problem than anything else trying to restore an error dialog box in an activity during screen rotation (portrait to landscape or vice-versa). The dialog box does get rendered correctly when the error occurs, but upon screen rotation the dialog is not restored correctly. Instead, the entire screen becomes dim, but nothing is visible. Here is the relevant code:
private void showErrorDialog() {
// assume hasErrorDialog is true at this point
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(SomeActivity.this);
LayoutInflater inflater = SomeActivity.this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog_alert, null);
dialogBuilder.setView(dialogView);
TextView msgText = (TextView) dialogView.findViewById(R.id.alertMessageText);
msgText.setText("something went wrong");
Button okButton = (Button) dialogView.findViewById(R.id.alertOkButton);
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
alertDialog.dismiss();
hasErrorDialog = false;
}
});
alertDialog = dialogBuilder.create();
alertDialog.show();
RelativeLayout rl = (RelativeLayout) findViewById(R.id.someActivity);
int width = rl.getWidth();
alertDialog.getWindow().setLayout((int) (0.9 * width), ViewGroup.LayoutParams.WRAP_CONTENT);
}
When the above method is called after the activity has loaded, and an error has happened, the dialog loads and behaves exactly as it should. So the above code is completely working when called under usual conditions.
However, I added logic which uses saved instance state to try to "remember" that in fact there should be an error dialog. Upon rotation, I attempt to call the above method again after checking for this instance state:
protected void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
bundle.putBoolean("HASERRORDIALOG", hasErrorDialog);
}
Then in onCreate() I attempt to check for this state, and if present, call showErrorDialog() again:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.some_activity);
if (savedInstanceState != null) {
hasErrorDialog = savedInstanceState.getBoolean("HASERRORDIALOG");
if (hasErrorDialog) {
// this does not load the dialog correctly
showErrorDialog();
}
}
}
Most of the questions/answers I have read on Stack Overflow get around this problem by suggesting to use a DialogFragment. While I'm open to going in this direction, I was wondering if there is not some remedy for my current code.
Running with the excellent comment by #MikeM, I realized the problem was that the RelativeLayout of my activity had not yet been fully created in the onCreate() method, and hence its width was not what I was expecting (likely to be zero).
As a workaround, I used getDisplayMetrics() to access the actual device width, which does still exist at the point in the lifecycle where onCreate gets called:
alertDialog = dialogBuilder.create();
alertDialog.show();
int width = (int)(getResources().getDisplayMetrics().widthPixels*0.90);
alertDialog.getWindow().setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);
The lesson to be learned here is that there is a potential caveat when basing a dialog off something in your layout. In this case, when attempting to restore that dialog in onCreate after device rotation would fail, but this is one possible workaround.

Fullscreen landscape Dialog

I have a dialog (that contains a video player by ExoPlayer) that is created like this:
dialog = new Dialog(this, android.R.style.Theme_Black_NoTitleBar_Fullscreen) {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
#Override
public void onBackPressed() {
if (exoPlayerFullscreen)
closeFullscreenDialog();
super.onBackPressed();
}
};
This way, the video is showed in landscape mode but not in fullscreen. If I remove setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE), the video is played in fullscreen but in portrait mode. How can I get the video show up in both (fullscreen and landscape mode)?
Thank you.
Make a cool icon and on the click of the icon change the orientation at runTime. .
#Override
onClick(View v) {
if(flag_landscape)
setRequestedOrientation(ActivityInfo
.SCREEN_ORIENTATION_LANDSCAPE);
else
setRequestedOrientation(ActivityInfo
.SCREEN_ORIENTATION_PORTRAIT);
}

Around Dialog everything becomes black

I have an Activity that is opened when the user clicks on it. Also, sometimes (in this case when the game is not started yet) there is another Activity opened. This Activity is in the form of a dialog. When this dialog pops up, everything around the dialog is black (I want it (dark) transparent). Here is the code how I start the activities:
private void initializeButtonStart(){
buttonStart = (Button) findViewById(R.id.buttonStartGame);
buttonStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(StartActivity.this, GameOverviewActivity.class);
startActivity(intent);
if (!bussen.isGameStarted()) {
Intent intent2 = new Intent(StartActivity.this, PlayerSettingsActivity.class);
startActivity(intent2);
}
}
});
}
This is the way how I make a dialog of the Activity:
private void setDialogWidth(){
DisplayMetrics metrics = getResources().getDisplayMetrics();
int screenWidth = (int) (metrics.widthPixels * DIALOG_WIDTH);
getWindow().setLayout(screenWidth, RelativeLayout.LayoutParams.WRAP_CONTENT);
}
I use this methods at several Activities to become a Dialog lookalike. Another Dialogs just turn (dark) transparent outside, but with this way everything becomes black around the dialog. This is the case only in Android 6, versions before I had no problems.
Hope you can help!
[i cant add comment so...]
in android studio you can select colors that have low opacity.if you don't have android studio so go to layout xml file and set background color to black and then set opacity to 0.7 or 0.6(a float between 0 and 1.0)

How to design android layout for this snapshot?

I am a new in android programming, I made a layout with this figure:
Now I want to know when one of these buttons clicked I should run an new activity or change visibility to false and show new layout without run a new activity, what is the best solution?
You consider that count of these buttons are more than ten.
I want show a text with image,..(when clicked) because that is a educational book and these buttons are chapters list of that book
for an example if you want to change only the layout then you could do something like this
FirstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
FirstView();
}
});
/
void FirstView(){
setContentView(R.layout.yourOtherLayout);
// then declare the layout views here.
firstView=false;
}
you can do this in all the buttons just create different methods for each
to handle the Back Button you can declare Boolean variables and use If else Statement to loop through them for example
boolean firstView = true, secondView = true;
#Override
public void onBackPressed(){
if (firstView == false ){
then firstView Is Showing.
// show the view you want and set
firstView = true;
}else if (SO ON)...
else { super.OnBackPressed(); // exit }
}

Change Screen Orientation programmatically using a Button

I think this is implementable since screen rotation behaviour can go up to the application level.
Yes it is implementable!
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ActivityInfo
http://developer.android.com/reference/android/content/pm/ActivityInfo.html
Refer the
link:
Button buttonSetPortrait = (Button)findViewById(R.id.setPortrait);
Button buttonSetLandscape = (Button)findViewById(R.id.setLandscape);
buttonSetPortrait.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
});
buttonSetLandscape.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
});
http://android-er.blogspot.in/2011/08/set-screen-orientation-programmatically.html
Yes, you can set the screen orientation programatically anytime you want using:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
for landscape and portrait mode respectively. The setRequestedOrientation() method is available for the Activity class, so it can be used inside your Activity.
And this is how you can get the current screen orientation and set it adequatly depending on its current state:
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
final int orientation = display.getOrientation();
// OR: orientation = getRequestedOrientation(); // inside an Activity
// set the screen orientation on button click
Button btn = (Button) findViewById(R.id.yourbutton);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
switch(orientation) {
case Configuration.ORIENTATION_PORTRAIT:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Configuration.ORIENTATION_LANDSCAPE:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
}
}
});
Taken from here: http://techblogon.com/android-screen-orientation-change-rotation-example/
EDIT
Also, you can get the screen orientation using the Configuration:
Activity.getResources().getConfiguration().orientation
Wherever possible, please don't use SCREEN_ORIENTATION_LANDSCAPE or SCREEN_ORIENTATION_PORTRAIT. Instead use:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
These allow the user to orient the device to either landscape orientation, or either portrait orientation, respectively. If you've ever had to play a game with a charging cable being driven into your stomach, then you know exactly why having both orientations available is important to the user.
Note: For phones, at least several that I've checked, it only allows the "right side up" portrait mode, however, SENSOR_PORTRAIT works properly on tablets.
Note: this feature was introduced in API Level 9, so if you must support 8 or lower (not likely at this point), then instead use:
setRequestedOrientation(Build.VERSION.SDK_INT < 9 ?
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE :
ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
setRequestedOrientation(Build.VERSION.SDK_INT < 9 ?
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT :
ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
Use this to set the orientation of the screen:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
or
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
and don't forget to add this to your manifest:
android:configChanges = "orientation"
A working code:
private void changeScreenOrientation() {
int orientation = yourActivityName.this.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
showMediaDescription();
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
hideMediaDescription();
}
if (Settings.System.getInt(getContentResolver(),
Settings.System.ACCELEROMETER_ROTATION, 0) == 1) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
}, 4000);
}
}
call this method in your button click
Android-Kotlin:
to make rotate X:
binding.btnRotateHorizontal.setOnClickListener {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
}
to make rotate Y:
binding.btnRotateVertical.setOnClickListener {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
}
Yes ، Hariharan answer works fine . but you should add below line to AndroidManifest.xml in activity tag :
android:screenOrientation="fullSensor"
android:configChanges="orientation|screenSize"
if not add above line , Hariharan answer not work .
Thanks Benny !
mine worked with:
setLandscapeIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setLandscapeIcon.setVisibility(View.GONE);
setPortraitIcon.setVisibility(View.VISIBLE);
}
});
setPortraitIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
setLandscapeIcon.setVisibility(View.VISIBLE);
setPortraitIcon.setVisibility(View.GONE);
}
});
In Manifest
android:configChanges = "orientation|screenSize"
android:screenOrientation="portrait"
NOTE: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); was not working on my device

Categories

Resources