|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Hossain Khan |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.hossainkhan.android.demo.layoutpreview |
| 18 | + |
| 19 | +import android.content.Context |
| 20 | +import android.content.Intent |
| 21 | +import android.os.Bundle |
| 22 | +import android.support.annotation.StringRes |
| 23 | +import android.support.constraint.ConstraintLayout |
| 24 | +import android.support.constraint.ConstraintSet |
| 25 | +import android.support.constraint.ConstraintSet.CHAIN_PACKED |
| 26 | +import android.support.constraint.ConstraintSet.CHAIN_SPREAD |
| 27 | +import android.support.constraint.ConstraintSet.CHAIN_SPREAD_INSIDE |
| 28 | +import android.support.transition.TransitionManager |
| 29 | +import android.view.View |
| 30 | +import com.hossainkhan.android.demo.R |
| 31 | +import android.widget.RadioButton |
| 32 | +import android.widget.TextView |
| 33 | +import timber.log.Timber |
| 34 | + |
| 35 | +/** |
| 36 | + * Showcases the constraint layout chaining with different styles. |
| 37 | + * |
| 38 | + * https://developer.android.com/reference/android/support/constraint/ConstraintLayout#Chains |
| 39 | + */ |
| 40 | +class LayoutChainStyleActivity : LayoutPreviewBaseActivity() { |
| 41 | + |
| 42 | + companion object { |
| 43 | + /** |
| 44 | + * Creates an intent with required information to start this activity. |
| 45 | + * |
| 46 | + * @param context Activity context. |
| 47 | + */ |
| 48 | + fun createStartIntent(context: Context): Intent { |
| 49 | + val intent = Intent(context, LayoutChainStyleActivity::class.java) |
| 50 | + intent.putExtra(BUNDLE_KEY_LAYOUT_RESID, R.layout.preview_chain_style_main) |
| 51 | + return intent |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private lateinit var constraintLayout: ConstraintLayout |
| 56 | + private lateinit var guideTextView: TextView |
| 57 | + /** |
| 58 | + * Use constraint set to dynamically update constraints |
| 59 | + * https://developer.android.com/reference/android/support/constraint/ConstraintSet |
| 60 | + */ |
| 61 | + private val constraintSet = ConstraintSet() |
| 62 | + |
| 63 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 64 | + super.onCreate(savedInstanceState) |
| 65 | + |
| 66 | + constraintLayout = findViewById(R.id.constraint_layout_root) |
| 67 | + guideTextView = findViewById(R.id.view_chain_horizontal_guide_text) |
| 68 | + constraintSet.clone(constraintLayout) |
| 69 | + } |
| 70 | + |
| 71 | + fun onRadioButtonClicked(view: View) { |
| 72 | + val checked = (view as RadioButton).isChecked |
| 73 | + |
| 74 | + // Check which radio button was clicked |
| 75 | + when (view.getId()) { |
| 76 | + R.id.radio_chain_action_packed -> { |
| 77 | + applyChainStyle(checked, R.string.view_guide_chain_style_packed, CHAIN_PACKED) |
| 78 | + } |
| 79 | + R.id.radio_chain_action_spread -> { |
| 80 | + applyChainStyle(checked, R.string.view_guide_chain_style_spread, CHAIN_SPREAD) |
| 81 | + } |
| 82 | + R.id.radio_chain_action_spread_inside -> { |
| 83 | + applyChainStyle(checked, R.string.view_guide_chain_style_spread_inside, CHAIN_SPREAD_INSIDE) |
| 84 | + } |
| 85 | + } |
| 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 | + } |
| 99 | +} |
0 commit comments