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

added an ability to launch all the layout #618

Merged
merged 2 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Pair;

import androidx.constraintlayout.motion.widget.Debug;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;
import androidx.constraintlayout.widget.Guideline;
Expand Down Expand Up @@ -81,8 +83,8 @@
*/
public class Grid extends VirtualLayout {
private static final String TAG = "Grid";
private static final String VERTICAL = "vertical";
private static final String HORIZONTAL = "horizontal";
public static final int VERTICAL = 1;
public static final int HORIZONTAL = 0;
private final int mMaxRows = 50; // maximum number of rows can be specified.
private final int mMaxColumns = 50; // maximum number of columns can be specified.
private final ConstraintSet mConstraintSet = new ConstraintSet();
Expand Down Expand Up @@ -131,17 +133,17 @@ public class Grid extends VirtualLayout {
/**
* Horizontal gaps in Dp
*/
private int mHorizontalGaps;
private float mHorizontalGaps;

/**
* Vertical gaps in Dp
*/
private int mVerticalGaps;
private float mVerticalGaps;

/**
* orientation of the view arrangement - vertical or horizontal
*/
private String mOrientation;
private int mOrientation = 0; // default value is horizontal

/**
* Indicates what is the next available position to place an widget
Expand Down Expand Up @@ -207,11 +209,11 @@ protected void init(AttributeSet attrs) {
} else if (attr == R.styleable.Grid_grid_columnWeights) {
mStrColumnWeights = a.getString(attr);
} else if (attr == R.styleable.Grid_grid_orientation) {
mOrientation = a.getString(attr);
mOrientation = a.getInt(attr,0);
} else if (attr == R.styleable.Grid_grid_horizontalGaps) {
mHorizontalGaps = a.getInteger(attr, 0);
mHorizontalGaps = a.getDimension(attr, 0);
} else if (attr == R.styleable.Grid_grid_verticalGaps) {
mVerticalGaps = a.getInteger(attr, 0);
mVerticalGaps = a.getDimension(attr, 0);
} else if (attr == R.styleable.Grid_grid_validateInputs) {
// @TODO handle validation
mValidateInputs = a.getBoolean(attr, false);
Expand All @@ -220,7 +222,7 @@ protected void init(AttributeSet attrs) {
mUseRtl = a.getBoolean(attr, false);
}
}

Log.v(TAG, " >>>>>>>>>>> col = "+mColumns);
initVariables();
a.recycle();
}
Expand Down Expand Up @@ -387,26 +389,26 @@ private void updateGuideLinePosition(Guideline guideline, float position) {
* @param column column position to place the view
*/
private void connectView(int viewId, int row, int column, int rowSpan, int columnSpan,
int horizontalGaps, int verticalGaps) {
float horizontalGaps, float verticalGaps) {

// @TODO handle RTL
// connect Start of the view
mConstraintSet.connect(viewId, ConstraintSet.START,
mVerticalGuideLines[column].getId(), ConstraintSet.END, horizontalGaps);
mVerticalGuideLines[column].getId(), ConstraintSet.END,(int) horizontalGaps);

// connect Top of the view
mConstraintSet.connect(viewId, ConstraintSet.TOP,
mHorizontalGuideLines[row].getId(), ConstraintSet.BOTTOM, verticalGaps);
mHorizontalGuideLines[row].getId(), ConstraintSet.BOTTOM, (int) verticalGaps);

// connect End of the view
mConstraintSet.connect(viewId, ConstraintSet.END,
mVerticalGuideLines[column + columnSpan].getId(),
ConstraintSet.START, horizontalGaps);
ConstraintSet.START, (int) horizontalGaps);

// connect Bottom of the view
mConstraintSet.connect(viewId, ConstraintSet.BOTTOM,
mHorizontalGuideLines[row + rowSpan].getId(),
ConstraintSet.TOP, verticalGaps);
ConstraintSet.TOP,(int) verticalGaps);
}

