Skip to content
This repository was archived by the owner on Nov 21, 2019. It is now read-only.

Commit 2222065

Browse files
author
Nagesh Susarla
committed
Examples for ConstraintLayout
Initial commit Change-Id: I7611ae34df904b2e8b6adbae225d49c6fe634c1f
1 parent c4b8355 commit 2222065

38 files changed

Lines changed: 1799 additions & 0 deletions

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.iml
2+
.gradle
3+
local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
.externalNativeBuild

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.2"
6+
defaultConfig {
7+
applicationId "com.example.android.constraintlayoutexamples"
8+
minSdkVersion 19
9+
targetSdkVersion 25
10+
versionCode 1
11+
versionName "1.0"
12+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25+
exclude group: 'com.android.support', module: 'support-annotations'
26+
})
27+
compile 'com.android.support:appcompat-v7:25.1.0'
28+
testCompile 'junit:junit:4.12'
29+
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
30+
}

app/proguard-rules.pro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/nageshs/Library/Android/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.example.android.constraintlayoutexamples;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumentation test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.example.android.constraintlayoutexamples", appContext.getPackageName());
25+
}
26+
}

app/src/main/AndroidManifest.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.android.constraintlayoutexamples">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:supportsRtl="true"
10+
android:theme="@style/AppTheme">
11+
<activity
12+
android:name=".MainActivity"
13+
android:configChanges="orientation|screenSize">
14+
<intent-filter>
15+
<action android:name="android.intent.action.MAIN" />
16+
17+
<category android:name="android.intent.category.LAUNCHER" />
18+
</intent-filter>
19+
</activity>
20+
</application>
21+
22+
</manifest>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.example.android.constraintlayoutexamples;
2+
3+
import android.content.res.Configuration;
4+
import android.support.constraint.ConstraintLayout;
5+
import android.support.constraint.ConstraintSet;
6+
import android.support.v7.app.AppCompatActivity;
7+
import android.os.Bundle;
8+
import android.transition.TransitionManager;
9+
import android.view.View;
10+
11+
public class MainActivity extends AppCompatActivity {
12+
private boolean mShowingLayout = false;
13+
ConstraintSet mSetNormal = new ConstraintSet();
14+
ConstraintSet mSetBig = new ConstraintSet();
15+
ConstraintSet mSetBigLandscape = new ConstraintSet();
16+
ConstraintLayout mLayout;
17+
boolean mBig = false;
18+
19+
@Override
20+
protected void onCreate(Bundle savedInstanceState) {
21+
super.onCreate(savedInstanceState);
22+
setContentView(R.layout.activity_main);
23+
}
24+
25+
public void show(View v) {
26+
String tag = (String) v.getTag();
27+
int id = getResources().getIdentifier(tag, "layout", getPackageName());
28+
setContentView(id);
29+
mShowingLayout = true;
30+
if (tag.equals("constraint_example_7")) {
31+
mLayout = (ConstraintLayout) findViewById(R.id.activity_main);
32+
mSetNormal.load(this, R.layout.constraint_example_7);
33+
mSetBig.load(this, R.layout.constraint_example_7a);
34+
mSetBigLandscape.load(this, R.layout.constraint_example_7b);
35+
} else {
36+
mLayout = null;
37+
}
38+
}
39+
40+
@Override
41+
public void onBackPressed() {
42+
if (mShowingLayout) {
43+
setContentView(R.layout.activity_main);
44+
mShowingLayout = false;
45+
} else {
46+
super.onBackPressed();
47+
}
48+
}
49+
50+
public void toggleMode(View v) {
51+
TransitionManager.beginDelayedTransition(mLayout);
52+
mBig = !mBig;
53+
int orientation = getResources().getConfiguration().orientation;
54+
layout7(orientation == Configuration.ORIENTATION_LANDSCAPE);
55+
}
56+
57+
private void layout7(boolean landscape) {
58+
if (mLayout == null) {
59+
return;
60+
}
61+
if (mBig) {
62+
if (landscape) {
63+
mSetBigLandscape.applyTo(mLayout);
64+
} else {
65+
mSetBig.applyTo(mLayout);
66+
}
67+
} else {
68+
mSetNormal.applyTo(mLayout);
69+
}
70+
}
71+
72+
@Override
73+
public void onConfigurationChanged(Configuration newConfig) {
74+
super.onConfigurationChanged(newConfig);
75+
layout7(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE);
76+
}
77+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="#FF000000"
8+
android:pathData="M6,18c0,0.55 0.45,1 1,1h1v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L11,19h2v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L16,19h1c0.55,0 1,-0.45 1,-1L18,8L6,8v10zM3.5,8C2.67,8 2,8.67 2,9.5v7c0,0.83 0.67,1.5 1.5,1.5S5,17.33 5,16.5v-7C5,8.67 4.33,8 3.5,8zM20.5,8c-0.83,0 -1.5,0.67 -1.5,1.5v7c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5v-7c0,-0.83 -0.67,-1.5 -1.5,-1.5zM15.53,2.16l1.3,-1.3c0.2,-0.2 0.2,-0.51 0,-0.71 -0.2,-0.2 -0.51,-0.2 -0.71,0l-1.48,1.48C13.85,1.23 12.95,1 12,1c-0.96,0 -1.86,0.23 -2.66,0.63L7.85,0.15c-0.2,-0.2 -0.51,-0.2 -0.71,0 -0.2,0.2 -0.2,0.51 0,0.71l1.31,1.31C6.97,3.26 6,5.01 6,7h12c0,-1.99 -0.97,-3.75 -2.47,-4.84zM10,5L9,5L9,4h1v1zM15,5h-1L14,4h1v1z"/>
9+
</vector>

app/src/main/res/drawable/lake.jpg

758 KB
Loading
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/activity_main"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
tools:context="com.example.android.constraintlayoutexamples.MainActivity">
9+
10+
<TextView
11+
android:id="@+id/textView"
12+
android:layout_width="0dp"
13+
android:layout_height="wrap_content"
14+
android:layout_marginEnd="16dp"
15+
android:layout_marginStart="16dp"
16+
android:layout_marginTop="40dp"
17+
android:text="@string/constraint_layout_example"
18+
android:textAlignment="center"
19+
android:textSize="30sp"
20+
app:layout_constraintLeft_toLeftOf="parent"
21+
app:layout_constraintRight_toRightOf="parent"
22+
app:layout_constraintTop_toTopOf="parent"
23+
tools:layout_constraintLeft_creator="1"
24+
tools:layout_constraintRight_creator="1"
25+
tools:layout_constraintTop_creator="1" />
26+
27+
<Button
28+
android:id="@+id/button"
29+
android:layout_width="0dp"
30+
android:layout_height="wrap_content"
31+
android:layout_marginBottom="8dp"
32+
android:layout_marginEnd="16dp"
33+
android:layout_marginStart="16dp"
34+
android:onClick="show"
35+
android:tag="constraint_example_1"
36+
android:text="Basic Use"
37+
app:layout_constraintBottom_toTopOf="@+id/button2"
38+
app:layout_constraintLeft_toLeftOf="parent"
39+
app:layout_constraintRight_toRightOf="parent"
40+
tools:layout_constraintBottom_creator="1"
41+
tools:layout_constraintLeft_creator="1"
42+
tools:layout_constraintRight_creator="1" />
43+
44+
<Button
45+
android:id="@+id/button2"
46+
android:layout_width="0dp"
47+
android:layout_height="wrap_content"
48+
android:layout_marginBottom="12dp"
49+
android:onClick="show"
50+
android:tag="constraint_example_2"
51+
android:text="Basic Arange"
52+
app:layout_constraintBottom_toTopOf="@+id/button30"
53+
app:layout_constraintLeft_toLeftOf="@+id/button"
54+
app:layout_constraintRight_toRightOf="@+id/button"
55+
tools:layout_constraintBottom_creator="1"
56+
tools:layout_constraintLeft_creator="1"
57+
tools:layout_constraintRight_creator="1" />
58+
59+
<Button
60+
android:id="@+id/button15"
61+
android:layout_width="0dp"
62+
android:layout_height="wrap_content"
63+
android:layout_marginBottom="16dp"
64+
android:onClick="show"
65+
android:tag="constraint_example_4"
66+
android:text="Advanced Arangement"
67+
app:layout_constraintBottom_toTopOf="@+id/button18"
68+
app:layout_constraintLeft_toLeftOf="@+id/button30"
69+
app:layout_constraintRight_toRightOf="@+id/button30"
70+
tools:layout_constraintBottom_creator="1"
71+
tools:layout_constraintLeft_creator="1"
72+
tools:layout_constraintRight_creator="1" />
73+
74+
<Button
75+
android:id="@+id/button18"
76+
android:layout_width="0dp"
77+
android:layout_height="wrap_content"
78+
android:layout_marginBottom="8dp"
79+
android:onClick="show"
80+
android:tag="constraint_example_5"
81+
android:text="Aspect Ratio"
82+
app:layout_constraintBottom_toTopOf="@+id/button19"
83+
app:layout_constraintLeft_toLeftOf="@+id/button15"
84+
app:layout_constraintRight_toRightOf="@+id/button15"
85+
tools:layout_constraintBottom_creator="1"
86+
tools:layout_constraintLeft_creator="1"
87+
tools:layout_constraintRight_creator="1" />
88+
89+
<Button
90+
android:id="@+id/button19"
91+
android:layout_width="0dp"
92+
android:layout_height="wrap_content"
93+
android:layout_marginBottom="8dp"
94+
android:onClick="show"
95+
android:tag="constraint_example_6"
96+
android:text="Basic Chains"
97+
app:layout_constraintBottom_toTopOf="@+id/button20"
98+
app:layout_constraintLeft_toLeftOf="@+id/button18"
99+
app:layout_constraintRight_toRightOf="@+id/button18"
100+
tools:layout_constraintBottom_creator="1"
101+
tools:layout_constraintLeft_creator="1"
102+
tools:layout_constraintRight_creator="1" />
103+
104+
<Button
105+
android:id="@+id/button20"
106+
android:layout_width="0dp"
107+
android:layout_height="wrap_content"
108+
android:layout_marginBottom="8dp"
109+
android:onClick="show"
110+
android:tag="constraint_example_7"
111+
android:text="ConstraintSet"
112+
app:layout_constraintBottom_toTopOf="@+id/button21"
113+
app:layout_constraintLeft_toLeftOf="@+id/button19"
114+
app:layout_constraintRight_toRightOf="@+id/button19"
115+
tools:layout_constraintBottom_creator="1"
116+
tools:layout_constraintLeft_creator="1"
117+
tools:layout_constraintRight_creator="1" />
118+
119+
<Button
120+
android:id="@+id/button21"
121+
android:layout_width="0dp"
122+
android:layout_height="wrap_content"
123+
android:layout_marginBottom="22dp"
124+
android:onClick="show"
125+
android:tag="constraint_example_x1"
126+
android:text="Advanced Chains"
127+
app:layout_constraintBottom_toBottomOf="parent"
128+
app:layout_constraintLeft_toLeftOf="@+id/button20"
129+
app:layout_constraintRight_toRightOf="@+id/button20"
130+
tools:layout_constraintBottom_creator="1"
131+
tools:layout_constraintLeft_creator="1"
132+
tools:layout_constraintRight_creator="1" />
133+
134+
<Button
135+
android:id="@+id/button30"
136+
android:layout_width="0dp"
137+
android:layout_height="wrap_content"
138+
android:layout_marginBottom="13dp"
139+
android:onClick="show"
140+
android:tag="constraint_example_3"
141+
android:text="Guidelines"
142+
app:layout_constraintBottom_toTopOf="@+id/button15"
143+
app:layout_constraintLeft_toLeftOf="@+id/button2"
144+
app:layout_constraintRight_toRightOf="@+id/button2"
145+
tools:layout_constraintBottom_creator="1"
146+
tools:layout_constraintLeft_creator="1"
147+
tools:layout_constraintRight_creator="1" />
148+
</android.support.constraint.ConstraintLayout>

0 commit comments

Comments
 (0)