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

Commit f94c9c4

Browse files
author
Nagesh Susarla
committed
Move ConstraintSet example to an activity
1. To make it easier to understand the ConstraintSet and its usage, move it to another activity. 2. Also move the landscape resource to res/xml/layout-land Change-Id: I405f7d2885186b6c249b092b395bbb40cb0ebd5f
1 parent d24c0b6 commit f94c9c4

8 files changed

Lines changed: 164 additions & 113 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
34
package="com.example.android.constraintlayoutexamples">
45

56
<application
67
android:allowBackup="true"
78
android:icon="@mipmap/ic_launcher"
89
android:label="@string/app_name"
910
android:supportsRtl="true"
10-
android:theme="@style/AppTheme">
11+
android:theme="@style/AppTheme"
12+
tools:ignore="GoogleAppIndexingWarning">
1113
<activity
1214
android:name=".MainActivity"
1315
android:configChanges="orientation|screenSize">
@@ -17,6 +19,7 @@
1719
<category android:name="android.intent.category.LAUNCHER" />
1820
</intent-filter>
1921
</activity>
22+
<activity android:name=".ConstraintSetExampleActivity" />
2023
</application>
2124

2225
</manifest>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (C) 2017 The Android Open Source Project
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.example.android.constraintlayoutexamples;
18+
19+
import android.os.Bundle;
20+
import android.support.constraint.ConstraintLayout;
21+
import android.support.constraint.ConstraintSet;
22+
import android.support.v7.app.AppCompatActivity;
23+
import android.transition.TransitionManager;
24+
import android.view.View;
25+
26+
/**
27+
* An example activity that show cases the use of {@link ConstraintSet}.
28+
* It starts out with a single ConstraintLayout which is then transitioned to
29+
* a different ConstraintLayout using {@link ConstraintSet#applyTo(ConstraintLayout)}.
30+
*/
31+
public class ConstraintSetExampleActivity extends AppCompatActivity {
32+
33+
private static final String SHOW_BIG_IMAGE = "showBigImage";
34+
35+
/**
36+
* Whether to show an enlarged image
37+
*/
38+
private boolean mShowBigImage = false;
39+
/**
40+
* The ConstraintLayout that any changes are applied to.
41+
*/
42+
private ConstraintLayout mRootLayout;
43+
/**
44+
* The ConstraintSet to use for the normal initial state
45+
*/
46+
private ConstraintSet mConstraintSetNormal = new ConstraintSet();
47+
/**
48+
* ConstraintSet to be applied on the normal ConstraintLayout to make the Image bigger.
49+
*/
50+
private ConstraintSet mConstraintSetBig = new ConstraintSet();
51+
52+
@Override
53+
protected void onCreate(Bundle savedInstanceState) {
54+
super.onCreate(savedInstanceState);
55+
setContentView(R.layout.constraintset_example_main);
56+
57+
mRootLayout = (ConstraintLayout) findViewById(R.id.activity_constraintset_example);
58+
// Note that this can also be achieved by calling
59+
// `mConstraintSetNormal.load(this, R.layout.constraintset_example_main);`
60+
// Since we already have an inflated ConstraintLayout in `mRootLayout`, clone() is
61+
// faster and considered the best practice.
62+
mConstraintSetNormal.clone(mRootLayout);
63+
// Load the constraints from the layout where ImageView is enlarged.
64+
mConstraintSetBig.load(this, R.layout.constraintset_example_big);
65+
66+
if (savedInstanceState != null) {
67+
boolean previous = savedInstanceState.getBoolean(SHOW_BIG_IMAGE);
68+
if (previous != mShowBigImage) {
69+
mShowBigImage = previous;
70+
applyConfig();
71+
}
72+
}
73+
}
74+
75+
@Override
76+
public void onSaveInstanceState(Bundle outState) {
77+
super.onSaveInstanceState(outState);
78+
outState.putBoolean(SHOW_BIG_IMAGE, mShowBigImage);
79+
}
80+
81+
// Method called when the ImageView within R.layout.constraintset_example_main
82+
// is clicked.
83+
public void toggleMode(View v) {
84+
TransitionManager.beginDelayedTransition(mRootLayout);
85+
mShowBigImage = !mShowBigImage;
86+
applyConfig();
87+
}
88+
89+
private void applyConfig() {
90+
if (mShowBigImage) {
91+
mConstraintSetBig.applyTo(mRootLayout);
92+
} else {
93+
mConstraintSetNormal.applyTo(mRootLayout);
94+
}
95+
}
96+
}
Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
1+
/*
2+
* Copyright (C) 2017 The Android Open Source Project
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+
117
package com.example.android.constraintlayoutexamples;
218

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;
19+
import android.content.Intent;
720
import android.os.Bundle;
8-
import android.transition.TransitionManager;
21+
import android.support.v7.app.AppCompatActivity;
922
import android.view.View;
1023

1124
public class MainActivity extends AppCompatActivity {
1225
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;
1826

1927
@Override
2028
protected void onCreate(Bundle savedInstanceState) {
@@ -27,14 +35,6 @@ public void show(View v) {
2735
int id = getResources().getIdentifier(tag, "layout", getPackageName());
2836
setContentView(id);
2937
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-
}
3838
}
3939

4040
@Override
@@ -47,31 +47,7 @@ public void onBackPressed() {
4747
}
4848
}
4949

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);
50+
public void showConstraintSetExample(View view) {
51+
startActivity(new Intent(this, ConstraintSetExampleActivity.class));
7652
}
7753
}

app/src/main/res/layout/constraint_example_7b.xml renamed to app/src/main/res/layout-land/constraintset_example_big.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
5-
android:id="@+id/activity_main"
5+
android:id="@+id/activity_constraintset_example"
66
android:layout_width="match_parent"
77
android:layout_height="match_parent" >
88

0 commit comments

Comments
 (0)