Skip to content

Instantly share code, notes, and snippets.

@mandrachek
Created January 18, 2017 16:11
Show Gist options
  • Save mandrachek/6b42e22f6c5001a95a71115f4096eba4 to your computer and use it in GitHub Desktop.
Save mandrachek/6b42e22f6c5001a95a71115f4096eba4 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/constraint_layout"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_margin="4dp"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
app:layout_constraintLeft_toRightOf="@+id/button1"
app:layout_constraintTop_toTopOf="parent"
android:layout_margin="4dp"
/>
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/constraint_layout"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_margin="4dp"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button1"
android:layout_margin="4dp"
/>
</android.support.constraint.ConstraintLayout>
import android.content.res.Configuration
import android.os.Bundle
import android.support.constraint.ConstraintSet
import android.support.v7.app.AppCompatActivity
import android.transition.ChangeBounds
import android.transition.TransitionManager
import kotlinx.android.synthetic.main.main_landscape.*
class MainActivity : AppCompatActivity() {
val portraitConstraintSet = ConstraintSet()
val landscapeConstraintSet = ConstraintSet()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// load portrait and layout constraint sets
// they need to have the same items inside the <ConstraintLayout/>, just with different constraints
portraitConstraintSet.clone(this,R.layout.main_portrait)
landscapeConstraintSet.clone(this,R.layout.main_landscape)
// start with the correct layout for the current orientation
val orientation = resources.configuration.orientation
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.main_landscape)
}
else {
setContentView(R.layout.main_portrait)
}
}
override fun onConfigurationChanged(configuration: Configuration?) {
super.onConfigurationChanged(configuration)
// we only want a changebounds transition, no fading
val transition = ChangeBounds()
// default duration is too small to see, so bump it up to 600ms
transition.duration = 600
TransitionManager.beginDelayedTransition(constraint_layout,transition)
if (configuration?.orientation == Configuration.ORIENTATION_PORTRAIT) {
portraitConstraintSet.applyTo(constraint_layout)
}
if (configuration?.orientation == Configuration.ORIENTATION_LANDSCAPE) {
landscapeConstraintSet.applyTo(constraint_layout)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment