2

I am playing video inside a layout in a ConstraintLayout and am overriding onConfigurationChanged rather than restarting the activity in order to prevent the video from stopping and starting on orientation change. I am trying to determine the best way to resize the layout to full screen as the layout is not refreshed during the rotation. The layout uses the following constraints:

app:layout_constraintEnd_toEndOf="parent"  
app:layout_constraintStart_toStartOf="parent"  
android:layout_width="0dp"  
android:layout_height="0dp"  

After rotation I would like the layout to use the same constraints but with the width and height changing to suit the landscape rather than portrait and vice versa. I currently set the video player layout to the width and height of the screen using DisplayMetrics but wondering if there is a better solution. RequestLayout does not seem to do it. Anyone have any better solutions?

Krisztián Balla
  • 19,223
  • 13
  • 68
  • 84
Jaz
  • 371
  • 1
  • 6
  • 20
  • You need to create two layouts one for portrait mode and the other for landscape mode, and yourself cofigure both of them as you want, its better than using functions in java code. See also https://stackoverflow.com/questions/4858026/android-alternate-layout-xml-for-landscape-mode – Azhy Jun 08 '18 at 17:12
  • Thanks Azhy, I have done that for some transitions but being this is a large layout I was sort of looking for a solution where I don't have to maintain multiple layouts if possible every time I wish to make a change to it. Good solution though. That may be the only alternative. Just wondering if there is any sort of "refresh" that would look at the new screen boundaries and adjust. Thanks – Jaz Jun 08 '18 at 17:18
  • I think this is the most succesfull way to do that. – Azhy Jun 08 '18 at 17:20
  • So the solution is to create two different files ? – Walid Aug 22 '18 at 08:18
  • Yes with lot's of includes to cut down on maintenance of 2 layouts. – Jaz Aug 27 '18 at 22:15
  • ConstraintLayout does resize itself automatically. Your problem lies elsewhere. Make sure you put `constraintTop` and `constraintBottom` constraints to parent also, and also make sure to call `super.onConfigurationChanged(newConfiguration)` in your overridden handler code. – deje Sep 05 '18 at 22:54

1 Answers1

0

Use onConfigurationChanged method and animate constrain set on the same layout...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Context context = this;
        mConstraintSet2.clone(context, R.layout.state2); // get constraints from layout
        setContentView(R.layout.state1);
        mConstraintLayout = (ConstraintLayout) findViewById(R.id.activity_main);
        mConstraintSet1.clone(mConstraintLayout); // get constraints from ConstraintSet
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
    
        // Checks the orientation of the screen
        if (newConfig.orientation === Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show()
            TransitionManager.beginDelayedTransition(mConstraintLayout);
            mConstraintSet1.applyTo(mConstraintLayout); // set new constraints
        } else if (newConfig.orientation === Configuration.ORIENTATION_PORTRAIT) {
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show()
            TransitionManager.beginDelayedTransition(mConstraintLayout);
            mConstraintSet2.applyTo(mConstraintLayout); // set new constraints
        }
    }
Qamar
  • 4,959
  • 1
  • 30
  • 49