In this tutorial we learn how to implement tag layout in android. By default android is not providing any layout for displaying tags. Here we will try an example to achieve it using a library named ChipCloud. First step is to compile this library in your build.gradle.
compile 'com.github.fiskurgit:ChipCloud:2.1.0'
You need to add the following repository also to your build.gradle
repositories { maven { url "https://jitpack.io" } }
Now use the below code in your layout file
<eu.fiskur.chipcloud.ChipCloud android:id="@+id/tag" android:layout_width="match_parent" android:layout_height="wrap_content"/>
Now simply use the below code in your Activity’s java file when you want to add tags.
ChipCloud tag_layout = (ChipCloud) findViewById(R.id.tag); tag_layout.addChip("Apple"); tag_layout.addChip("Orange"); tag_layout.addChip("Grape"); tag_layout.addChip("Pinapple"); tag_layout.addChip("Banana");
if you want to make list of tags with single choice option ,use:
tag_layout.setMode(ChipCloud.Mode.SINGLE);
For multichoice mode
tag_layout.setMode(ChipCloud.Mode.MULTI);
If it is multichoice mode you can use the following code to get the selected/unselected status
if(tag_layout.isSelected(position)){ }
You can write actions on clicking tag using following code
tag_layout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // your action comes here } });
You can change the selected and unselected colors of tag using the following code
tag_layout.setSelectedColor(Color.BLACK); tag_layout.setSelectedColor(Color.BLUE);
