Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android chips which are horizontally scrollable

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.

enter image description here enter image description here

like image 957
Michele La Ferla Avatar asked Oct 04 '16 19:10

Michele La Ferla


3 Answers

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>
like image 187
adriennoir Avatar answered Sep 19 '22 11:09

adriennoir


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>
like image 41
Vishal Nagvadiya Avatar answered Sep 21 '22 11:09

Vishal Nagvadiya


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);
            }
        });
    }
like image 31
Rameswar Tarai Avatar answered Sep 20 '22 11:09

Rameswar Tarai