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

Commit f68c9aa

Browse files
authored
added an ability to launch all the layout (#618)
* added an ability to launch all the layout * minior cleanup
1 parent c4e3472 commit f68c9aa

File tree

10 files changed

+470
-51
lines changed

10 files changed

+470
-51
lines changed

constraintlayout/constraintlayout/src/main/java/androidx/constraintlayout/helper/widget/Grid.java

+26-23
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
import android.content.Context;
1919
import android.content.res.TypedArray;
2020
import android.util.AttributeSet;
21+
import android.util.Log;
2122
import android.util.Pair;
2223

24+
import androidx.constraintlayout.motion.widget.Debug;
2325
import androidx.constraintlayout.widget.ConstraintLayout;
2426
import androidx.constraintlayout.widget.ConstraintSet;
2527
import androidx.constraintlayout.widget.Guideline;
@@ -81,8 +83,8 @@
8183
*/
8284
public class Grid extends VirtualLayout {
8385
private static final String TAG = "Grid";
84-
private static final String VERTICAL = "vertical";
85-
private static final String HORIZONTAL = "horizontal";
86+
public static final int VERTICAL = 1;
87+
public static final int HORIZONTAL = 0;
8688
private final int mMaxRows = 50; // maximum number of rows can be specified.
8789
private final int mMaxColumns = 50; // maximum number of columns can be specified.
8890
private final ConstraintSet mConstraintSet = new ConstraintSet();
@@ -131,17 +133,17 @@ public class Grid extends VirtualLayout {
131133
/**
132134
* Horizontal gaps in Dp
133135
*/
134-
private int mHorizontalGaps;
136+
private float mHorizontalGaps;
135137

136138
/**
137139
* Vertical gaps in Dp
138140
*/
139-
private int mVerticalGaps;
141+
private float mVerticalGaps;
140142

141143
/**
142144
* orientation of the view arrangement - vertical or horizontal
143145
*/
144-
private String mOrientation;
146+
private int mOrientation = 0; // default value is horizontal
145147

146148
/**
147149
* Indicates what is the next available position to place an widget
@@ -207,11 +209,11 @@ protected void init(AttributeSet attrs) {
207209
} else if (attr == R.styleable.Grid_grid_columnWeights) {
208210
mStrColumnWeights = a.getString(attr);
209211
} else if (attr == R.styleable.Grid_grid_orientation) {
210-
mOrientation = a.getString(attr);
212+
mOrientation = a.getInt(attr,0);
211213
} else if (attr == R.styleable.Grid_grid_horizontalGaps) {
212-
mHorizontalGaps = a.getInteger(attr, 0);
214+
mHorizontalGaps = a.getDimension(attr, 0);
213215
} else if (attr == R.styleable.Grid_grid_verticalGaps) {
214-
mVerticalGaps = a.getInteger(attr, 0);
216+
mVerticalGaps = a.getDimension(attr, 0);
215217
} else if (attr == R.styleable.Grid_grid_validateInputs) {
216218
// @TODO handle validation
217219
mValidateInputs = a.getBoolean(attr, false);
@@ -220,7 +222,7 @@ protected void init(AttributeSet attrs) {
220222
mUseRtl = a.getBoolean(attr, false);
221223
}
222224
}
223-
225+
Log.v(TAG, " >>>>>>>>>>> col = "+mColumns);
224226
initVariables();
225227
a.recycle();
226228
}
@@ -387,26 +389,26 @@ private void updateGuideLinePosition(Guideline guideline, float position) {
387389
* @param column column position to place the view
388390
*/
389391
private void connectView(int viewId, int row, int column, int rowSpan, int columnSpan,
390-
int horizontalGaps, int verticalGaps) {
392+
float horizontalGaps, float verticalGaps) {
391393

392394
// @TODO handle RTL
393395
// connect Start of the view
394396
mConstraintSet.connect(viewId, ConstraintSet.START,
395-
mVerticalGuideLines[column].getId(), ConstraintSet.END, horizontalGaps);
397+
mVerticalGuideLines[column].getId(), ConstraintSet.END,(int) horizontalGaps);
396398

397399
// connect Top of the view
398400
mConstraintSet.connect(viewId, ConstraintSet.TOP,
399-
mHorizontalGuideLines[row].getId(), ConstraintSet.BOTTOM, verticalGaps);
401+
mHorizontalGuideLines[row].getId(), ConstraintSet.BOTTOM, (int) verticalGaps);
400402

401403
// connect End of the view
402404
mConstraintSet.connect(viewId, ConstraintSet.END,
403405
mVerticalGuideLines[column + columnSpan].getId(),
404-
ConstraintSet.START, horizontalGaps);
406+
ConstraintSet.START, (int) horizontalGaps);
405407

406408
// connect Bottom of the view
407409
mConstraintSet.connect(viewId, ConstraintSet.BOTTOM,
408410
mHorizontalGuideLines[row + rowSpan].getId(),
409-
ConstraintSet.TOP, verticalGaps);
411+
ConstraintSet.TOP,(int) verticalGaps);
410412
}
411413

