Repository: google/flexbox-layout Branch: main Commit: 366b461fd042 Files: 181 Total size: 1.1 MB Directory structure: gitextract_48clvfh9/ ├── .circleci/ │ └── config.yml ├── .github/ │ ├── issue_template.md │ └── workflows/ │ └── gradle-wrapper-validation.yml ├── .gitignore ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── build.gradle ├── demo-cat-gallery/ │ ├── build.gradle │ ├── proguard-rules.pro │ └── src/ │ └── main/ │ ├── AndroidManifest.xml │ ├── java/ │ │ └── com/ │ │ └── google/ │ │ └── android/ │ │ └── flexbox/ │ │ └── apps/ │ │ └── catgallery/ │ │ ├── CatAdapter.kt │ │ ├── CatViewHolder.kt │ │ └── MainActivity.kt │ └── res/ │ ├── layout/ │ │ ├── activity_main.xml │ │ ├── content_main.xml │ │ └── viewholder_cat.xml │ ├── values/ │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── values-v21/ │ └── styles.xml ├── demo-playground/ │ ├── build.gradle │ ├── proguard-rules.pro │ └── src/ │ ├── androidTest/ │ │ └── java/ │ │ └── com/ │ │ └── google/ │ │ └── android/ │ │ └── apps/ │ │ └── flexbox/ │ │ └── test/ │ │ └── MainActivityTest.kt │ └── main/ │ ├── AndroidManifest.xml │ ├── java/ │ │ └── com/ │ │ └── google/ │ │ └── android/ │ │ └── flexbox/ │ │ ├── Extensions.kt │ │ ├── FlexItemAdapter.kt │ │ ├── FlexItemChangedListener.kt │ │ ├── FlexItemChangedListenerImpl.kt │ │ ├── FlexItemChangedListenerImplRecyclerView.kt │ │ ├── FlexItemClickListener.kt │ │ ├── FlexItemEditFragment.kt │ │ ├── FlexItemViewHolder.kt │ │ ├── FlexboxLayoutFragment.kt │ │ ├── FragmentHelper.kt │ │ ├── MainActivity.kt │ │ ├── RecyclerViewFragment.kt │ │ ├── SettingsActivity.kt │ │ └── validators/ │ │ ├── DimensionInputValidator.kt │ │ ├── FixedDimensionInputValidator.kt │ │ ├── FlexBasisPercentInputValidator.kt │ │ ├── InputValidator.kt │ │ ├── IntegerInputValidator.kt │ │ └── NonNegativeDecimalInputValidator.kt │ └── res/ │ ├── drawable/ │ │ ├── flex_item_background.xml │ │ └── side_nav_bar.xml │ ├── layout/ │ │ ├── activity_main.xml │ │ ├── app_bar_main.xml │ │ ├── content_main.xml │ │ ├── fragment_flex_item_edit.xml │ │ ├── fragment_flexboxlayout.xml │ │ ├── fragment_recyclerview.xml │ │ ├── nav_header_main.xml │ │ ├── spinner_align_content.xml │ │ ├── spinner_align_items.xml │ │ ├── spinner_flex_direction.xml │ │ ├── spinner_flex_wrap.xml │ │ ├── spinner_item.xml │ │ ├── spinner_justify_content.xml │ │ └── viewholder_flex_item.xml │ ├── menu/ │ │ ├── activity_main_drawer.xml │ │ └── menu_main.xml │ ├── values/ │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ ├── values-v14/ │ │ └── styles.xml │ ├── values-v21/ │ │ └── styles.xml │ ├── values-w720dp/ │ │ └── dimens.xml │ └── xml/ │ └── new_flex_item_preferences.xml ├── flexbox/ │ ├── .gitignore │ ├── build.gradle │ ├── constants.gradle │ ├── maven-puglisher-plugin.gradle │ ├── proguard-rules.txt │ └── src/ │ ├── androidTest/ │ │ ├── AndroidManifest.xml │ │ ├── java/ │ │ │ └── com/ │ │ │ └── google/ │ │ │ └── android/ │ │ │ └── flexbox/ │ │ │ ├── FakeFlexContainer.kt │ │ │ ├── FlexboxHelperTest.kt │ │ │ └── test/ │ │ │ ├── ConfigChangeActivity.kt │ │ │ ├── FlexboxAndroidTest.kt │ │ │ ├── FlexboxLayoutManagerConfigChangeTest.kt │ │ │ ├── FlexboxLayoutManagerTest.kt │ │ │ ├── FlexboxTestActivity.kt │ │ │ ├── IsEqualAllowingError.kt │ │ │ ├── NestedInnerAdapter.kt │ │ │ ├── NestedOuterAdapter.kt │ │ │ ├── TestAdapter.kt │ │ │ ├── TestAdapterMultiViewTypes.kt │ │ │ ├── TestUtil.kt │ │ │ └── TestViewHolder.kt │ │ └── res/ │ │ ├── drawable/ │ │ │ ├── divider.xml │ │ │ ├── divider_thick.xml │ │ │ └── flex_item_background.xml │ │ ├── layout/ │ │ │ ├── activity_align_content_test.xml │ │ │ ├── activity_align_content_test_overflowed.xml │ │ │ ├── activity_align_items_baseline_test.xml │ │ │ ├── activity_align_items_baseline_wrap_content.xml │ │ │ ├── activity_align_items_parent_padding_test.xml │ │ │ ├── activity_align_items_test.xml │ │ │ ├── activity_align_self_stretch_test.xml │ │ │ ├── activity_child_needs_remeasure_column.xml │ │ │ ├── activity_child_needs_remeasure_row.xml │ │ │ ├── activity_direction_column_align_items_center_margin_oneside.xml │ │ │ ├── activity_direction_row_align_items_center_margin_oneside.xml │ │ │ ├── activity_divider_test_direction_column.xml │ │ │ ├── activity_divider_test_direction_row.xml │ │ │ ├── activity_empty_children.xml │ │ │ ├── activity_first_item_large_horizontal_test.xml │ │ │ ├── activity_first_item_large_vertical_test.xml │ │ │ ├── activity_first_view_gone_first_line_single_item.xml │ │ │ ├── activity_first_view_gone_layout_grow_set_for_rest.xml │ │ │ ├── activity_first_view_gone_layout_shrink_set_for_rest.xml │ │ │ ├── activity_flex_basis_percent_test.xml │ │ │ ├── activity_flex_grow_test.xml │ │ │ ├── activity_flex_item_match_parent.xml │ │ │ ├── activity_flex_item_match_parent_direction_column.xml │ │ │ ├── activity_flex_wrap_test.xml │ │ │ ├── activity_flexbox_wrap_content.xml │ │ │ ├── activity_flexbox_wrapped_with_horizontalscrollview.xml │ │ │ ├── activity_flexbox_wrapped_with_scrollview.xml │ │ │ ├── activity_justify_content_test.xml │ │ │ ├── activity_justify_content_with_gone.xml │ │ │ ├── activity_justify_content_with_parent_padding.xml │ │ │ ├── activity_maxheight_test.xml │ │ │ ├── activity_maxheight_upper_bound_test.xml │ │ │ ├── activity_maxwidth_test.xml │ │ │ ├── activity_maxwidth_upper_bound_test.xml │ │ │ ├── activity_minheight_lower_bound_test.xml │ │ │ ├── activity_minheight_test.xml │ │ │ ├── activity_minwidth_lower_bound_test.xml │ │ │ ├── activity_minwidth_test.xml │ │ │ ├── activity_order_test.xml │ │ │ ├── activity_simple.xml │ │ │ ├── activity_stretch_test.xml │ │ │ ├── activity_views_visibility_gone.xml │ │ │ ├── activity_views_visibility_invisible.xml │ │ │ ├── activity_visibility_gone_first_item_in_flex_line_column.xml │ │ │ ├── activity_visibility_gone_first_item_in_flex_line_row.xml │ │ │ ├── activity_wrap_before_test.xml │ │ │ ├── activity_wrap_child_margin_horizontal_test.xml │ │ │ ├── activity_wrap_child_margin_vertical_test.xml │ │ │ ├── activity_wrap_content_child_bottom_margin_column_grow.xml │ │ │ ├── activity_wrap_content_child_bottom_margin_column_shrink.xml │ │ │ ├── activity_wrap_content_child_bottom_margin_row_grow.xml │ │ │ ├── activity_wrap_content_child_bottom_margin_row_shrink.xml │ │ │ ├── activity_wrap_parent_padding_horizontal_test.xml │ │ │ ├── activity_wrap_parent_padding_vertical_test.xml │ │ │ ├── activity_zero_height_positive_flexgrow.xml │ │ │ ├── activity_zero_width_positive_flexgrow.xml │ │ │ ├── recyclerview.xml │ │ │ ├── recyclerview_reverse.xml │ │ │ ├── recyclerview_viewholder.xml │ │ │ ├── viewholder_inner_recyclerview.xml │ │ │ ├── viewholder_inner_recyclerview_wrap_horizontally.xml │ │ │ ├── viewholder_textview.xml │ │ │ ├── wrapped_recyclerview.xml │ │ │ └── wrapped_recyclerview_scroll_vertical.xml │ │ └── values/ │ │ └── strings.xml │ └── main/ │ ├── AndroidManifest.xml │ ├── java/ │ │ └── com/ │ │ └── google/ │ │ └── android/ │ │ └── flexbox/ │ │ ├── AlignContent.java │ │ ├── AlignItems.java │ │ ├── AlignSelf.java │ │ ├── FlexContainer.java │ │ ├── FlexDirection.java │ │ ├── FlexItem.java │ │ ├── FlexLine.java │ │ ├── FlexWrap.java │ │ ├── FlexboxHelper.java │ │ ├── FlexboxItemDecoration.java │ │ ├── FlexboxLayout.java │ │ ├── FlexboxLayoutManager.java │ │ └── JustifyContent.java │ └── res/ │ └── values/ │ └── attrs.xml ├── gradle/ │ └── wrapper/ │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradle.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── tool/ └── codeStyleSettings.xml ================================================ FILE CONTENTS ================================================ ================================================ FILE: .circleci/config.yml ================================================ version: 2 jobs: build: working_directory: ~/code docker: - image: circleci/android:api-29 environment: JVM_OPTS: -Xmx3200m MAX_RETRY: 4 steps: - checkout - restore_cache: key: jars-{{ checksum "build.gradle" }}-{{ checksum "flexbox/build.gradle" }} - run: name: Download Dependencies command: ./gradlew androidDependencies - run: name: Set up gcloud service key command: | if [ -n "$GCLOUD_SERVICE_KEY" ]; then echo ${GCLOUD_SERVICE_KEY} | base64 --decode > ${HOME}/client-secret.json ; gcloud config set project ${GCLOUD_PROJECT} ; gcloud auth activate-service-account ${GCLOUD_SERVICE_ACCOUNT} --key-file ${HOME}/client-secret.json ; fi - save_cache: paths: - ~/.gradle key: jars-{{ checksum "build.gradle" }}-{{ checksum "flexbox/build.gradle" }} - run: name: Build apks command: ./gradlew build assembleAndroidTest - run: name: Run Firebase Test Lab command: | if [ -n "$GCLOUD_SERVICE_KEY" ]; then set +e ; counter=0 ; result=1 ; while [ $result != 0 -a $counter -lt $MAX_RETRY ]; do gcloud firebase test android run \ --type instrumentation \ --app demo-playground/build/outputs/apk/debug/demo-playground-debug.apk \ --test flexbox/build/outputs/apk/androidTest/debug/flexbox-debug-androidTest.apk \ --device-ids hammerhead,sailfish \ --os-version-ids 19,21,23,24,25,26 \ --locales en --orientations portrait,landscape \ --results-bucket android-devrel-ci-flexbox \ --timeout 180s ; result=$? ; let counter=counter+1 ; done exit $result ; fi ================================================ FILE: .github/issue_template.md ================================================ - [ ] I have searched [existing issues](https://github.com/google/flexbox-layout/issues) and confirmed this is not a duplicate ## Issues and steps to reproduce *Please replace this with steps to reproduce your issue.* ## Expected behavior *Please describe what you expected would happen.* ## Version of the flexbox library *e.g. 0.2.6, 0.3.0-alpha3* ## Link to code *Please link to the code we can use to reproduce this issue.* *A complete project we can build/run is preferred, if you can't provide one, please show* *us relevant code* ================================================ FILE: .github/workflows/gradle-wrapper-validation.yml ================================================ name: "Validate Gradle Wrapper" on: [push, pull_request] jobs: validation: name: "Validation" runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: gradle/wrapper-validation-action@v1 ================================================ FILE: .gitignore ================================================ *.iml .gradle .idea/ /local.properties /.idea/workspace.xml /.idea/libraries !.idea/codeStyleSettings.xml .DS_Store /build /captures .vscode/ # Taken from Android.gitignore https://github.com/github/gitignore/blob/master/Android.gitignore # # Built application files *.apk *.ap_ # Files for the Dalvik VM *.dex # Java class files *.class # Generated files bin/ gen/ out/ # Gradle files .gradle/ build/ # Local configuration file (sdk path, etc) local.properties # Proguard folder generated by Eclipse proguard/ # Log Files *.log # Android Studio Navigation editor temp files .navigation/ # Android Studio captures folder captures/ # Intellij *.iml ================================================ FILE: CONTRIBUTING.md ================================================ # How to become a contributor and submit your own code ## Contributor License Agreements We'd love to accept your sample apps and patches! Before we can take them, we have to jump a couple of legal hurdles. Please fill out either the individual or corporate Contributor License Agreement (CLA). * If you are an individual writing original source code and you're sure you own the intellectual property, then you'll need to sign an [individual CLA] (https://cla.developers.google.com). * If you work for a company that wants to allow you to contribute your work, then you'll need to sign a [corporate CLA] (https://cla.developers.google.com). Follow either of the two links above to access the appropriate CLA and instructions for how to sign and return it. Once we receive it, we'll be able to accept your pull requests. ## Contributing A Patch 1. Submit an issue describing your proposed change to the repo in question. 1. The repo owner will respond to your issue promptly. 1. If your proposed change is accepted, and you haven't already done so, sign a Contributor License Agreement (see details above). 1. Fork the desired repo, develop and test your code changes. 1. Ensure that your code adheres to the existing style in the sample to which you are contributing. Refer to the [Android Code Style Guide] (https://source.android.com/source/code-style.html) for the recommended coding standards for this organization. 1. Ensure that your code has an appropriate set of unit tests which all pass. 1. Submit a pull request. ## Code Style This repository follows the official Android code style. When you send a patch, please try to follow that. Here are the example steps to follow: 1. From Android Studio or IntelliJ IDEA, navigate to "Preferences" -> "Editor" -> "Code Style" 1. Select "Import Scheme" by clicking the gear icon next to the Scheme pull down 1. Choose /tool/codeStyleSettings.xml 1. Create a new scheme by typing a scheme name in the "To" edit box or apply to the current scheme. ================================================ FILE: LICENSE ================================================ Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2018 Google LLC 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. ================================================ FILE: README.md ================================================ # FlexboxLayout [ ![Circle CI](https://circleci.com/gh/google/flexbox-layout.svg?style=shield&circle-token=2a42716dfffab73d73c5ce7ed7b3ee620cfa137b) ](https://circleci.com/gh/google/flexbox-layout/tree/main) FlexboxLayout is a library project which brings the similar capabilities of [CSS Flexible Box Layout Module](https://www.w3.org/TR/css-flexbox-1) to Android. # Installation Add the following dependency to your `build.gradle` file: ``` dependencies { implementation 'com.google.android.flexbox:flexbox:3.0.0' } ``` **Starting from 3.0.0, the groupId is changed to `com.google.android.flexbox` in preparation to uploading the artifacts to google maven. You can still download the artifacts from jcenter for the past versions with the prior groupId (`com.google.android`), but migrating the library 3.0.0 is recommended.** Note that the default values for `alignItems` and `alignContent` for `FlexboxLayout` have been changed from `stretch` to `flex_start` starting from 2.0.0, it may break the existing apps. Please make sure to set `stretch` explicitly if you want to apply the behavior of `stretch`. Note that starting from 1.1.0, the library is expeced to use with AndroidX. Please migrate to [AndroidX](https://developer.android.com/jetpack/androidx/migrate) if you use 1.1.0 or above. Please use 1.0.0 if you haven't migrated to AndroidX. # Usage There are two ways of using Flexbox in your layout. ## FlexboxLayout The first one is `FlexboxLayout` that extends the `ViewGroup` like `LinearLayout` and `RelativeLayout`. You can specify the attributes from a layout XML like: ```xml ``` Or from code like: ```java FlexboxLayout flexboxLayout = (FlexboxLayout) findViewById(R.id.flexbox_layout); flexboxLayout.setFlexDirection(FlexDirection.ROW); View view = flexboxLayout.getChildAt(0); FlexboxLayout.LayoutParams lp = (FlexboxLayout.LayoutParams) view.getLayoutParams(); lp.setOrder(-1); lp.setFlexGrow(2); view.setLayoutParams(lp); ``` ## FlexboxLayoutManager (within RecyclerView) The second one is `FlexboxLayoutManager` that can be used within `RecyclerView`. ```java RecyclerView recyclerView = (RecyclerView) context.findViewById(R.id.recyclerview); FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(context); layoutManager.setFlexDirection(FlexDirection.COLUMN); layoutManager.setJustifyContent(JustifyContent.FLEX_END); recyclerView.setLayoutManager(layoutManager); ``` or for the attributes for the children of the `FlexboxLayoutManager` you can do like: ```java mImageView.setImageDrawable(drawable); ViewGroup.LayoutParams lp = mImageView.getLayoutParams(); if (lp instanceof FlexboxLayoutManager.LayoutParams) { FlexboxLayoutManager.LayoutParams flexboxLp = (FlexboxLayoutManager.LayoutParams) lp; flexboxLp.setFlexGrow(1.0f); flexboxLp.setAlignSelf(AlignSelf.FLEX_END); } ``` The advantage of using `FlexboxLayoutManager` is that it recycles the views that go off the screen for reuse for the views that are appearing as the user scrolls instead of inflating every individual view, which consumes much less memory especially when the number of items contained in the Flexbox container is large. ![FlexboxLayoutManager in action](/assets/flexbox-layoutmanager.gif) ## Supported attributes/features comparison Due to some characteristics of `RecyclerView`, some Flexbox attributes are not available/not implemented to the `FlexboxLayoutManager`. Here is a quick overview of the attributes/features comparison between the two implementations. |Attribute / Feature|FlexboxLayout| FlexboxLayoutManager (RecyclerView)| | ------- |:-----------:|:----------------------------------:| |flexDirection|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)| |flexWrap|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png) (except `wrap_reverse`)| |justifyContent|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)| |alignItems|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)| |alignContent|![Check](/assets/pngs/check_green_small.png)| - | |layout_order|![Check](/assets/pngs/check_green_small.png)| - | |layout_flexGrow|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)| |layout_flexShrink|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)| |layout_alignSelf|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)| |layout_flexBasisPercent|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)| |layout_(min/max)Width|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)| |layout_(min/max)Height|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)| |layout_wrapBefore|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)| |Divider|![Check](/assets/pngs/check_green_small.png)|![Check](/assets/pngs/check_green_small.png)| |View recycling| - |![Check](/assets/pngs/check_green_small.png)| |Scrolling| *1 |![Check](/assets/pngs/check_green_small.png)| *1 Partially possible by wrapping it with `ScrollView`. But it isn't likely to work with a large set of views inside the layout. Because it doesn't consider view recycling. # Supported attributes ## Attributes for the FlexboxLayout: * __flexDirection__ * This attribute determines the direction of the main axis (and the cross axis, perpendicular to the main axis). The direction children items are placed inside the Flexbox layout. Possible values are: * row (default) * row_reverse * column * column_reverse ![Flex Direction explanation](/assets/flex-direction.gif) * __flexWrap__ * This attribute controls whether the flex container is single-line or multi-line, and the direction of the cross axis. Possible values are: * nowrap (default for FlexboxLayout) * wrap (default for FlexboxLayoutManager) * wrap_reverse (not supported by FlexboxLayoutManager) ![Flex Wrap explanation](/assets/flex-wrap.gif) * __justifyContent__ * This attribute controls the alignment along the main axis. Possible values are: * flex_start (default) * flex_end * center * space_between * space_around * space_evenly ![Justify Content explanation](/assets/justify-content.gif) * __alignItems__ * This attribute controls the alignment along the cross axis. Possible values are: * flex_start (default for FlexboxLayout) * flex_end * center * baseline * stretch (default for FlexboxLayoutManager) ![Align Items explanation](/assets/align-items.gif) * __alignContent__ * This attribute controls the alignment of the flex lines in the flex container. Possible values are: * flex_start (default) * flex_end * center * space_between * space_around * stretch ![Align Content explanation](/assets/align-content.gif) * __showDividerHorizontal__ (one or more of `none | beginning | middle | end`) * __dividerDrawableHorizontal__ (reference to a drawable) * Puts horizontal dividers between flex lines (or flex items when flexDirection is set to `column` or `column_rebase`). * __showDividerVertical__ (one or more of `none | beginning | middle | end`) * __dividerDrawableVertical__ (reference to a drawable) * Puts vertical dividers between flex items (or flex lines when flexDirection is set to `column` or `column_rebase`). * __showDivider__ (one or more of `none | beginning | middle | end`) * __dividerDrawable__ (reference to a drawable) * Shorthand for setting both horizontal and vertical dividers. Note that if used with other attributes (such as `justifyContent="space_around"` or `alignContent="space_between"` ... etc) for putting spaces between flex lines or flex items, you may see unexpected spaces. Please avoid using these at the same time. Example of putting both vertical and horizontal dividers. `res/drawable/divider.xml` ```xml ``` `res/layout/content_main.xml` ```xml ``` ![Dividers beginning and middle](/assets/divider-beginning-middle.png) ## Attributes for the children of a FlexboxLayout * __layout_order__ (integer) * This attribute can change how the ordering of the children views are laid out. By default, children are displayed and laid out in the same order as they appear in the layout XML. If not specified, `1` is set as a default value. ![Order explanation](/assets/layout_order.gif) * __layout_flexGrow__ (float) * This attribute determines how much this child will grow if positive free space is distributed relative to the rest of other flex items included in the same flex line. If a flex item has a positive `layout_flexGrow` value, the item will take up the remaining space in the flex line. If multiple flex items in the same flex line have positive `layout_flexGrow` values, the remaining free space is distributed depending on the proportion of their declared `layout_flexGrow` value. (Similar to the `layout_weight` attribute in the `LinearLayout`) If not specified, `0` is set as a default value. ![Flex Grow explanation](/assets/layout_flexGrow.gif) * __layout_flexShrink__ (float) * This attribute determines how much this child will shrink if negative free space is distributed relative to the rest of other flex items included in the same flex line. If not specified, `1` is set as a default value. ![Flex Shrink explanation](/assets/layout_flexShrink.gif) * __layout_alignSelf__ * This attribute determines the alignment along the cross axis (perpendicular to the main axis). The alignment in the same direction can be determined by the `alignItems` in the parent, but if this is set to other than `auto`, the cross axis alignment is overridden for this child. Possible values are: * auto (default) * flex_start * flex_end * center * baseline * stretch ![Align Self explanation](/assets/layout_alignSelf.gif) * __layout_flexBasisPercent__ (fraction) * The initial flex item length in a fraction format relative to its parent. The initial main size of this child view is trying to be expanded as the specified fraction against the parent main size. If this value is set, the length specified from `layout_width` (or `layout_height`) is overridden by the calculated value from this attribute. This attribute is only effective when the parent's length is definite (MeasureSpec mode is `MeasureSpec.EXACTLY`). The default value is `-1`, which means not set. ![Flex basis percent explanation](/assets/layout_flexBasisPercent.gif) * __layout_minWidth__ / __layout_minHeight__ (dimension) * These attributes impose minimum size constraints for the children of FlexboxLayout. A child view won't shrink less than the value of these attributes (varies based on the `flexDirection` attribute as to which attribute imposes the size constraint along the main axis) regardless of the `layout_flexShrink` attribute. ![Min width explanation](/assets/layout_minWidth.gif) * __layout_maxWidth__ / __layout_maxHeight__ (dimension) * These attributes impose maximum size constraints for the children of FlexboxLayout. A child view won't be expanded more than the value of these attributes (varies based on the `flexDirection` attribute as to which attribute imposes the size constraint along the main axis) regardless of the `layout_flexGrow` attribute. ![Max width explanation](/assets/layout_maxWidth.gif) * __layout_wrapBefore__ (boolean) * This attribute forces a flex line wrapping, the default value is `false`. i.e. if this is set to `true` for a flex item, the item will become the first item of a flex line. (A wrapping happens regardless of the flex items being processed in the previous flex line) This attribute is ignored if the `flex_wrap` attribute is set to `nowrap`. The equivalent attribute isn't defined in the original CSS Flexible Box Module specification, but having this attribute is useful for Android developers. For example, to flatten the layouts when building a grid-like layout or for a situation where developers want to put a new flex line to make a semantic difference from the previous one, etc. ![Wrap before explanation](/assets/layout_wrapBefore.gif) # Others ## Known differences from the original CSS specification This library tries to achieve the same capabilities of the original [Flexible Box specification](https://www.w3.org/TR/css-flexbox-1) as much as possible, but due to some reasons such as the way specifying attributes can't be the same between CSS and Android XML, there are some known differences from the original specification. (1) There is no [flex-flow](https://www.w3.org/TR/css-flexbox-1/#flex-flow-property) equivalent attribute * Because `flex-flow` is a shorthand for setting the `flex-direction` and `flex-wrap` properties, specifying two attributes from a single attribute is not practical in Android. (2) There is no [flex](https://www.w3.org/TR/css-flexbox-1/#flex-property) equivalent attribute * Likewise `flex` is a shorthand for setting the `flex-grow`, `flex-shrink` and `flex-basis`, specifying those attributes from a single attribute is not practical. (3) `layout_flexBasisPercent` is introduced instead of [flexBasis](https://www.w3.org/TR/css-flexbox-1/#flex-basis-property) * Both `layout_flexBasisPercent` in this library and `flex-basis` property in the CSS are used to determine the initial length of an individual flex item. The `flex-basis` property accepts width values such as `1em`, `10px`, and `content` as strings as well as percentage values such as `10%` and `30%`. `layout_flexBasisPercent` only accepts percentage values. However, specifying initial fixed width values can be done by specifying width (or height) values in layout_width (or layout_height, varies depending on the `flexDirection`). Also, the same effect can be done by specifying "wrap_content" in layout_width (or layout_height) if developers want to achieve the same effect as 'content'. Thus, `layout_flexBasisPercent` only accepts percentage values, which can't be done through layout_width (or layout_height) for simplicity. (4) `layout_wrapBefore` is introduced. * The equivalent attribute doesn't exist in the CSS Flexible Box Module specification, but as explained above, Android developers will benefit by having this attribute for having more control over when a wrapping happens. (5) Default values for `alignItems` and `alignContent` are set to `flex_start` instead of `stretch`. * Setting `stretch` for the `alignItems` is expensive because the children of `FlexboxLayout` are measured more than twice. The difference is more obvious when the layout hierarchy is deeply nested. ## Xamarin Binding Xamarin binding is now available on [NuGet](https://www.nuget.org/packages/FlexboxLayoutXamarinBindingAndroid/) thanks to [@btripp](https://github.com/btripp) ## Demo apps ### Flexbox Playground demo app The `demo-playground` module works as a playground demo app for trying various values for the supported attributes. You can install it by ``` ./gradlew demo-playground:installDebug ``` ### Cat gallery demo app The `demo-cat-gallery` module showcases the usage of the FlexboxLayoutManager inside the RecyclerView that handles various sizes of views aligned nicely regardless of the device width like the Google Photo app without loading all the images on the memory. Thus compared to using the {@link FlexboxLayout}, it's much less likely to abuse the memory, which sometimes leads to the OutOfMemoryError. ``` ./gradlew demo-cat-gallery:installDebug ``` ## How to make contributions Please read and follow the steps in [CONTRIBUTING.md](/CONTRIBUTING.md) ## License Please see [LICENSE](/LICENSE) ================================================ FILE: build.gradle ================================================ /* * 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. */ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext { minSdkVersion = 14 targetSdkVersion = 30 compileSdkVersion = 30 androidGradlePluginVersion = "4.2.0" androidxAnnotationVersion = "1.2.0" androidxAppCompatVersion = "1.2.0" androidxCoreVersion = "1.3.2" androidxPreferenceVersion = "1.1.1" androidxRecyclerViewVersion = "1.2.0" androidxEspressoVersion = "3.3.0" androidxTestExtVersion = "1.1.2" androidxTestVersion = "1.3.0" junitVersion = "4.13.2" kotlinVersion = "1.4.32" materialVersion = "1.3.0" } repositories { jcenter() google() } dependencies { classpath "com.android.tools.build:gradle:$androidGradlePluginVersion" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() google() } } task clean(type: Delete) { delete rootProject.buildDir } // This allows to disable pre dexing. If not disabled, it makes each CI build slow // See https://circleci.com/docs/android/#disable-pre-dexing-to-improve-build-performance // http://tools.android.com/tech-docs/new-build-system/tips#TOC-Improving-Build-Server-performance project.ext.preDexLibs = !project.hasProperty('disablePreDex') subprojects { project.plugins.whenPluginAdded { plugin -> if ("com.android.build.gradle.AppPlugin" == plugin.class.name) { project.android.dexOptions.preDexLibraries = rootProject.ext.preDexLibs } else if ("com.android.build.gradle.LibraryPlugin" == plugin.class.name) { project.android.dexOptions.preDexLibraries = rootProject.ext.preDexLibs } } } ================================================ FILE: demo-cat-gallery/build.gradle ================================================ /* * Copyright 2017 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. */ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' android { compileSdkVersion rootProject.ext.compileSdkVersion defaultConfig { applicationId "com.google.android.flexbox.apps.catgallery" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { shrinkResources true minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation project(path: ":flexbox") implementation "androidx.appcompat:appcompat:${rootProject.androidxAppCompatVersion}" implementation "androidx.recyclerview:recyclerview:${rootProject.androidxRecyclerViewVersion}" implementation "com.google.android.material:material:${rootProject.materialVersion}" implementation "org.jetbrains.kotlin:kotlin-stdlib:${rootProject.kotlinVersion}" } ================================================ FILE: demo-cat-gallery/proguard-rules.pro ================================================ # Add project specific ProGuard rules here. # By default, the flags in this file are appended to flags specified # in /usr/local/google/home/thagikura/android-sdk/tools/proguard/proguard-android.txt # You can edit the include path and order by changing the proguardFiles # directive in build.gradle. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html # Add any project specific keep options here: # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} ================================================ FILE: demo-cat-gallery/src/main/AndroidManifest.xml ================================================ ================================================ FILE: demo-cat-gallery/src/main/java/com/google/android/flexbox/apps/catgallery/CatAdapter.kt ================================================ /* * Copyright 2017 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.flexbox.apps.catgallery import android.view.LayoutInflater import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView /** * Adapter class that handles the data set with the {@link RecyclerView.LayoutManager} */ internal class CatAdapter : RecyclerView.Adapter() { companion object { private val CAT_IMAGE_IDS = intArrayOf( R.drawable.cat_1, R.drawable.cat_2, R.drawable.cat_3, R.drawable.cat_4, R.drawable.cat_5, R.drawable.cat_6, R.drawable.cat_7, R.drawable.cat_8, R.drawable.cat_9, R.drawable.cat_10, R.drawable.cat_11, R.drawable.cat_12, R.drawable.cat_13, R.drawable.cat_14, R.drawable.cat_15, R.drawable.cat_16, R.drawable.cat_17, R.drawable.cat_18, R.drawable.cat_19 ) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CatViewHolder { val view = LayoutInflater.from(parent.context) .inflate(R.layout.viewholder_cat, parent, false) return CatViewHolder(view) } override fun onBindViewHolder(holder: CatViewHolder, position: Int) { val pos = position % CAT_IMAGE_IDS.size holder.bindTo(CAT_IMAGE_IDS[pos]) } override fun getItemCount() = CAT_IMAGE_IDS.size * 4 } ================================================ FILE: demo-cat-gallery/src/main/java/com/google/android/flexbox/apps/catgallery/CatViewHolder.kt ================================================ /* * Copyright 2017 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.flexbox.apps.catgallery import android.view.View import android.widget.ImageView import androidx.annotation.DrawableRes import androidx.recyclerview.widget.RecyclerView import com.google.android.flexbox.FlexboxLayoutManager /** * ViewHolder that represents a cat image. */ internal class CatViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { private val imageView: ImageView = itemView.findViewById(R.id.imageview) internal fun bindTo(@DrawableRes drawableRes: Int) { imageView.setImageResource(drawableRes) val lp = imageView.layoutParams if (lp is FlexboxLayoutManager.LayoutParams) { lp.flexGrow = 1f } } } ================================================ FILE: demo-cat-gallery/src/main/java/com/google/android/flexbox/apps/catgallery/MainActivity.kt ================================================ /* * Copyright 2017 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.flexbox.apps.catgallery import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar import androidx.recyclerview.widget.RecyclerView import com.google.android.flexbox.AlignItems import com.google.android.flexbox.FlexDirection import com.google.android.flexbox.FlexWrap import com.google.android.flexbox.FlexboxLayoutManager /** * Launcher Activity for the cat gallery demo app that demonstrates the usage of the * {@link FlexboxLayoutManager} that handles various sizes of views aligned nicely regardless of * the device width like the Google Photo app without loading all the images on the memory. * Thus compared to using the {@link FlexboxLayout}, it's much less likely to abuse the memory, * which some times leads to the OutOfMemoryError. */ class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val toolbar: Toolbar = findViewById(R.id.toolbar) setSupportActionBar(toolbar) val flexboxLayoutManager = FlexboxLayoutManager(this).apply { flexWrap = FlexWrap.WRAP flexDirection = FlexDirection.ROW alignItems = AlignItems.STRETCH } val recyclerView: RecyclerView = findViewById(R.id.recyclerview) recyclerView.apply { layoutManager = flexboxLayoutManager adapter = CatAdapter() } } } ================================================ FILE: demo-cat-gallery/src/main/res/layout/activity_main.xml ================================================ ================================================ FILE: demo-cat-gallery/src/main/res/layout/content_main.xml ================================================ ================================================ FILE: demo-cat-gallery/src/main/res/layout/viewholder_cat.xml ================================================ ================================================ FILE: demo-cat-gallery/src/main/res/values/colors.xml ================================================ #3F51B5 #303F9F #FF4081 ================================================ FILE: demo-cat-gallery/src/main/res/values/dimens.xml ================================================ 180dp ================================================ FILE: demo-cat-gallery/src/main/res/values/strings.xml ================================================ Cat Gallery Cat image ================================================ FILE: demo-cat-gallery/src/main/res/values/styles.xml ================================================ ================================================ FILE: demo-playground/build.gradle ================================================ /* * 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. */ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' android { compileSdkVersion rootProject.ext.compileSdkVersion defaultConfig { applicationId "com.google.android.apps.flexbox" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { shrinkResources true minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation project(":flexbox") implementation "androidx.annotation:annotation:${rootProject.androidxAnnotationVersion}" implementation "androidx.appcompat:appcompat:${rootProject.androidxAppCompatVersion}" implementation "androidx.preference:preference:${rootProject.androidxPreferenceVersion}" implementation "com.google.android.material:material:${rootProject.materialVersion}" implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion" testImplementation "junit:junit:${rootProject.ext.junitVersion}" androidTestImplementation "androidx.annotation:annotation:${rootProject.androidxAnnotationVersion}" androidTestImplementation "androidx.test:runner:${rootProject.androidxTestVersion}" androidTestImplementation "androidx.test:rules:${rootProject.androidxTestVersion}" androidTestImplementation "androidx.test.espresso:espresso-core:${rootProject.androidxEspressoVersion}" } ================================================ FILE: demo-playground/proguard-rules.pro ================================================ # # 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. # # Add project specific ProGuard rules here. # By default, the flags in this file are appended to flags specified # in ${sdk.dir}/tools/proguard/proguard-android.txt # You can edit the include path and order by changing the proguardFiles # directive in build.gradle. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html # Add any project specific keep options here: # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} ================================================ FILE: demo-playground/src/androidTest/java/com/google/android/apps/flexbox/test/MainActivityTest.kt ================================================ /* * 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.test import android.content.pm.ActivityInfo import android.view.View import android.widget.ArrayAdapter import android.widget.RadioGroup import android.widget.Spinner import android.widget.TextView import androidx.test.InstrumentationRegistry import androidx.test.espresso.Espresso.onView import androidx.test.espresso.action.ViewActions.* import androidx.test.espresso.matcher.ViewMatchers.withId import androidx.test.filters.FlakyTest import androidx.test.filters.MediumTest import androidx.test.rule.ActivityTestRule import androidx.test.runner.AndroidJUnit4 import com.google.android.apps.flexbox.R import com.google.android.flexbox.* import com.google.android.material.navigation.NavigationView import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.core.Is.`is` import org.junit.Assert.* import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith /** * Integration tests for [MainActivity]. */ @RunWith(AndroidJUnit4::class) @MediumTest class MainActivityTest { @JvmField @Rule var activityRule = ActivityTestRule(MainActivity::class.java) @Test @FlakyTest fun testAddFlexItem() { val activity = activityRule.activity val flexboxLayout = activity.findViewById(R.id.flexbox_layout) assertNotNull(flexboxLayout) val beforeCount = flexboxLayout.childCount onView(withId(R.id.add_fab)).perform(click()) assertThat(flexboxLayout.childCount, `is`(beforeCount + 1)) } @Test @FlakyTest fun testRemoveFlexItem() { val activity = activityRule.activity val flexboxLayout = activity.findViewById(R.id.flexbox_layout) assertNotNull(flexboxLayout) val beforeCount = flexboxLayout.childCount onView(withId(R.id.remove_fab)).perform(click()) assertThat(flexboxLayout.childCount, `is`(beforeCount - 1)) } @Test @FlakyTest fun testConfigurationChange() { val activity = activityRule.activity val flexboxLayout = activity.findViewById(R.id.flexbox_layout) assertNotNull(flexboxLayout) onView(withId(R.id.add_fab)).perform(click()) onView(withId(R.id.add_fab)).perform(click()) onView(withId(R.id.add_fab)).perform(click()) val beforeCount = flexboxLayout.childCount activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE InstrumentationRegistry.getInstrumentation().waitForIdleSync() // Verify the flex items are restored across the configuration change. assertThat(flexboxLayout.childCount, `is`(beforeCount)) } @Test @FlakyTest fun testFlexDirectionSpinner() { val activity = activityRule.activity val flexboxLayout = activity.findViewById(R.id.flexbox_layout) assertNotNull(flexboxLayout) val navigationView = activity.findViewById(R.id.nav_view) assertNotNull(navigationView) val menu = navigationView.menu val spinner = menu.findItem(R.id.menu_item_flex_direction).actionView as Spinner val spinnerAdapter = spinner.adapter as ArrayAdapter val columnPosition = spinnerAdapter.getPosition(activity.getString(R.string.column)) activity.runOnUiThread { spinner.setSelection(columnPosition) } InstrumentationRegistry.getInstrumentation().waitForIdleSync() assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) val rowReversePosition = spinnerAdapter.getPosition(activity.getString(R.string.row_reverse)) activity.runOnUiThread { spinner.setSelection(rowReversePosition) } InstrumentationRegistry.getInstrumentation().waitForIdleSync() assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW_REVERSE)) } @Test @FlakyTest fun testFlexWrapSpinner() { val activity = activityRule.activity val flexboxLayout = activity.findViewById(R.id.flexbox_layout) assertNotNull(flexboxLayout) val navigationView = activity.findViewById(R.id.nav_view) assertNotNull(navigationView) val menu = navigationView.menu val spinner = menu.findItem(R.id.menu_item_flex_wrap).actionView as Spinner val spinnerAdapter = spinner.adapter as ArrayAdapter val wrapReversePosition = spinnerAdapter.getPosition(activity.getString(R.string.wrap_reverse)) activity.runOnUiThread { spinner.setSelection(wrapReversePosition) } InstrumentationRegistry.getInstrumentation().waitForIdleSync() assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP_REVERSE)) val noWrapPosition = spinnerAdapter.getPosition(activity.getString(R.string.nowrap)) activity.runOnUiThread { spinner.setSelection(noWrapPosition) } InstrumentationRegistry.getInstrumentation().waitForIdleSync() assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.NOWRAP)) } @Test @FlakyTest fun testJustifyContentSpinner() { val activity = activityRule.activity val flexboxLayout = activity.findViewById(R.id.flexbox_layout) as FlexboxLayout assertNotNull(flexboxLayout) val navigationView = activity.findViewById(R.id.nav_view) as NavigationView assertNotNull(navigationView) val menu = navigationView.menu val spinner = menu.findItem(R.id.menu_item_justify_content).actionView as Spinner val spinnerAdapter = spinner.adapter as ArrayAdapter val spaceBetweenPosition = spinnerAdapter.getPosition(activity.getString(R.string.space_between)) activity.runOnUiThread { spinner.setSelection(spaceBetweenPosition) } InstrumentationRegistry.getInstrumentation().waitForIdleSync() assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.SPACE_BETWEEN)) val centerPosition = spinnerAdapter.getPosition(activity.getString(R.string.center)) activity.runOnUiThread { spinner.setSelection(centerPosition) } InstrumentationRegistry.getInstrumentation().waitForIdleSync() assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.CENTER)) } @Test @FlakyTest fun testAlignItemsSpinner() { val activity = activityRule.activity val flexboxLayout = activity.findViewById(R.id.flexbox_layout) assertNotNull(flexboxLayout) val navigationView = activity.findViewById(R.id.nav_view) assertNotNull(navigationView) val menu = navigationView.menu val spinner = menu.findItem(R.id.menu_item_align_items).actionView as Spinner val spinnerAdapter = spinner.adapter as ArrayAdapter val baselinePosition = spinnerAdapter.getPosition(activity.getString(R.string.baseline)) activity.runOnUiThread { spinner.setSelection(baselinePosition) } InstrumentationRegistry.getInstrumentation().waitForIdleSync() assertThat(flexboxLayout.alignItems, `is`(AlignItems.BASELINE)) val flexEndPosition = spinnerAdapter.getPosition(activity.getString(R.string.flex_end)) activity.runOnUiThread { spinner.setSelection(flexEndPosition) } InstrumentationRegistry.getInstrumentation().waitForIdleSync() assertThat(flexboxLayout.alignItems, `is`(AlignItems.FLEX_END)) } @Test @FlakyTest fun testAlignContentSpinner() { val activity = activityRule.activity val flexboxLayout = activity.findViewById(R.id.flexbox_layout) assertNotNull(flexboxLayout) val navigationView = activity.findViewById(R.id.nav_view) assertNotNull(navigationView) val menu = navigationView.menu val spinner = menu.findItem(R.id.menu_item_align_content).actionView as Spinner val spinnerAdapter = spinner.adapter as ArrayAdapter val spaceAroundPosition = spinnerAdapter.getPosition(activity.getString(R.string.space_around)) activity.runOnUiThread { spinner.setSelection(spaceAroundPosition) } InstrumentationRegistry.getInstrumentation().waitForIdleSync() assertThat(flexboxLayout.alignContent, `is`(AlignContent.SPACE_AROUND)) val stretchPosition = spinnerAdapter.getPosition(activity.getString(R.string.stretch)) activity.runOnUiThread { spinner.setSelection(stretchPosition) } InstrumentationRegistry.getInstrumentation().waitForIdleSync() assertThat(flexboxLayout.alignContent, `is`(AlignContent.STRETCH)) } @Test @FlakyTest fun testEditFragment_changeOrder() { val activity = activityRule.activity val flexboxLayout = activity.findViewById(R.id.flexbox_layout) as FlexboxLayout assertNotNull(flexboxLayout) onView(withId(R.id.textview1)).perform(click()) onView(withId(R.id.edit_text_order)).perform(replaceText("3"), closeSoftKeyboard()) onView(withId(R.id.button_ok)).perform(click()) val first = flexboxLayout.getReorderedChildAt(0) as TextView val second = flexboxLayout.getReorderedChildAt(1) as TextView val third = flexboxLayout.getReorderedChildAt(2) as TextView assertThat(first.text.toString(), `is`("2")) assertThat(second.text.toString(), `is`("3")) assertThat(third.text.toString(), `is`("1")) } @Test @FlakyTest fun testEditFragment_changeFlexGrow() { val activity = activityRule.activity val flexboxLayout = activity.findViewById(R.id.flexbox_layout) as FlexboxLayout assertNotNull(flexboxLayout) onView(withId(R.id.textview1)).perform(click()) onView(withId(R.id.edit_text_flex_grow)).perform(replaceText("1"), closeSoftKeyboard()) onView(withId(R.id.button_ok)).perform(click()) val first = activity.findViewById(R.id.textview1) as TextView val second = activity.findViewById(R.id.textview2) as TextView val third = activity.findViewById(R.id.textview3) as TextView assertNotNull(first) assertNotNull(second) assertNotNull(third) assertThat(first.width, `is`(flexboxLayout.width - second.width - third.width)) } @Test @FlakyTest fun testEditFragment_changeFlexGrowFloat() { val activity = activityRule.activity val flexboxLayout = activity.findViewById(R.id.flexbox_layout) as FlexboxLayout assertNotNull(flexboxLayout) onView(withId(R.id.textview1)).perform(click()) onView(withId(R.id.edit_text_flex_grow)).perform(replaceText("1.0"), closeSoftKeyboard()) onView(withId(R.id.button_ok)).perform(click()) val first = activity.findViewById(R.id.textview1) as TextView val second = activity.findViewById(R.id.textview2) as TextView val third = activity.findViewById(R.id.textview3) as TextView assertNotNull(first) assertNotNull(second) assertNotNull(third) assertThat(first.width, `is`(flexboxLayout.width - second.width - third.width)) } @Test @FlakyTest fun testEditFragment_changeFlexBasisPercent() { val activity = activityRule.activity val flexboxLayout = activity.findViewById(R.id.flexbox_layout) as FlexboxLayout assertNotNull(flexboxLayout) onView(withId(R.id.textview1)).perform(click()) onView(withId(R.id.edit_text_flex_basis_percent)) .perform(replaceText("50"), closeSoftKeyboard()) onView(withId(R.id.button_ok)).perform(click()) val first = activity.findViewById(R.id.textview1) val second = activity.findViewById(R.id.textview2) val third = activity.findViewById(R.id.textview3) assertNotNull(first) assertNotNull(second) assertNotNull(third) assertTrue(first.width - 1 <= flexboxLayout.width / 2 || flexboxLayout.width / 2 <= first.width + 1) } @Test @FlakyTest fun testSwitchRecyclerViewFragment() { val activity = activityRule.activity val flexboxLayout = activity.findViewById(R.id.flexbox_layout) assertNotNull(flexboxLayout) val navigationView = activity.findViewById(R.id.nav_view) assertNotNull(navigationView) assertNull(activity.findViewById(R.id.recyclerview)) assertNotNull(activity.findViewById(R.id.flexbox_layout)) val radioGroup = navigationView.getHeaderView(0) .findViewById(R.id.radiogroup_container_implementation) activity.runOnUiThread { radioGroup.check(R.id.radiobutton_recyclerview) } InstrumentationRegistry.getInstrumentation().waitForIdleSync() assertNotNull(activity.findViewById(R.id.recyclerview)) assertNull(activity.findViewById(R.id.flexbox_layout)) } } ================================================ FILE: demo-playground/src/main/AndroidManifest.xml ================================================ ================================================ FILE: demo-playground/src/main/java/com/google/android/flexbox/Extensions.kt ================================================ /* * Copyright 2017 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.flexbox import android.content.Context /** * Convert pixel to dp. Preserve the negative value as it's used for representing * MATCH_PARENT(-1) and WRAP_CONTENT(-2). * Ignore the round error that might happen in dividing the pixel by the density. * * @param pixel the value in pixel * * @return the converted value in dp */ fun Context.pixelToDp(pixel: Int): Int { val displayMetrics = this.resources.displayMetrics return if (pixel < 0) pixel else Math.round(pixel / displayMetrics.density) } /** * Convert dp to pixel. Preserve the negative value as it's used for representing * MATCH_PARENT(-1) and WRAP_CONTENT(-2). * * @param dp the value in dp * * @return the converted value in pixel */ fun Context.dpToPixel(dp: Int): Int { val displayMetrics = this.resources.displayMetrics return if (dp < 0) dp else Math.round(dp * displayMetrics.density) } ================================================ FILE: demo-playground/src/main/java/com/google/android/flexbox/FlexItemAdapter.kt ================================================ /* * Copyright 2017 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.flexbox import android.view.LayoutInflater import android.view.ViewGroup import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.RecyclerView import com.google.android.apps.flexbox.R /** * [RecyclerView.Adapter] implementation for [FlexItemViewHolder]. */ internal class FlexItemAdapter(private val activity: AppCompatActivity, private val flexContainer: FlexContainer) : RecyclerView.Adapter() { private val layoutParams = mutableListOf() override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FlexItemViewHolder { val view = LayoutInflater.from(parent.context) .inflate(R.layout.viewholder_flex_item, parent, false) return FlexItemViewHolder(view) } override fun onBindViewHolder(holder: FlexItemViewHolder, position: Int) { val adapterPosition = holder.adapterPosition // TODO: More optimized set the click listener inside the view holder holder.itemView.setOnClickListener(FlexItemClickListener(activity, FlexItemChangedListenerImplRecyclerView(flexContainer, this), adapterPosition)) holder.bindTo(layoutParams[position]) } fun addItem(lp: FlexboxLayoutManager.LayoutParams) { layoutParams.add(lp) notifyItemInserted(layoutParams.size - 1) } fun removeItem(position: Int) { if (position < 0 || position >= layoutParams.size) { return } layoutParams.removeAt(position) notifyItemRemoved(layoutParams.size) notifyItemRangeChanged(position, layoutParams.size) } val items get() = layoutParams override fun getItemCount() = layoutParams.size } ================================================ FILE: demo-playground/src/main/java/com/google/android/flexbox/FlexItemChangedListener.kt ================================================ /* * Copyright 2017 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.flexbox /** * A listener that listens to the change of a flex item */ internal interface FlexItemChangedListener { fun onFlexItemChanged(flexItem: FlexItem, viewIndex: Int) } ================================================ FILE: demo-playground/src/main/java/com/google/android/flexbox/FlexItemChangedListenerImpl.kt ================================================ /* * Copyright 2017 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.flexbox import android.view.ViewGroup /** * Default implementation for the [FlexItemChangedListener]. */ internal class FlexItemChangedListenerImpl(private val flexContainer: FlexContainer) : FlexItemChangedListener { override fun onFlexItemChanged(flexItem: FlexItem, viewIndex: Int) { val view = flexContainer.getFlexItemAt(viewIndex) view.layoutParams = flexItem as ViewGroup.LayoutParams } } ================================================ FILE: demo-playground/src/main/java/com/google/android/flexbox/FlexItemChangedListenerImplRecyclerView.kt ================================================ /* * Copyright 2017 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.flexbox import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView /** * Implementation for the [FlexItemChangedListener]. * It expects RecyclerView as the underlying flex container implementation. */ internal class FlexItemChangedListenerImplRecyclerView(private val flexContainer: FlexContainer, private val adapter: RecyclerView.Adapter<*>) : FlexItemChangedListener { override fun onFlexItemChanged(flexItem: FlexItem, viewIndex: Int) { val view = flexContainer.getFlexItemAt(viewIndex) view.layoutParams = flexItem as ViewGroup.LayoutParams adapter.notifyDataSetChanged() // TODO: An Exception is thrown if notifyItemChanged(int) is used. // Investigate that, but using LinearLayoutManager also produces the same Exception // java.lang.IllegalArgumentException: Called attach on a child which is not detached: } } ================================================ FILE: demo-playground/src/main/java/com/google/android/flexbox/FlexItemClickListener.kt ================================================ /* * Copyright 2017 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.flexbox import android.view.View import androidx.appcompat.app.AppCompatActivity /** * Implementation of the [android.view.View.OnClickListener] when a flex item is clicked in * the Flexbox Playground demo app. */ internal class FlexItemClickListener(private val activity: AppCompatActivity, private val flexItemChangedListener: FlexItemChangedListener, private val viewIndex: Int) : View.OnClickListener { override fun onClick(v: View) = FlexItemEditFragment.newInstance(v.layoutParams as FlexItem, viewIndex).apply { setFlexItemChangedListener(flexItemChangedListener) }.show(activity.supportFragmentManager, EDIT_DIALOG_TAG) companion object { private const val EDIT_DIALOG_TAG = "edit_dialog_tag" } } ================================================ FILE: demo-playground/src/main/java/com/google/android/flexbox/FlexItemEditFragment.kt ================================================ /* * Copyright 2017 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.flexbox import android.content.Context import android.os.Build import android.os.Bundle import android.text.Editable import android.text.TextWatcher import android.view.KeyEvent import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.view.inputmethod.EditorInfo import android.view.inputmethod.InputMethodManager import android.widget.* import androidx.fragment.app.DialogFragment import com.google.android.apps.flexbox.R import com.google.android.flexbox.validators.* import com.google.android.material.textfield.TextInputLayout /** * DialogFragment that changes the properties for a flex item. */ internal class FlexItemEditFragment : DialogFragment() { private lateinit var alignSelfAuto: String private lateinit var alignSelfFlexStart: String private lateinit var alignSelfFlexEnd: String private lateinit var alignSelfCenter: String private lateinit var alignSelfBaseline: String private lateinit var alignSelfStretch: String private var viewIndex: Int = 0 private lateinit var flexItem: FlexItem /** * Instance of a [FlexItem] being edited. At first it's created as another instance from * the [flexItem] because otherwise changes before clicking the ok button will be * reflected if the [flexItem] is changed directly. */ private lateinit var flexItemInEdit: FlexItem private var flexItemChangedListener: FlexItemChangedListener? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { setStyle(STYLE_NORMAL, android.R.style.Theme_Material_Light_Dialog) } else { setStyle(STYLE_NORMAL, android.R.style.Theme_Dialog) } arguments?.let { flexItem = it.getParcelable(FLEX_ITEM_KEY)!! viewIndex = it.getInt(VIEW_INDEX_KEY) } flexItemInEdit = createNewFlexItem(flexItem) activity?.let { alignSelfAuto = it.getString(R.string.auto) alignSelfFlexStart = it.getString(R.string.flex_start) alignSelfFlexEnd = it.getString(R.string.flex_end) alignSelfCenter = it.getString(R.string.center) alignSelfBaseline = it.getString(R.string.baseline) alignSelfStretch = it.getString(R.string.stretch) } } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view = inflater.inflate(R.layout.fragment_flex_item_edit, container, false) dialog?.setTitle((viewIndex + 1).toString()) val context = activity ?: return view val orderTextInput: TextInputLayout = view.findViewById(R.id.input_layout_order) val orderEdit: EditText = view.findViewById(R.id.edit_text_order) orderEdit.setText(flexItem.order.toString()) orderEdit.addTextChangedListener( FlexEditTextWatcher(context, orderTextInput, IntegerInputValidator(), R.string.must_be_integer)) if (flexItem is FlexboxLayoutManager.LayoutParams) { // Order is not enabled in FlexboxLayoutManager orderEdit.isEnabled = false } val flexGrowInput: TextInputLayout = view .findViewById(R.id.input_layout_flex_grow) val flexGrowEdit: EditText = view.findViewById(R.id.edit_text_flex_grow) flexGrowEdit.setText(flexItem.flexGrow.toString()) flexGrowEdit.addTextChangedListener( FlexEditTextWatcher(context, flexGrowInput, NonNegativeDecimalInputValidator(), R.string.must_be_non_negative_float)) val flexShrinkInput: TextInputLayout = view.findViewById(R.id.input_layout_flex_shrink) val flexShrinkEdit: EditText = view.findViewById(R.id.edit_text_flex_shrink) flexShrinkEdit.setText(flexItem.flexShrink.toString()) flexShrinkEdit.addTextChangedListener( FlexEditTextWatcher(context, flexShrinkInput, NonNegativeDecimalInputValidator(), R.string.must_be_non_negative_float)) val flexBasisPercentInput: TextInputLayout = view.findViewById(R.id.input_layout_flex_basis_percent) val flexBasisPercentEdit: EditText = view.findViewById(R.id.edit_text_flex_basis_percent) if (flexItem.flexBasisPercent != FlexboxLayout.LayoutParams.FLEX_BASIS_PERCENT_DEFAULT) { flexBasisPercentEdit .setText(Math.round(flexItem.flexBasisPercent * 100).toString()) } else { flexBasisPercentEdit.setText(flexItem.flexBasisPercent.toInt().toString()) } flexBasisPercentEdit.addTextChangedListener( FlexEditTextWatcher(context, flexBasisPercentInput, FlexBasisPercentInputValidator(), R.string.must_be_minus_one_or_non_negative_integer)) val widthInput: TextInputLayout = view.findViewById(R.id.input_layout_width) val widthEdit: EditText = view.findViewById(R.id.edit_text_width) widthEdit.setText(context.pixelToDp(flexItem.width).toString()) widthEdit.addTextChangedListener( FlexEditTextWatcher(context, widthInput, DimensionInputValidator(), R.string.must_be_minus_one_or_minus_two_or_non_negative_integer)) val heightInput: TextInputLayout = view.findViewById(R.id.input_layout_height) val heightEdit: EditText= view.findViewById(R.id.edit_text_height) heightEdit.setText(context.pixelToDp(flexItem.height).toString()) heightEdit.addTextChangedListener( FlexEditTextWatcher(context, heightInput, DimensionInputValidator(), R.string.must_be_minus_one_or_minus_two_or_non_negative_integer)) val minWidthInput: TextInputLayout = view.findViewById(R.id.input_layout_min_width) val minWidthEdit: EditText = view.findViewById(R.id.edit_text_min_width) minWidthEdit.setText(context.pixelToDp(flexItem.minWidth).toString()) minWidthEdit.addTextChangedListener( FlexEditTextWatcher(context, minWidthInput, FixedDimensionInputValidator(), R.string.must_be_non_negative_integer)) val minHeightInput: TextInputLayout = view.findViewById(R.id.input_layout_min_height) val minHeightEdit: EditText = view.findViewById(R.id.edit_text_min_height) minHeightEdit.setText(context.pixelToDp(flexItem.minHeight).toString()) minHeightEdit.addTextChangedListener( FlexEditTextWatcher(context, minHeightInput, FixedDimensionInputValidator(), R.string.must_be_non_negative_integer)) val maxWidthInput: TextInputLayout = view.findViewById(R.id.input_layout_max_width) val maxWidthEdit: EditText = view.findViewById(R.id.edit_text_max_width) maxWidthEdit.setText(context.pixelToDp(flexItem.maxWidth).toString()) maxWidthEdit.addTextChangedListener( FlexEditTextWatcher(context, maxWidthInput, FixedDimensionInputValidator(), R.string.must_be_non_negative_integer)) val maxHeightInput: TextInputLayout = view.findViewById(R.id.input_layout_max_height) val maxHeightEdit: EditText = view.findViewById(R.id.edit_text_max_height) maxHeightEdit.setText(context.pixelToDp(flexItem.maxHeight).toString()) maxHeightEdit.addTextChangedListener( FlexEditTextWatcher(context, maxHeightInput, FixedDimensionInputValidator(), R.string.must_be_non_negative_integer)) setNextFocusesOnEnterDown(orderEdit, flexGrowEdit, flexShrinkEdit, flexBasisPercentEdit, widthEdit, heightEdit, minWidthEdit, minHeightEdit, maxWidthEdit, maxHeightEdit) val alignSelfSpinner: Spinner = view.findViewById(R.id.spinner_align_self) val arrayAdapter = ArrayAdapter.createFromResource(requireActivity(), R.array.array_align_self, R.layout.spinner_item) alignSelfSpinner.adapter = arrayAdapter alignSelfSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onItemSelected(parent: AdapterView<*>, ignored: View?, position: Int, id: Long) { flexItemInEdit.alignSelf = when (parent.getItemAtPosition(position).toString()) { alignSelfAuto -> AlignSelf.AUTO alignSelfFlexStart -> AlignItems.FLEX_START alignSelfFlexEnd -> AlignItems.FLEX_END alignSelfCenter -> AlignItems.CENTER alignSelfBaseline -> AlignItems.BASELINE alignSelfStretch -> AlignItems.STRETCH else -> return } } override fun onNothingSelected(parent: AdapterView<*>) { // No op } } val wrapBeforeCheckBox: CheckBox = view.findViewById(R.id.checkbox_wrap_before) wrapBeforeCheckBox.isChecked = flexItem.isWrapBefore wrapBeforeCheckBox.setOnCheckedChangeListener { _, isChecked -> flexItemInEdit.isWrapBefore = isChecked } val alignSelfPosition = arrayAdapter .getPosition(alignSelfAsString(flexItem.alignSelf)) alignSelfSpinner.setSelection(alignSelfPosition) view.findViewById