0

This is the screenshot of what I am working on.

enter image description here

There are two classes involved here. DraggableLayout and DraggableChip. Now, I want to change the style of the chip to R.style.Widget_MaterialComponents_Chip_Entry

This is what the code looks like:

private void setupDraggableChipLayout() {
        DraggableLayout draggableLayout = this.findViewById(R.id.draggableLayout);

        androidx.appcompat.view.ContextThemeWrapper contextWrapper = new ContextThemeWrapper(this, R.style.Widget_MaterialComponents_Chip_Entry);
        DraggableChip chip1 = new DraggableChip(contextWrapper, null, 0);
        chip1.setText("Chip1");
        chip1.enableCancelIcon(true);


        DraggableChip chip2 = new DraggableChip(this);
        chip2.setText("Chip2");



        DraggableChip chip3 = new DraggableChip(this);
        chip3.setText("Chip3");

        draggableLayout.addChip(chip1);
        draggableLayout.addChip(chip2);
        draggableLayout.addChip(chip3);

    }

I have tried using ContextWrapper but it does not work.

I have referred to this answer. But how to do it without using a layout file?

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Harshal Kshatriya
  • 5,630
  • 11
  • 44
  • 60
  • 1
    I've updated the [linked answer](https://stackoverflow.com/questions/53557863/change-chip-widget-style-programmatically-not-working-android/58247341#58247341). The only way to avoid the layout file is to use the `chip.setChipDrawable(chipDrawable)` method. In the Chip there isn't the 4th parameter to set the style in the constructor. – Gabriele Mariotti Dec 12 '19 at 09:48
  • try this: https://stackoverflow.com/a/59390473/1079842 – Mojtaba Haddadi Dec 26 '19 at 17:40
  • 2nd option in [this answer](https://stackoverflow.com/a/58247341/1117531) worked for me. – Harshal Kshatriya Jan 06 '20 at 09:41

0 Answers0