WanderingWizard
WanderingWizard

Reputation: 23

always view dialog in landscape

I'm repeating a post I've made at the android group in google groups (http://groups.google.com/group/android-developers/browse_thread/thread/78d0b7496a51e3b7# ), hopefully I'll have some luck here!

I have an activity displaying a list of items. Upon clicking an item, I display a full-screen dialog with some buttons and an image. This looks great in landscape, but doesn't look good in portrait (Due to the image aspect ratio). I would like to know if its possible to always display this dialog in landscape irrespective of current screen orientation. And after the dialog is dismissed, I'll handover the orientation back to the sensor. Here's what I have so far:

OnItemClickListener listlistener = new OnItemClickListener() { 
                @Override 
                public void onItemClick(AdapterView parent, View arg1, int position, 
                                long arg3) { 
                        final Dialog dialog = new Dialog(getParent()); 
                        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
                        final int orientation = 
getResources().getConfiguration().orientation; 
                        if (orientation == 1){ 
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
                        } 
                        dialog.setContentView(R.layout.dialog); 
                        dialog.setCancelable(true); 
                        ImageView img = (ImageView) dialog.findViewById(R.id.img); //A 
static image for testing UI 
                        //BUTTON LISTENERS HERE 
                        dialog.show(); 
                        dialog.setOnCancelListener(new OnCancelListener() { 
                                @Override 
                                public void onCancel(DialogInterface dialog) { 
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); 
                                } 
                        }); 
                } 
        }; 

But by doing so, the screen changes orientation first and then tries to display the dialog. But on orientation change the screen is restarting the activity and my dialog isn't shown. Is there a simpler way to do this? Could I use two different layout for the dialog and load a corresponding one depending on the orientation? If so, then what would the XML for the portrait mode look like? Thanks!

Upvotes: 2

Views: 4031

Answers (2)

You can also rotate your top view
android:rotation="90" in XML
or
constraintNumber.rotation = 90f programmatically.

Then you don't need to change activity orientation, which can lead to some other issues.

If you have a background for your screen then you can add background to the top view group and then add rotation for rotation to the child view group. Because the rotation will work for the background as well.

For example:

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/rectangle_blue_24dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:rotation="90"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent">

<!--          Your views-->
            
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

blindstuff
blindstuff

Reputation: 18348

If you are using a custom layout for the dialog, just design a new one and put it in the layout-land folder (create it if necesary), that way it will load the corresponding one automatically.

Upvotes: 1

Related Questions