Created
July 5, 2020 15:01
-
-
Save smdevidan/8d6b2943c358c8459cdc7895dcc9c6bd to your computer and use it in GitHub Desktop.
Adjustable pills inflater
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void inflate(Context context, | |
double maxAvailableWidthScaleFactor, | |
LinearLayout containerView, | |
int selectOptionInflaterLayout, | |
int optionTextViewId, | |
List<String> pillTitles, | |
View.OnClickListener optionSelectionListener) { | |
List<LinearLayout> layouts = new ArrayList<>(); | |
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); | |
// Scale down from max width with the given maxAvailableWidthScaleFactor and exclusion for padding. | |
int totalAvailableWidth = (int) (displayMetrics.widthPixels * maxAvailableWidthScaleFactor); | |
int size = options.size(); | |
int i = 0; | |
// Loop to add horizontal linear layouts until we have adjusted all the options. | |
while (i < size) { | |
// Create a horizontal oriented layout | |
LinearLayout linearLayout = new LinearLayout(context); | |
linearLayout.setOrientation(LinearLayout.HORIZONTAL); | |
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, | |
LinearLayout.LayoutParams.WRAP_CONTENT)); | |
linearLayout.setGravity(Gravity.END); | |
// This loop breaks when the occupied size exceeds the total available or the options list is finished. | |
while (true) { | |
// Inflate option content and add option text to the text view. | |
final View optionContentView = LayoutInflater.from(context).inflate(selectOptionInflaterLayout, null, false); | |
final TextView optionTextView = optionContentView.findViewById(optionTextViewId); | |
optionTextView.setMaxWidth(totalAvailableWidth); | |
String title = pillTitles.get(i); | |
optionTextView.setTag(title); | |
optionTextView.setText(title); | |
optionTextView.setOnClickListener(optionSelectionListener); | |
// Add the inflated option content to linear layout and check for measured width. | |
linearLayout.addView(optionContentView); | |
linearLayout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); | |
int occupiedWidth = linearLayout.getMeasuredWidth(); | |
// If the width exceeds the total available then remove the last added view and retain | |
// the current linear layout to be added to the final containerView. | |
if (occupiedWidth > totalAvailableWidth) { | |
// If only one view exceeds the max limits then increment the i count else remove the latest added view. | |
if (linearLayout.getChildCount() == 1) { | |
i++; | |
} | |
else { | |
// Do not increment i count since we want to current option item to be included in the next iteration. | |
linearLayout.removeView(optionContentView); | |
} | |
layouts.add(linearLayout); | |
break; | |
} | |
else { | |
// If the item is the last in the list then retain the layout since loop will break after this. | |
if (i == (size - 1)) { | |
layouts.add(linearLayout); | |
} | |
// Increment to move on to next option item. | |
i++; | |
} | |
if (i >= size) { | |
break; | |
} | |
} | |
} | |
// Add all the generated layouts to the containerView. | |
for (LinearLayout l : layouts) { | |
containerView.addView(l); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment