I need to implement a solution using android chips in a horizontally scrollable view. All the libraries I found use a multiline solution when having many chips to filter with. However I wish mine to be in a single line and scroll through its container.
I know that apps such as Pinterest already use this and concept, however I have no way how to go around it.
Try placing your ChipGroup inside of a HorizontalScrollView
layout.
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.design.chip.ChipGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.design.chip.Chip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:chipText="This" />
<android.support.design.chip.Chip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:chipText="is" />
// more chips...
</android.support.design.chip.ChipGroup>
</HorizontalScrollView>
use this to hide scrollbars
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<com.google.android.material.chip.ChipGroup
android:id="@+id/chipsPrograms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:paddingStart="@dimen/text_margin"
android:paddingEnd="@dimen/text_margin"
app:chipSpacing="8dp"
app:singleSelection="true"/>
</HorizontalScrollView>
Try to place ChipGroup inside HorizontalScrollView
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.design.chip.ChipGroup
android:id="@+id/chipGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</HorizontalScrollView>
And Then add chips to the ChipGroup dinamically
for(String string:set){
Chip chip = new Chip(binding.chipGroup.getContext());
LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(5,5,5,5);
chip.setLayoutParams(layoutParams);
chip.setText(string);
chip.setCloseIconEnabled(true);
chip.setChipBackgroundColor(getResources().getColorStateList(R.color.colorChipIconTint));
chip.setTextColor(getResources().getColorStateList(R.color.colorChipText));
chip.setCloseIconTint(getResources().getColorStateList(R.color.colorChipCloseIcon));
chip.setClickable(true);
chip.setCheckable(false);
binding.chipGroup.addView(chip );
chip.setOnCloseIconClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//binding.chipGroup.removeView(v);
setDataContainer.remove((String)((Chip)v).getText());
}
});
chip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SMSHomeActivity.this,PhoneNumberActivity.class);
ArrayList<GroupMobileNumberModel> groupMobileNumberModelList = (ArrayList<GroupMobileNumberModel>) PhoneNumberActivity.groupMobileNumberModelList();
intent.putParcelableArrayListExtra("groupMobileNumberModelList",groupMobileNumberModelList);
startActivity(intent);
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With