412414
/**
@@ -444,7 +446,7 @@ private Pair<Integer, Integer> getPositionByIndex(int index) {
444446
int row;
445447
int col;
446448

447-
if (mOrientation.equals(VERTICAL)) {
449+
if (mOrientation == 1) {
448450
row = index % mRows;
449451
col = index / mRows;
450452
} else {
@@ -662,6 +664,7 @@ public int getColumns() {
662664
* @return true if it succeeds otherwise false
663665
*/
664666
public boolean setColumns(int columns) {
667+
Debug.logStack(TAG, " >>>>>>>>>>>>> col " + columns, 5);
665668
if (columns < 2 || columns > mMaxColumns) {
666669
return false;
667670
}
@@ -682,7 +685,7 @@ public boolean setColumns(int columns) {
682685
* get the value of orientation
683686
* @return the value of orientation
684687
*/
685-
public String getOrientation() {
688+
public int getOrientation() {
686689
return mOrientation;
687690
}
688691

@@ -691,12 +694,12 @@ public String getOrientation() {
691694
* @param orientation new orientation value
692695
* @return true if it succeeds otherwise false
693696
*/
694-
public boolean setOrientation(String orientation) {
695-
if (!(orientation.equals(HORIZONTAL) || orientation.equals(VERTICAL))) {
697+
public boolean setOrientation(int orientation) {
698+
if (!(orientation == HORIZONTAL || orientation == VERTICAL)) {
696699
return false;
697700
}
698701

699-
if (mOrientation != null && mOrientation.equals(orientation)) {
702+
if (mOrientation == orientation) {
700703
return true;
701704
}
702705

@@ -823,7 +826,7 @@ public Boolean setColumnWeights(String columnWeights) {
823826
* get the value of horizontalGaps
824827
* @return the value of horizontalGaps
825828
*/
826-
public int getHorizontalGaps() {
829+
public float getHorizontalGaps() {
827830
return mHorizontalGaps;
828831
}
829832

@@ -832,7 +835,7 @@ public int getHorizontalGaps() {
832835
* @param horizontalGaps new horizontalGaps value
833836
* @return true if it succeeds otherwise false
834837
*/
835-
public boolean setHorizontalGaps(int horizontalGaps) {
838+
public boolean setHorizontalGaps(float horizontalGaps) {
836839
if (horizontalGaps < 0) {
837840
return false;
838841
}
@@ -851,7 +854,7 @@ public boolean setHorizontalGaps(int horizontalGaps) {
851854
* get the value of verticalGaps
852855
* @return the value of verticalGaps
853856
*/
854-
public int getVerticalGaps() {
857+
public float getVerticalGaps() {
855858
return mVerticalGaps;
856859
}
857860

@@ -860,7 +863,7 @@ public int getVerticalGaps() {
860863
* @param verticalGaps new verticalGaps value
861864
* @return true if it succeeds otherwise false
862865
*/
863-
public boolean setVerticalGaps(int verticalGaps) {
866+
public boolean setVerticalGaps(float verticalGaps) {
864867
if (verticalGaps < 0) {
865868
return false;
866869
}

constraintlayout/constraintlayout/src/main/res/values/attrs.xml

+6-3
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,12 @@
310310
<attr name="grid_skips" format="string"/>
311311
<attr name="grid_rowWeights" format="string"/>
312312
<attr name="grid_columnWeights" format="string"/>
313-
<attr name="grid_orientation" format="string"/>
314-
<attr name="grid_horizontalGaps" format="integer" />
315-
<attr name="grid_verticalGaps" format="integer" />
313+
<attr name="grid_orientation" format="enum">
314+
<enum name="horizontal" value="0" />
315+
<enum name="vertical" value="1" />
316+
</attr>
317+
<attr name="grid_horizontalGaps" format="dimension" />
318+
<attr name="grid_verticalGaps" format="dimension" />
316319
<attr name="grid_validateInputs" format="boolean" />
317320
<attr name="grid_useRtl" format="boolean" />
318321
</declare-styleable>

projects/GridExperiments/app/src/main/java/android/support/constraint/app/MainActivity.java

+115-17
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,23 @@
1616

1717
package android.support.constraint.app;
1818

19+
import android.content.Intent;
1920
import android.os.Bundle;
21+
import android.util.Log;
2022
import android.view.View;
23+
import android.widget.Button;
24+
import android.widget.LinearLayout;
25+
import android.widget.TextView;
2126

2227
import androidx.appcompat.app.AppCompatActivity;
2328
import androidx.constraintlayout.helper.widget.Grid;
2429

30+
import java.lang.reflect.Field;
31+
import java.util.ArrayList;
32+
import java.util.Arrays;
33+
import java.util.Calendar;
34+
import java.util.GregorianCalendar;
35+
2536
/**
2637
* MainActivity for Grid helper
2738
*/
@@ -38,32 +49,117 @@ public class MainActivity extends AppCompatActivity {
3849
private int mColumnWeightsIndex = 0;
3950
private int mSkipsIndex = 0;
4051
private int mSpansIndex = 0;
41-
private int mOrientationIndex = 0;
52+
private int mOrientation = Grid.HORIZONTAL;
4253

4354
private int[] mGaps = new int[] {0, 20};
4455
private int[] mSizes = new int[] {3, 4};
4556
private String[] mThreeWeights = new String[] {"1,1,1", "1,2,2"};
4657
private String[] mFourWeights = new String[] {"1,1,1,1", "1,2,1,1"};
4758
private String[] mSkips = new String[] {"", "0:1x2, 4:1x1"};
4859
private String[] mSpans = new String[] {"", "6:1x2"};
49-
private String[] mOrientations = new String[] {"horizontal", "vertical"};
50-
60+
private static String LAYOUT_TO_USE = "layout";
5161

5262
@Override
5363
protected void onCreate(Bundle savedInstanceState) {
5464
super.onCreate(savedInstanceState);
55-
setContentView(R.layout.responsive);
65+
Bundle extra = getIntent().getExtras();
66+
if (extra == null) {
67+
LinearLayout linearLayout = new LinearLayout(this,null);
68+
linearLayout.setOrientation(LinearLayout.VERTICAL);
69+
setContentView(linearLayout);
5670

57-
mGrid = findViewById(R.id.grid);
58-
mGrid.setRows(mSizes[0]);
59-
mGrid.setColumns(mSizes[0]);
60-
mGrid.setRowWeights(mThreeWeights[0]);
61-
mGrid.setColumnWeights(mThreeWeights[0]);
62-
mGrid.setSkips(mSkips[0]);
63-
mGrid.setSpans(mSpans[0]);
64-
mGrid.setVerticalGaps(mGaps[0]);
65-
mGrid.setHorizontalGaps(mGaps[0]);
66-
mGrid.setOrientation(mOrientations[0]);
71+
int []layouts = getLayouts();// you could { R.layout.responsive, R.layout.activity_main,
72+
for (int i = 0; i < layouts.length; i++) {
73+
final int layout = layouts[i];
74+
Button button = new Button(this,null);
75+
button.setText(getResources().getResourceEntryName(layout));
76+
linearLayout.addView(button);
77+
button.setOnClickListener(v->{
78+
Intent intent= new Intent( this,MainActivity.class);
79+
intent.putExtra(LAYOUT_TO_USE , layout );
80+
startActivity(intent);
81+
});
82+
}
83+
return;
84+
}
85+
int layout = extra.getInt(LAYOUT_TO_USE);
86+
setContentView(layout);
87+
if (layout == R.layout.calendar) {
88+
fillCal(0);
89+
}
90+
// mGrid = findViewById(R.id.grid);
91+
// mGrid.setRows(mSizes[0]);
92+
// mGrid.setColumns(mSizes[0]);
93+
// mGrid.setRowWeights(mThreeWeights[0]);
94+
// mGrid.setColumnWeights(mThreeWeights[0]);
95+
// mGrid.setSkips(mSkips[0]);
96+
// mGrid.setSpans(mSpans[0]);
97+
// mGrid.setVerticalGaps(mGaps[0]);
98+
// mGrid.setHorizontalGaps(mGaps[0]);
99+
// mGrid.setOrientation(mOrientation);
100+
}
101+
int mMonth = 0;
102+
public void nextPrev(View v) {
103+
if (((TextView)v).getText().equals("<")) {
104+
fillCal(++mMonth);
105+
}
106+
fillCal(--mMonth);
107+
}
108+
void fillCal(int month_offset) {
109+
String[] days = {"S", "M", "T", "W", "T", "F", "S"};
110+
Field[] f = R.id.class.getDeclaredFields();
111+
for (int i = 0; i < f.length; i++) {
112+
String name = f[i].getName();
113+
if (name.startsWith("date") && name.length() == "date00".length()) {
114+
try {
115+
TextView tv = findViewById(f[i].getInt(null));
116+
if (tv == null) {
117+
Log.v("main", " >>>>>>>>>>> " + name);
118+
continue;
119+
}
120+
if (name.startsWith("date0")) {
121+
int p = name.substring("date0".length()).charAt(0) - '0';
122+
tv.setText(days[p]);
123+
} else {
124+
int col = name.substring("date".length()).charAt(0) - '1';
125+
int row = name.substring("date".length()).charAt(1) - '0';
126+
int pos = row + col * 7;
127+
Calendar cal = Calendar.getInstance();
128+
cal.add(Calendar.MONTH, month_offset);
129+
cal.set(Calendar.DAY_OF_MONTH, 1);
130+
int offset = cal.get(Calendar.DAY_OF_WEEK) - 1;
131+
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
132+
if (offset > pos || pos - offset >= lastDay) {
133+
tv.setText("");
134+
} else {
135+
tv.setText("" + (pos - offset + 1));
136+
}
137+
138+
}
139+
} catch (IllegalAccessException e) {
140+
e.printStackTrace();
141+
}
142+
}
143+
}
144+
}
145+
146+
private static int[] getLayouts( ) {
147+
ArrayList<String> list = new ArrayList<>();
148+
Field[] f = R.layout.class.getDeclaredFields();
149+
150+
int []ret = new int[f.length];
151+
int count = 0;
152+
for (int i = 0; i < f.length; i++) {
153+
try {
154+
String name = f[i].getName();
155+
if (!name.contains("_") || name.equals("activity_main")) {
156+
ret[count++] = f[i].getInt(null);
157+
}
158+
} catch (IllegalAccessException e) {
159+
e.printStackTrace();
160+
}
161+
}
162+
return Arrays.copyOf(ret,count);
67163
}
68164

69165
/**
@@ -146,7 +242,9 @@ public void toggleSpans(View v) {
146242
* @param v
147243
*/
148244
public void toggleOrientation(View v) {
149-
mGrid.setOrientation(mOrientations[++mOrientationIndex % 2]);
245+
mOrientation = mGrid.getOrientation() == Grid.HORIZONTAL
246+
? Grid.VERTICAL : Grid.HORIZONTAL;
247+
mGrid.setOrientation(mOrientation);
150248
}
151249

152250
/**
@@ -180,7 +278,7 @@ public void restore(View v) {
180278
mColumnWeightsIndex = 0;
181279
mSkipsIndex = 0;
182280
mSpansIndex = 0;
183-
mOrientationIndex = 0;
281+
mOrientation = Grid.VERTICAL;
184282

185283
mGrid = findViewById(R.id.grid);
186284
mGrid.setRows(mSizes[mRowIndex]);
@@ -191,6 +289,6 @@ public void restore(View v) {
191289
mGrid.setSpans(mSpans[mSpansIndex]);
192290
mGrid.setVerticalGaps(mGaps[mVerticalGapsIndex]);
193291
mGrid.setHorizontalGaps(mGaps[mHorizontalGapsIndex]);
194-
mGrid.setOrientation(mOrientations[mOrientationIndex]);
292+
mGrid.setOrientation(mOrientation);
195293
}
196294
}

projects/GridExperiments/app/src/main/res/layout/activity_main.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
app:grid_columns="3"
1414
app:grid_rows="3"
1515
app:grid_columnWeights="3,1,2"
16-
app:grid_horizontalGaps="10"
17-
app:grid_verticalGaps="10"
16+
app:grid_horizontalGaps="10dp"
17+
app:grid_verticalGaps="10dp"
1818
app:grid_orientation="horizontal"
1919
app:grid_skips="0:1x2,4:1x1,6:1x1" />
2020

projects/GridExperiments/app/src/main/res/layout/calculator.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
app:constraint_referenced_ids="calculatorText,btn0,clear,neg,percent,div,btn7,btn8,btn9,mult,btn4,btn5,btn6,sub,btn1,btn2,btn3,plus,btn0,dot,equal"
1313
app:grid_columns="4"
1414
app:grid_rows="7"
15-
app:grid_horizontalGaps="5"
16-
app:grid_verticalGaps="5"
15+
app:grid_horizontalGaps="5dp"
16+
app:grid_verticalGaps="5dp"
1717
app:grid_orientation="horizontal"
1818
app:grid_spans="0:2x4,24:1x2" />
1919

0 commit comments

Comments
 (0)