Skip to content
This repository was archived by the owner on Aug 22, 2024. It is now read-only.

Commit 4f6ecb5

Browse files
authored
Merge pull request #43 from amardeshbd/feature/42_chaining
Feature/42 chaining
2 parents 6d5f446 + 981834a commit 4f6ecb5

File tree

12 files changed

+584
-2
lines changed

12 files changed

+584
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ A demo application for Android `ConstraintLayout` with various usage with sample
3030
* [ ] `WRAP_CONTENT` : enforcing constraints _(Added in 1.1)_
3131
* [ ] Widgets dimension constraints
3232
- [ ] Chains
33-
* [ ] Chain Style
33+
* [x] Chain Style
3434
* [ ] Weighted chains
3535
* [ ] Margins and chains _(Added in 1.1)_
3636
- [ ] Virtual Helpers objects

app/src/main/AndroidManifest.xml

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
<activity
2424
android:name=".layoutpreview.LayoutVisibilityGoneActivity"
2525
android:parentActivityName=".browse.LayoutBrowseActivity" />
26+
<activity
27+
android:name=".layoutpreview.LayoutChainStyleActivity"
28+
android:parentActivityName=".browse.LayoutBrowseActivity" />
2629
</application>
2730

2831
</manifest>

app/src/main/java/com/hossainkhan/android/demo/browse/LayoutBrowseActivity.kt

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import android.support.v7.app.AppCompatActivity
2222
import android.support.v7.widget.GridLayoutManager
2323
import android.support.v7.widget.RecyclerView
2424
import com.hossainkhan.android.demo.R
25+
import com.hossainkhan.android.demo.layoutpreview.LayoutChainStyleActivity
2526
import com.hossainkhan.android.demo.layoutpreview.LayoutPreviewBaseActivity
2627
import com.hossainkhan.android.demo.layoutpreview.LayoutVisibilityGoneActivity
2728
import com.hossainkhan.android.demo.viewmodel.LayoutPreviewViewModelFactory
@@ -80,6 +81,9 @@ class LayoutBrowseActivity : AppCompatActivity() {
8081
R.layout.preview_visibility_gone -> {
8182
startActivity(LayoutVisibilityGoneActivity.createStartIntent(this))
8283
}
84+
R.layout.preview_chain_style_main -> {
85+
startActivity(LayoutChainStyleActivity.createStartIntent(this))
86+
}
8387
else -> {
8488
startActivity(LayoutPreviewBaseActivity.createStartIntent(this, layoutResId))
8589
}

app/src/main/java/com/hossainkhan/android/demo/dagger/ActivityBindingModule.kt

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.hossainkhan.android.demo.dagger
1818

19+
import com.hossainkhan.android.demo.layoutpreview.LayoutChainStyleActivity
1920
import com.hossainkhan.android.demo.layoutpreview.LayoutPreviewBaseActivity
2021
import com.hossainkhan.android.demo.layoutpreview.LayoutVisibilityGoneActivity
2122
import dagger.Module
@@ -52,4 +53,8 @@ abstract class ActivityBindingModule {
5253
@ActivityScope
5354
@ContributesAndroidInjector
5455
abstract fun layoutVisibilityActivity(): LayoutVisibilityGoneActivity
56+
57+
@ActivityScope
58+
@ContributesAndroidInjector
59+
abstract fun layoutChainActivity(): LayoutChainStyleActivity
5560
}

app/src/main/java/com/hossainkhan/android/demo/data/LayoutDataStore.kt

+23-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,29 @@ class LayoutDataStore @Inject constructor(
7474
"But in terms of the layout computations, GONE widgets are still part of it, with an important distinction:" +
7575
"\n\n" +
7676
" * For the layout pass, their dimension will be considered as zero (basically, they will be resolved to a point)\n" +
77-
" * If they have constraints to other widgets they will still be respected, but any margins will be as if equals to zero")
77+
" * If they have constraints to other widgets they will still be respected, but any margins will be as if equals to zero"),
78+
LayoutInformation(
79+
layoutResourceId = R.layout.preview_chain_style_main,
80+
thumbnailResourceId = R.drawable.thumb_chain_style,
81+
title = "Chain: Pack Style",
82+
description = "When setting the attribute `constraintHorizontal_chainStyle` or " +
83+
"`constraintVertical_chainStyle` on the first element of a chain, " +
84+
"the behavior of the chain will change according to the specified style (default is CHAIN_SPREAD)." +
85+
"\n\n" +
86+
" * CHAIN_SPREAD -- the elements will be spread out (default style)\n" +
87+
" * Weighted chain -- in CHAIN_SPREAD mode, if some widgets are set to MATCH_CONSTRAINT, they will split the available space\n" +
88+
" * CHAIN_SPREAD_INSIDE -- similar, but the endpoints of the chain will not be spread out\n" +
89+
" * CHAIN_PACKED -- the elements of the chain will be packed together. The horizontal or vertical bias attribute of the child will then affect the positioning of the packed elements")
90+
/*
91+
Next item template (easy to copy and paste)
92+
LayoutInformation(
93+
layoutResourceId = R.layout.XYZ,
94+
thumbnailResourceId = R.drawable.XYZ,
95+
title = "XYZ: XYZ",
96+
description = "XYZ")
97+
*/
98+
99+
78100
)
79101

80102
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)