@@ -19,6 +19,7 @@ package com.hossainkhan.android.demo.layoutpreview
19
19
import android.content.Context
20
20
import android.content.Intent
21
21
import android.os.Bundle
22
+ import android.support.annotation.StringRes
22
23
import android.support.constraint.ConstraintLayout
23
24
import android.support.constraint.ConstraintSet
24
25
import android.support.constraint.ConstraintSet.CHAIN_PACKED
@@ -52,7 +53,7 @@ class LayoutChainStyleActivity : LayoutPreviewBaseActivity() {
52
53
}
53
54
54
55
private lateinit var constraintLayout: ConstraintLayout
55
- private lateinit var guideText : TextView
56
+ private lateinit var guideTextView : TextView
56
57
/* *
57
58
* Use constraint set to dynamically update constraints
58
59
* https://developer.android.com/reference/android/support/constraint/ConstraintSet
@@ -63,41 +64,36 @@ class LayoutChainStyleActivity : LayoutPreviewBaseActivity() {
63
64
super .onCreate(savedInstanceState)
64
65
65
66
constraintLayout = findViewById(R .id.constraint_layout_root)
66
- guideText = findViewById(R .id.view_chain_horizontal_guide_text)
67
+ guideTextView = findViewById(R .id.view_chain_horizontal_guide_text)
67
68
constraintSet.clone(constraintLayout)
68
69
}
69
70
70
71
fun onRadioButtonClicked (view : View ) {
71
- Timber .d(" Constraint style change requested %s" , view)
72
- // Is the button now checked?
73
72
val checked = (view as RadioButton ).isChecked
74
73
75
74
// Check which radio button was clicked
76
75
when (view.getId()) {
77
76
R .id.radio_chain_action_packed -> {
78
- if (checked) {
79
- guideText.setText(R .string.view_guide_chain_style_packed)
80
- TransitionManager .beginDelayedTransition(constraintLayout)
81
- constraintSet.setHorizontalChainStyle(R .id.view_chain_view_first, CHAIN_PACKED )
82
- constraintSet.applyTo(constraintLayout)
83
- }
77
+ applyChainStyle(checked, R .string.view_guide_chain_style_packed, CHAIN_PACKED )
84
78
}
85
79
R .id.radio_chain_action_spread -> {
86
- if (checked) {
87
- guideText.setText(R .string.view_guide_chain_style_spread)
88
- TransitionManager .beginDelayedTransition(constraintLayout)
89
- constraintSet.setHorizontalChainStyle(R .id.view_chain_view_first, CHAIN_SPREAD )
90
- constraintSet.applyTo(constraintLayout)
91
- }
80
+ applyChainStyle(checked, R .string.view_guide_chain_style_spread, CHAIN_SPREAD )
92
81
}
93
82
R .id.radio_chain_action_spread_inside -> {
94
- if (checked) {
95
- guideText.setText(R .string.view_guide_chain_style_spread_inside)
96
- TransitionManager .beginDelayedTransition(constraintLayout)
97
- constraintSet.setHorizontalChainStyle(R .id.view_chain_view_first, CHAIN_SPREAD_INSIDE )
98
- constraintSet.applyTo(constraintLayout)
99
- }
83
+ applyChainStyle(checked, R .string.view_guide_chain_style_spread_inside, CHAIN_SPREAD_INSIDE )
100
84
}
101
85
}
102
86
}
87
+
88
+ private fun applyChainStyle (isChecked : Boolean , @StringRes guideText : Int , chainStyle : Int ) {
89
+ if (isChecked) {
90
+ Timber .d(" Updating chain style to %s, and text to %s" , chainStyle, getString(guideText))
91
+ guideTextView.setText(guideText)
92
+ TransitionManager .beginDelayedTransition(constraintLayout)
93
+ constraintSet.setHorizontalChainStyle(R .id.view_chain_view_first, chainStyle)
94
+ constraintSet.applyTo(constraintLayout)
95
+ } else {
96
+ Timber .i(" View was not checked. Not taking action." )
97
+ }
98
+ }
103
99
}
0 commit comments