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

Commit 6046dd7

Browse files
authored
Merge pull request #33 from amardeshbd/feature/27_positioning_with_bias
Feature/27 positioning with bias
2 parents 0687da0 + 5fa7b17 commit 6046dd7

File tree

10 files changed

+208
-10
lines changed

10 files changed

+208
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ A demo application for Android `ConstraintLayout` with various usage with sample
2121
- [ ] Margins
2222
- [x] Centering positioning and bias
2323
- [x] Circular positioning _(Added in 1.1)_
24-
- [ ] Visibility behavior
24+
- [x] Visibility behavior
2525
- [ ] Dimension constraints
2626
* [ ] Ratio
2727
* [ ] Percent dimension

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,15 @@ class MainActivity : AppCompatActivity() {
6262
}
6363
}
6464

65-
fun onLayoutItemSelected(layoutResId: Int) {
65+
private fun onLayoutItemSelected(layoutResId: Int) {
6666
Timber.i("Selected layout id: %s", layoutResId)
6767

6868
when (layoutResId) {
6969
R.layout.preview_visibility_gone -> {
70-
startActivity(LayoutVisibilityGoneActivity
71-
.createStartIntent(this))
70+
startActivity(LayoutVisibilityGoneActivity.createStartIntent(this))
7271
}
7372
else -> {
74-
startActivity(LayoutPreviewBaseActivity
75-
.createStartIntent(this, layoutResId))
73+
startActivity(LayoutPreviewBaseActivity.createStartIntent(this, layoutResId))
7674
}
7775
}
7876

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ import com.hossainkhan.android.demo.base.AppConfig
2323
import javax.inject.Inject
2424
import javax.inject.Singleton
2525

26+
/**
27+
* Provides layout data, which is currently static based on what is available.
28+
*
29+
* In future, we may store the title and description based on layout ID.
30+
*/
2631
@Singleton
2732
class LayoutDataStore @Inject constructor(
2833
private val resources: Resources) {
@@ -44,6 +49,14 @@ class LayoutDataStore @Inject constructor(
4449
thumbnailResourceId = R.drawable.thumb_positioning_center,
4550
title = "Positioning: Centered",
4651
description = "Centered view using constraints on top-bottom and left-right."),
52+
LayoutInformation(
53+
layoutResourceId = R.layout.preview_positioning_bias,
54+
thumbnailResourceId = R.drawable.thumb_positioning_bias,
55+
title = "Positioning: Horizontal and Vertical Bias",
56+
description = "The default when encountering such opposite constraints is to center the widget; but you can tweak the positioning to favor one side over another using the bias attributes:" +
57+
"\n\n" +
58+
" * layout_constraintHorizontal_bias\n" +
59+
" * layout_constraintVertical_bias"),
4760
LayoutInformation(
4861
layoutResourceId = R.layout.preview_positioning_circular,
4962
thumbnailResourceId = R.drawable.thumb_positioning_circular,
@@ -78,8 +91,8 @@ class LayoutDataStore @Inject constructor(
7891
* @return The URL to load the layout blob file.
7992
*/
8093
fun getLayoutUrl(@LayoutRes layoutResourceId: Int): String {
81-
// Containes package name and layout name
82-
// com.hossainkhan.android.demo:layout/preview_positioning_top_left
94+
// Contains package name and layout name
95+
// For example: com.hossainkhan.android.demo:layout/preview_positioning_top_left
8396
val resourceName = resources.getResourceName(layoutResourceId)
8497

8598
if (!resourceName.contains("layout")) {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
18+
android:width="24dp"
19+
android:height="24dp"
20+
android:viewportWidth="640"
21+
android:viewportHeight="640">
22+
<path
23+
android:fillAlpha="0"
24+
android:fillColor="#FF000000"
25+
android:pathData="M2.73,638.39L637.81,638.39L637.81,2.7L2.73,2.7L2.73,638.39Z"
26+
android:strokeWidth="2"
27+
android:strokeAlpha="1"
28+
android:strokeColor="#000000" />
29+
<path
30+
android:fillAlpha="1"
31+
android:fillColor="#00acc1"
32+
android:pathData="M537.81,317.54C543.34,317.54 547.81,322.02 547.81,327.54C547.81,366.52 547.81,473.48 547.81,512.46C547.81,517.98 543.34,522.46 537.81,522.46C500.69,522.46 399.31,522.46 362.19,522.46C356.66,522.46 352.19,517.98 352.19,512.46C352.19,473.48 352.19,366.52 352.19,327.54C352.19,322.02 356.66,317.54 362.19,317.54C399.31,317.54 500.69,317.54 537.81,317.54Z" />
33+
<path
34+
android:fillAlpha="0.85"
35+
android:fillColor="#006064"
36+
android:pathData="M492.22,265.09L513.75,302.62L535.28,265.09L521.28,265.09L521.28,51.33L535.28,51.33L513.75,10L492.22,51.33L506.21,51.33L506.21,265.09L492.22,265.09Z" />
37+
<path
38+
android:fillAlpha="0"
39+
android:fillColor="#FF000000"
40+
android:pathData="M492.22,265.09L513.75,302.62L535.28,265.09L521.28,265.09L521.28,51.33L535.28,51.33L513.75,10L492.22,51.33L506.21,51.33L506.21,265.09L492.22,265.09Z"
41+
android:strokeWidth="1"
42+
android:strokeAlpha="1"
43+
android:strokeColor="#000000" />
44+
<path
45+
android:fillAlpha="0"
46+
android:fillColor="#FF000000"
47+
android:pathData="M367.69,133.71C367.69,148.28 358.39,160.12 346.92,160.12C335.46,160.12 326.15,148.28 326.15,133.71C326.15,119.13 335.46,107.29 346.92,107.29C358.39,107.29 367.69,119.13 367.69,133.71Z"
48+
android:strokeWidth="8"
49+
android:strokeAlpha="1"
50+
android:strokeColor="#000000" />
51+
<path
52+
android:fillAlpha="0"
53+
android:fillColor="#FF000000"
54+
android:pathData="M420,159.09C420,186.69 411.27,209.09 400.51,209.09C389.76,209.09 381.03,186.69 381.03,159.09C381.03,131.49 389.76,109.09 400.51,109.09C411.27,109.09 420,131.49 420,159.09Z"
55+
android:strokeWidth="8"
56+
android:strokeAlpha="1"
57+
android:strokeColor="#000000" />
58+
<path
59+
android:fillAlpha="0"
60+
android:fillColor="#FF000000"
61+
android:pathData="M367.95,186.29C367.95,200.87 358.64,212.71 347.18,212.71C335.72,212.71 326.41,200.87 326.41,186.29C326.41,171.72 335.72,159.88 347.18,159.88C358.64,159.88 367.95,171.72 367.95,186.29Z"
62+
android:strokeWidth="8"
63+
android:strokeAlpha="1"
64+
android:strokeColor="#000000" />
65+
<path
66+
android:fillAlpha="0"
67+
android:fillColor="#FF000000"
68+
android:pathData="M437.44,211.4L480,109.09"
69+
android:strokeWidth="6"
70+
android:strokeAlpha="1"
71+
android:strokeColor="#303030" />
72+
<path
73+
android:fillAlpha="0"
74+
android:fillColor="#FF000000"
75+
android:pathData="M454.36,129.09C454.36,137.58 451.72,144.47 448.46,144.47C445.21,144.47 442.56,137.58 442.56,129.09C442.56,120.6 445.21,113.71 448.46,113.71C451.72,113.71 454.36,120.6 454.36,129.09Z"
76+
android:strokeWidth="6"
77+
android:strokeAlpha="1"
78+
android:strokeColor="#303030" />
79+
<path
80+
android:fillAlpha="0"
81+
android:fillColor="#FF000000"
82+
android:pathData="M477.18,190.37C477.18,198.86 474.54,205.76 471.28,205.76C468.03,205.76 465.38,198.86 465.38,190.37C465.38,181.88 468.03,174.99 471.28,174.99C474.54,174.99 477.18,181.88 477.18,190.37Z"
83+
android:strokeWidth="6"
84+
android:strokeAlpha="1"
85+
android:strokeColor="#303030" />
86+
</vector>

app/src/main/res/layout/.README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ Each layout must also be named based on sub features. Use following convention t
3434
* Prefix: **`_TBD_`** | Group
3535
- Prefix: **`_TBD_`** | Optimizer _(Added in 1.1)_
3636

37-
> NOTE: These feature set is taken from original readme.
37+
> NOTE: The list of feature set is taken from original [readme](https://github.com/amardeshbd/android-constraint-layout-cheatsheet/blob/master/README.md).
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (c) 2018 Hossain Khan
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<merge xmlns:tools="http://schemas.android.com/tools"
19+
xmlns:app="http://schemas.android.com/apk/res-auto"
20+
xmlns:android="http://schemas.android.com/apk/res/android"
21+
tools:showIn="@layout/preview_positioning_bias">
22+
23+
24+
<TextView
25+
android:layout_width="wrap_content"
26+
android:layout_height="wrap_content"
27+
android:layout_marginStart="8dp"
28+
android:text="Horizontal Bias = 20%\n(Towards Left)"
29+
app:layout_constraintBottom_toBottomOf="@+id/view_positioning_bias_20_percent"
30+
app:layout_constraintStart_toEndOf="@+id/view_positioning_bias_20_percent"
31+
app:layout_constraintTop_toTopOf="@+id/view_positioning_bias_20_percent" />
32+
33+
<TextView
34+
android:layout_width="wrap_content"
35+
android:layout_height="wrap_content"
36+
android:layout_marginTop="8dp"
37+
android:text="Vertical Bias = 85% (Towards Bottom)"
38+
app:layout_constraintEnd_toEndOf="@+id/view_positioning_bias_85_percent"
39+
app:layout_constraintStart_toStartOf="@+id/view_positioning_bias_85_percent"
40+
app:layout_constraintTop_toBottomOf="@+id/view_positioning_bias_85_percent" />
41+
</merge>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="utf-8"?><!--
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+
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
18+
xmlns:app="http://schemas.android.com/apk/res-auto"
19+
xmlns:tools="http://schemas.android.com/tools"
20+
android:layout_width="match_parent"
21+
android:layout_height="match_parent"
22+
android:foreground="@drawable/ic_grid_overlay"
23+
android:foregroundTint="@color/grid_tint"
24+
tools:context=".layoutpreview.LayoutPreviewBaseActivity">
25+
26+
<!--
27+
1st view that is horizontally biased towards the bottom using 85% bias
28+
-->
29+
<View
30+
android:id="@+id/view_positioning_bias_20_percent"
31+
style="@style/MediumBox"
32+
android:layout_marginTop="50dp"
33+
app:layout_constraintEnd_toEndOf="parent"
34+
app:layout_constraintHorizontal_bias="0.20"
35+
app:layout_constraintStart_toStartOf="parent"
36+
app:layout_constraintTop_toTopOf="parent" />
37+
38+
39+
<!--
40+
2nd view that is vertically biased towards the bottom using 85% bias
41+
-->
42+
<View
43+
android:id="@+id/view_positioning_bias_85_percent"
44+
style="@style/MediumBox.Purple"
45+
app:layout_constraintBottom_toBottomOf="parent"
46+
app:layout_constraintEnd_toEndOf="parent"
47+
app:layout_constraintStart_toStartOf="parent"
48+
app:layout_constraintTop_toTopOf="parent"
49+
app:layout_constraintVertical_bias="0.85" />
50+
51+
<!--
52+
Additional text views to explain the views - Ignore
53+
-->
54+
<include layout="@layout/include_layout_positioning_bias" />
55+
56+
57+
</android.support.constraint.ConstraintLayout>

app/src/main/res/values/styles.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<style name="MediumBox">
1212
<item name="android:layout_width">@dimen/box_size_medium</item>
1313
<item name="android:layout_height">@dimen/box_size_medium</item>
14-
<item name="android:background">@color/md_red_700</item>
14+
<item name="android:background">@color/md_amber_700</item>
1515
</style>
1616

1717
<style name="MediumBox.Purple">
280 KB
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)