ChipTextView
ChipTextView is a simple lightweight Android library purely written in Kotlin. The library provides a view that draws text within a rounded colored rectangle as a background.
Minimal Android version - 15.
Installing
To add this library to your project simply add the following line into your app level build.gradle
file:
implementation 'com.apandroid:chiptextview:1.0.15'
Or if you want to use some app-compat
features (custom fonts) add the following line:
implementation 'com.apandroid:chiptextviewappcompat:1.0.15'
// or if you are using androidx
implementation 'com.apandroid:chiptextviewandroidx:1.0.15'
For most Android projects this will be enough but if you have any problems just make sure that you have the jcenter()
function called inside your repositories
closure in your root build.gradle
file.
Getting Started
To start using this library just add ChipTextView
(AppCompatChipTextView
, AndroidXChipTextView
) class to your xml layout file.
<com.apandroid.chiptextview.ChipTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- If you want to use app-compat version of the library -->
<com.apandroid.chiptextviewcompat.AppCompatChipTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- If you want to use androidx version of the library -->
<com.apandroid.chiptextviewandroidx.AndroidXChipTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Customizing
You can use the following properties to customize the look of your ChipTextView
:
Style Properties
-
ctv_text
- Text that will be drawn -
ctv_textSize
- Text size -
ctv_textColor
- Text color -
ctv_chipColor
- Chip background color -
ctv_font
- Custom font. Available only for API ≥ 26 -
ctv_fontCompat
- Custom font. Available in the app-compat version of the library viaAppCompatChipTextView
orAndroidXChipTextView
-
ctv_chipStrokeColor
- Color of a stroke that will be drawn around a chip -
ctv_chipStrokeWidth
- Width of a stroke that will be drawn around a chip -
ctv_chipBackgroundDrawable
- Custom drawable which can be used as an additional background along with a simple color background.
Paddings Properties
The following properties are used to control paddings of rounded rectangle within which text is drawn.
ctv_chipPadding
ctv_chipPaddingVertical
ctv_chipPaddingHorizontal
ctv_chipPaddingStart
ctv_chipPaddingEnd
ctv_chipPaddingBottom
ctv_chipPaddingTop
Elevation
Feel free to use the android:elevation
property because the library comes along with implementation of OutlineProvider
.
Styles and Themes
Also you can use the chipTextViewStyle
theme attribute to customize a look of all of your ChipTextView
in a particular theme. For example:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="chipTextViewStyle">@style/DefaultChipTextView</item>
</style>
<style name="DefaultChipTextView" parent="ChipTextViewDefaultStyle">
<item name="ctv_textSize">72sp</item>
</style>
Default ChipTextViewStyle comes along with library.
<style name="ChipTextViewDefaultStyle">
<item name="ctv_chipPadding">10dp</item>
<item name="ctv_text">Chip Text</item>
<item name="ctv_textSize">14sp</item>
<item name="ctv_textColor">@android:color/white</item>
<item name="ctv_chipColor">@android:color/black</item>
<item name="ctv_chipStrokeColor">@android:color/transparent</item>
<item name="ctv_chipStrokeWidth">4dp</item>
</style>
Helper functions and properties
-
paintFlags
- Allows manually set text paint flags. -
strikeThroughText()/hideStrikeThroughText()
- Shows/hides a strike through a chip text. -
underlineText()/hideTextUnderline()
- Shows/hides a chip text underline.
Author
Anton Popov - initial work.
Feel free to report issues and make pull requests.
License
MIT