/**
Expand Down Expand Up @@ -444,7 +446,7 @@ private Pair<Integer, Integer> getPositionByIndex(int index) {
int row;
int col;

if (mOrientation.equals(VERTICAL)) {
if (mOrientation == 1) {
row = index % mRows;
col = index / mRows;
} else {
Expand Down Expand Up @@ -662,6 +664,7 @@ public int getColumns() {
* @return true if it succeeds otherwise false
*/
public boolean setColumns(int columns) {
Debug.logStack(TAG, " >>>>>>>>>>>>> col " + columns, 5);
if (columns < 2 || columns > mMaxColumns) {
return false;
}
Expand All @@ -682,7 +685,7 @@ public boolean setColumns(int columns) {
* get the value of orientation
* @return the value of orientation
*/
public String getOrientation() {
public int getOrientation() {
return mOrientation;
}

Expand All @@ -691,12 +694,12 @@ public String getOrientation() {
* @param orientation new orientation value
* @return true if it succeeds otherwise false
*/
public boolean setOrientation(String orientation) {
if (!(orientation.equals(HORIZONTAL) || orientation.equals(VERTICAL))) {
public boolean setOrientation(int orientation) {
if (!(orientation == HORIZONTAL || orientation == VERTICAL)) {
return false;
}

if (mOrientation != null && mOrientation.equals(orientation)) {
if (mOrientation == orientation) {
return true;
}

Expand Down Expand Up @@ -823,7 +826,7 @@ public Boolean setColumnWeights(String columnWeights) {
* get the value of horizontalGaps
* @return the value of horizontalGaps
*/
public int getHorizontalGaps() {
public float getHorizontalGaps() {
return mHorizontalGaps;
}

Expand All @@ -832,7 +835,7 @@ public int getHorizontalGaps() {
* @param horizontalGaps new horizontalGaps value
* @return true if it succeeds otherwise false
*/
public boolean setHorizontalGaps(int horizontalGaps) {
public boolean setHorizontalGaps(float horizontalGaps) {
if (horizontalGaps < 0) {
return false;
}
Expand All @@ -851,7 +854,7 @@ public boolean setHorizontalGaps(int horizontalGaps) {
* get the value of verticalGaps
* @return the value of verticalGaps
*/
public int getVerticalGaps() {
public float getVerticalGaps() {
return mVerticalGaps;
}

Expand All @@ -860,7 +863,7 @@ public int getVerticalGaps() {
* @param verticalGaps new verticalGaps value
* @return true if it succeeds otherwise false
*/
public boolean setVerticalGaps(int verticalGaps) {
public boolean setVerticalGaps(float verticalGaps) {
if (verticalGaps < 0) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,12 @@
<attr name="grid_skips" format="string"/>
<attr name="grid_rowWeights" format="string"/>
<attr name="grid_columnWeights" format="string"/>
<attr name="grid_orientation" format="string"/>
<attr name="grid_horizontalGaps" format="integer" />
<attr name="grid_verticalGaps" format="integer" />
<attr name="grid_orientation" format="enum">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
<attr name="grid_horizontalGaps" format="dimension" />
<attr name="grid_verticalGaps" format="dimension" />
<attr name="grid_validateInputs" format="boolean" />
<attr name="grid_useRtl" format="boolean" />
</declare-styleable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,23 @@

package android.support.constraint.app;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.helper.widget.Grid;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
* MainActivity for Grid helper
*/
Expand All @@ -38,32 +49,117 @@ public class MainActivity extends AppCompatActivity {
private int mColumnWeightsIndex = 0;
private int mSkipsIndex = 0;
private int mSpansIndex = 0;
private int mOrientationIndex = 0;
private int mOrientation = Grid.HORIZONTAL;

private int[] mGaps = new int[] {0, 20};
private int[] mSizes = new int[] {3, 4};
private String[] mThreeWeights = new String[] {"1,1,1", "1,2,2"};
private String[] mFourWeights = new String[] {"1,1,1,1", "1,2,1,1"};
private String[] mSkips = new String[] {"", "0:1x2, 4:1x1"};
private String[] mSpans = new String[] {"", "6:1x2"};
private String[] mOrientations = new String[] {"horizontal", "vertical"};

private static String LAYOUT_TO_USE = "layout";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.responsive);
Bundle extra = getIntent().getExtras();
if (extra == null) {
LinearLayout linearLayout = new LinearLayout(this,null);
linearLayout.setOrientation(LinearLayout.VERTICAL);
setContentView(linearLayout);

mGrid = findViewById(R.id.grid);
mGrid.setRows(mSizes[0]);
mGrid.setColumns(mSizes[0]);
mGrid.setRowWeights(mThreeWeights[0]);
mGrid.setColumnWeights(mThreeWeights[0]);
mGrid.setSkips(mSkips[0]);
mGrid.setSpans(mSpans[0]);
mGrid.setVerticalGaps(mGaps[0]);
mGrid.setHorizontalGaps(mGaps[0]);
mGrid.setOrientation(mOrientations[0]);
int []layouts = getLayouts();// you could { R.layout.responsive, R.layout.activity_main,
for (int i = 0; i < layouts.length; i++) {
final int layout = layouts[i];
Button button = new Button(this,null);
button.setText(getResources().getResourceEntryName(layout));
linearLayout.addView(button);
button.setOnClickListener(v->{
Intent intent= new Intent( this,MainActivity.class);
intent.putExtra(LAYOUT_TO_USE , layout );
startActivity(intent);
});
}
return;
}
int layout = extra.getInt(LAYOUT_TO_USE);
setContentView(layout);
if (layout == R.layout.calendar) {
fillCal(0);
}
// mGrid = findViewById(R.id.grid);
// mGrid.setRows(mSizes[0]);
// mGrid.setColumns(mSizes[0]);
// mGrid.setRowWeights(mThreeWeights[0]);
// mGrid.setColumnWeights(mThreeWeights[0]);
// mGrid.setSkips(mSkips[0]);
// mGrid.setSpans(mSpans[0]);
// mGrid.setVerticalGaps(mGaps[0]);
// mGrid.setHorizontalGaps(mGaps[0]);
// mGrid.setOrientation(mOrientation);
}
int mMonth = 0;
public void nextPrev(View v) {
if (((TextView)v).getText().equals("<")) {
fillCal(++mMonth);
}
fillCal(--mMonth);
}
void fillCal(int month_offset) {
String[] days = {"S", "M", "T", "W", "T", "F", "S"};
Field[] f = R.id.class.getDeclaredFields();
for (int i = 0; i < f.length; i++) {
String name = f[i].getName();
if (name.startsWith("date") && name.length() == "date00".length()) {
try {
TextView tv = findViewById(f[i].getInt(null));
if (tv == null) {
Log.v("main", " >>>>>>>>>>> " + name);
continue;
}
if (name.startsWith("date0")) {
int p = name.substring("date0".length()).charAt(0) - '0';
tv.setText(days[p]);
} else {
int col = name.substring("date".length()).charAt(0) - '1';
int row = name.substring("date".length()).charAt(1) - '0';
int pos = row + col * 7;
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, month_offset);
cal.set(Calendar.DAY_OF_MONTH, 1);
int offset = cal.get(Calendar.DAY_OF_WEEK) - 1;
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
if (offset > pos || pos - offset >= lastDay) {
tv.setText("");
} else {
tv.setText("" + (pos - offset + 1));
}

}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}

private static int[] getLayouts( ) {
ArrayList<String> list = new ArrayList<>();
Field[] f = R.layout.class.getDeclaredFields();

int []ret = new int[f.length];
int count = 0;
for (int i = 0; i < f.length; i++) {
try {
String name = f[i].getName();
if (!name.contains("_") || name.equals("activity_main")) {
ret[count++] = f[i].getInt(null);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return Arrays.copyOf(ret,count);
}

/**
Expand Down Expand Up @@ -146,7 +242,9 @@ public void toggleSpans(View v) {
* @param v
*/
public void toggleOrientation(View v) {
mGrid.setOrientation(mOrientations[++mOrientationIndex % 2]);
mOrientation = mGrid.getOrientation() == Grid.HORIZONTAL
? Grid.VERTICAL : Grid.HORIZONTAL;
mGrid.setOrientation(mOrientation);
}

/**
Expand Down Expand Up @@ -180,7 +278,7 @@ public void restore(View v) {
mColumnWeightsIndex = 0;
mSkipsIndex = 0;
mSpansIndex = 0;
mOrientationIndex = 0;
mOrientation = Grid.VERTICAL;

mGrid = findViewById(R.id.grid);
mGrid.setRows(mSizes[mRowIndex]);
Expand All @@ -191,6 +289,6 @@ public void restore(View v) {
mGrid.setSpans(mSpans[mSpansIndex]);
mGrid.setVerticalGaps(mGaps[mVerticalGapsIndex]);
mGrid.setHorizontalGaps(mGaps[mHorizontalGapsIndex]);
mGrid.setOrientation(mOrientations[mOrientationIndex]);
mGrid.setOrientation(mOrientation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
app:grid_columns="3"
app:grid_rows="3"
app:grid_columnWeights="3,1,2"
app:grid_horizontalGaps="10"
app:grid_verticalGaps="10"
app:grid_horizontalGaps="10dp"
app:grid_verticalGaps="10dp"
app:grid_orientation="horizontal"
app:grid_skips="0:1x2,4:1x1,6:1x1" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
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"
app:grid_columns="4"
app:grid_rows="7"
app:grid_horizontalGaps="5"
app:grid_verticalGaps="5"
app:grid_horizontalGaps="5dp"
app:grid_verticalGaps="5dp"
app:grid_orientation="horizontal"
app:grid_spans="0:2x4,24:1x2" />

Expand Down
Loading