How to dynamically create Entry Chip in Android

In this android programming source code example, we are going to dynamically create Entry Chip in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to dynamically create Entry Chip in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    android:background="#CACACA">

    <com.google.android.material.chip.ChipGroup
        android:id="@+id/user"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;

import com.bluapp.androidview2.R;
import com.google.android.material.chip.Chip;
import com.google.android.material.chip.ChipDrawable;
import com.google.android.material.chip.ChipGroup;

public class AndroidChipActivity4 extends AppCompatActivity {
    private ChipGroup chipGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_chip4);
        chipGroup = (ChipGroup)findViewById(R.id.user);
        Chip maleChip = getChip(chipGroup, "Male");
        Chip femaleChip = getChip(chipGroup, "Female");
        chipGroup.addView(maleChip);
        chipGroup.addView(femaleChip);
    }

    private Chip getChip(final ChipGroup chipGroup, String text){
        final Chip chip = new Chip(this);
        chip.setChipDrawable(ChipDrawable.createFromResource(this, R.xml.chip));
        int paddingDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
        chip.setPadding(paddingDp, paddingDp, paddingDp, paddingDp);
        chip.setText(text);
        chip.setOnCloseIconClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chipGroup.removeView(chip);
            }
        });
        return chip;
    }
}

xml/chip.xml

<chip
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:checkable="false"
    app:chipIcon="@drawable/ic_user"
    app:iconStartPadding="5dp"
    style="@style/Widget.MaterialComponents.Chip.Entry"/>

values/styles.xml

<style name="ChipTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

app/build.gradle

implementation 'com.google.android.material:material:1.0.0'

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment