Skip to content

Commit

Permalink
Updated sample app for min-width and min-height #1
Browse files Browse the repository at this point in the history
  • Loading branch information
evant authored and thagikura committed May 31, 2016
1 parent c98bbcc commit b205160
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 1 deletion.
12 changes: 12 additions & 0 deletions app/src/main/java/com/google/android/apps/flexbox/FlexItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public class FlexItem implements Parcelable {

public float flexBasisPercent;

public int minWidth;

public int minHeight;

public FlexItem() {
}

Expand All @@ -90,6 +94,8 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeFloat(this.flexShrink);
dest.writeInt(this.alignSelf);
dest.writeFloat(this.flexBasisPercent);
dest.writeInt(this.minWidth);
dest.writeInt(this.minHeight);
}

protected FlexItem(Parcel in) {
Expand All @@ -109,6 +115,8 @@ protected FlexItem(Parcel in) {
this.flexShrink = in.readFloat();
this.alignSelf = in.readInt();
this.flexBasisPercent = in.readFloat();
this.minWidth = in.readInt();
this.minHeight = in.readInt();
}

public FlexboxLayout.LayoutParams toLayoutParams(Context context) {
Expand All @@ -124,6 +132,8 @@ public FlexboxLayout.LayoutParams toLayoutParams(Context context) {
MarginLayoutParamsCompat.setMarginStart(lp, startMargin);
MarginLayoutParamsCompat.setMarginEnd(lp, endMargin);
lp.bottomMargin = bottomMargin;
lp.minWidth = minWidth;
lp.minHeight = minHeight;
return lp;
}

Expand All @@ -146,6 +156,8 @@ public static FlexItem fromFlexView(View view, int index) {
flexItem.paddingStart = ViewCompat.getPaddingStart(view);
flexItem.paddingEnd = ViewCompat.getPaddingEnd(view);
flexItem.paddingBottom = view.getPaddingBottom();
flexItem.minWidth = lp.minWidth;
flexItem.minHeight = lp.minHeight;
return flexItem;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.android.apps.flexbox;

import com.google.android.apps.flexbox.validators.FixedDimensionInputValidator;
import com.google.android.apps.flexbox.validators.FlexBasisPercentInputValidator;
import com.google.android.apps.flexbox.validators.InputValidator;
import com.google.android.apps.flexbox.validators.IntegerInputValidator;
Expand Down Expand Up @@ -156,6 +157,23 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
new FlexEditTextWatcher(heightInput, new DimensionInputValidator(),
R.string.must_be_minus_one_or_minus_two_or_non_negative_integer));

final TextInputLayout minWidthInput = (TextInputLayout) view
.findViewById(R.id.input_layout_min_width);
EditText minWidthEdit = (EditText) view.findViewById(R.id.edit_text_min_width);
minWidthEdit.setText(String.valueOf(mFlexItem.minWidth));
minWidthEdit.addTextChangedListener(
new FlexEditTextWatcher(minWidthInput, new FixedDimensionInputValidator(),
R.string.must_be_non_negative_integer));

final TextInputLayout minHeightInput = (TextInputLayout) view
.findViewById(R.id.input_layout_min_height);
EditText minHeightEdit = (EditText) view.findViewById(
R.id.edit_text_min_height);
minHeightEdit.setText(String.valueOf(mFlexItem.minHeight));
minHeightEdit.addTextChangedListener(
new FlexEditTextWatcher(minHeightInput, new FixedDimensionInputValidator(),
R.string.must_be_non_negative_integer));

Spinner alignSelfSpinner = (Spinner) view.findViewById(
R.id.spinner_align_self);
ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(getActivity(),
Expand Down Expand Up @@ -305,6 +323,12 @@ public void afterTextChanged(Editable editable) {
= FlexboxLayout.LayoutParams.FLEX_BASIS_PERCENT_DEFAULT;
}
break;
case R.id.input_layout_min_width:
mFlexItem.minWidth = intValue;
break;
case R.id.input_layout_min_height:
mFlexItem.minHeight = intValue;
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import android.text.TextUtils;

/**
* Validator for dimension values.
* Validator for dimension values including match_parent and wrap_content.
*/
public class DimensionInputValidator implements InputValidator {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.apps.flexbox.validators;

import android.text.TextUtils;

/**
* Validator for dimension values.
*/
public class FixedDimensionInputValidator implements InputValidator {

@Override
public boolean isValidInput(CharSequence charSequence) {
return !TextUtils.isEmpty(charSequence) && TextUtils.isDigitsOnly(charSequence);
}
}
30 changes: 30 additions & 0 deletions app/src/main/res/layout/fragment_flex_item_edit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,36 @@ limitations under the License.
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_min_width"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/edit_text_min_width"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="1"
android:singleLine="true"
android:inputType="number"
android:hint="@string/hint_min_width" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_min_height"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/edit_text_min_height"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="1"
android:singleLine="true"
android:inputType="number"
android:hint="@string/hint_min_height" />
</android.support.design.widget.TextInputLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ limitations under the License.
<string name="hint_align_self">Align Self</string>
<string name="hint_width">Width (-1: match_parent, -2: wrap_content)</string>
<string name="hint_height">Height (-1: match_parent, -2: wrap_content)</string>
<string name="hint_min_width">Min Width</string>
<string name="hint_min_height">Min Height</string>

<string name="must_be_non_negative_float">Must be a non-negative float value</string>
<string name="must_be_non_negative_integer">Must a non-negative integer value</string>
<string name="must_be_minus_one_or_minus_two_or_non_negative_integer">Must be -1 or -2 or a non-negative integer value</string>
<string name="must_be_integer">Must be an integer value</string>
<string name="must_be_minus_one_or_non_negative_integer">Must be -1 or a non-negative integer value</string>
Expand Down

0 comments on commit b205160

Please sign in to comment.