Home

Awesome

AndroidTagView

Build Status Android Arsenal

An Android TagView library. You can customize awesome TagView by using this library.

Screenshots

<img src="/screenshots/androidtagview_record_1.gif" alt="androidtagview_record_1.gif" title="androidtagview_record_1.gif" width="400" height="660" /> <img src="/screenshots/device-2016-11-09-223523.png" alt="device-2016-11-09-223523.png" title="device-2016-01-16-233617.png" width="400" height="660" />

Usage

Step 1

Add below dependency in your build.gradle file.

dependencies {
    implementation 'co.lujun:androidtagview:1.1.7'
    implementation 'androidx.appcompat:appcompat:1.0.1'
}

Step 2

Use the AndroidTagView in layout file, you can add customized attributes here.

<co.lujun.androidtagview.TagContainerLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="0dp"
    android:padding="10dp"
    app:container_enable_drag="false"
    app:horizontal_interval="10dp"
    app:vertical_interval="10dp"
    app:tag_clickable="true"
    app:tag_theme="pure_teal" />

Step 3

Use TagView in your code.

TagContainerLayout mTagContainerLayout = (TagContainerLayout) findViewById(R.id.tagcontainerLayout);
mTagContainerLayout.setTags(List<String> tags);

Now, you have successfully created some TagViews. The following will show some more useful features for you customize.

Attributes

nameformatdescription
vertical_intervaldimensionVertical interval, default 5(dp)
horizontal_intervaldimensionHorizontal interval, default 5(dp)
container_border_widthdimensionTagContainerLayout border width(default 0.5dp)
container_border_radiusdimensionTagContainerLayout border radius(default 10.0dp)
container_border_colorcolorTagContainerLayout border color(default #22FF0000)
container_background_colorcolorTagContainerLayout background color(default #11FF0000)
container_enable_dragbooleanCan drag TagView(default false)
container_drag_sensitivityfloatThe sensitive of the ViewDragHelper(default 1.0f, normal)
container_gravityenumThe TagContainerLayout gravity
container_max_linesintegerThe max lines for TagContainerLayout(default 0, auto increase)
tag_border_widthdimensionTagView Border width(default 0.5dp)
tag_corner_radiusdimensionTagView Border radius(default 15.0dp)
tag_horizontal_paddingdimensionHorizontal padding for TagView, include left and right padding(left and right padding are equal, default 10dp)
tag_vertical_paddingdimensionVertical padding for TagView, include top and bottom padding(top and bottom padding are equal, default 8dp)
tag_text_sizedimensionTagView Text size(default 14sp)
tag_bd_distancedimensionThe distance between baseline and descent(default 2.75dp)
tag_text_colorcolorTagView text color(default #FF666666)
tag_border_colorcolorTagView border color(default #88F44336)
tag_background_colorcolorTagView background color(default #33F44336)
tag_max_lengthintegerThe max length for TagView(default max length 23)
tag_clickablebooleanWhether TagView can clickable(default false)
tag_selectablebooleanWhether TagView can be selectable(default false)
tag_themeenumThe TagView theme
tag_text_directionenumThe TagView text direction
tag_ripple_colorcolorThe ripple effect color(default #EEEEEE)
tag_ripple_alphaintegerThe ripple effect color alpha(the value may between 0 - 255, default 128)
tag_ripple_durationintegerThe ripple effect duration(In milliseconds, default 1000ms)
tag_enable_crossbooleanEnable draw cross icon(default false)
tag_cross_widthdimensionThe cross area width(your cross click area, default equal to the TagView's height)
tag_cross_colorcolorThe cross icon color(default Color.BLACK)
tag_cross_line_widthdimensionThe cross line width(default 1dp)
tag_cross_area_paddingdimensionThe padding of the cross area(default 10dp)
tag_support_letters_rltbooleanWhether to support 'letters show with RTL(eg: Android -> diordnA)' style(default false)
tag_backgroundreferenceTagView background resource(default none background)

You can set these attributes in layout file, or use setters(each attribute has get and set method) to set them.

<span id="themes">Themes</span>

themecodevaluedescription
noneColorFactory.NONE-1If you customize TagView with your way, set this theme
randomColorFactory.RANDOM0Create each TagView using random color
pure_cyanColorFactory.PURE_CYAN1All TagView created by pure cyan color
pure_tealColorFactory.PURE_TEAL2All TagView created by pure teal color

<span id="directions">Directions</span>

directioncodevaluedescription
ltrView.TEXT_DIRECTION_LTR3Text direction is forced to LTR(default)
rtlView.TEXT_DIRECTION_RTL4Text direction is forced to RTL

<span id="gravity">Gravity</span>

gravitycodevaluedescription
leftGravity.LEFT3Push TagView to the left of TagContainerLayout(default)
centerGravity.CENTER17Push TagView to the center of TagContainerLayout
rightGravity.RIGHT5Push TagView to the right of TagContainerLayout

<span id="Methods">Methods</span>

mTagContainerLayout.setOnTagClickListener(new TagView.OnTagClickListener() {

    @Override
    public void onTagClick(int position, String text) {
        // ...
    }

    @Override
    public void onTagLongClick(final int position, String text) {
        // ...
    }

    @Override
    public void onSelectedTagDrag(int position, String text){
        // ...
    }
    
    @Override
    public void onTagCrossClick(int position) {
        // ...
    }
});
mTagContainerLayout.setTagMaxLength(int max);
String text = mTagContainerLayout.getTagText(int position);
List<String> list = mTagContainerLayout.getTags();
int state = mTagContainerLayout.getTagViewState();
// Set library provides theme
mTagContainerLayout.setTheme(ColorFactory.PURE_CYAN);
// Set customize theme
mTagContainerLayout.setTheme(ColorFactory.NONE);
mTagContainerLayout.setTagBackgroundColor(Color.TRANSPARENT);
mTagContainerLayout.setTagTextDirection(View.TEXT_DIRECTION_RTL);
Typeface typeface = Typeface.createFromAsset(getAssets(), "iran_sans.ttf");
mTagContainerLayout.setTagTypeface(typeface);

After set the attributes, set tags or add a tag.

mTagContainerLayout.setTags(List<String> tags);
mTagContainerLayout.addTag(String text);
mTagContainerLayout.addTag(String text, int position);
mTagContainerLayout.removeTag(int position);
mTagContainerLayout.removeAllTags();
mTagContainerLayout.getTagView(int position);
List<int[]> colors = new ArrayList<int[]>();
//int[] color = {TagBackgroundColor, TabBorderColor, TagTextColor, TagSelectedBackgroundColor}
int[] color1 = {Color.RED, Color.BLACK, Color.WHITE, Color.YELLOW};
int[] color2 = {Color.BLUE, Color.BLACK, Color.WHITE, Color.YELLOW};
colors.add(color1);
colors.add(color2);
mTagcontainerLayout.setTags(tags, colors);

Change logs

1.1.7(2019-01-21)

1.1.6(2018-12-1)

1.1.5(2018-8-20)

1.1.4(2017-6-1)

1.1.3(2017-5-17)

1.1.2(2017-5-16)

1.1.1(2017-4-16)

1.1.0(2017-3-5)

1.0.6(2017-2-14)

1.0.5(2016-11-9)

1.0.4(2016-10-30)

1.0.3(2016-4-3)

1.0.2(2016-1-18)

1.0.1(2016-1-14)

1.0.0(2016-1-6)

Sample App

APK

About

If you have any questions, contact me: lujun.byte#gmail.com.

License

Copyright 2015 lujun

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.