Moment For Technology

One line of code to set the Shape Selector style

Posted on Nov. 27, 2023, 1:49 a.m. by Harriet Thompson-Hughes
Category: android Tag: android github gradle Android Studio


Original intention of the project: Company project n + 1 style file, file naming various people development, to search for half a day, every time I watched a lot of similar style Settings library on the Internet, most of it is a custom View, I just want to code directly set style, more convenient, in their spare time to write this program, code directly set style, not too many attributes you need to set up, Simple to use, in order to more convenient, fast, time-saving project development.

Github project address

1. Function introduction

  • Shape styles: Circle, rounded Corner, border, dotted border, color gradient
  • Selector style: Touch background changes, touch font color changes

2. How to use it

2.1 Android Studio import method, add Gradle dependency

Add the following to your project build.gradle:

dependencies {
        implementation 'cn. Luliangdev: DevShapeUtils: 1.0.1'
Copy the code

2.2 Used in the project

PS: minSdkVersion is set to 16 in the project, otherwise the compilation fails. (Android4.1, the current mobile version is basically above 4.4)
android {
    defaultConfig {
        minSdkVersion 16. }}Copy the code
2.2.1 Initialization in project Application
public class DevApplication extends Application {
    public void onCreate(a) {
        DevShapeUtils.init(this); }}Copy the code
Used in 2.2.2 code
  • into (view)-- set directly to the corresponding view ② build()-- return Drawable
  • Color Settings: all color Settings can be r.color. XXX or #FFFFFF
  • Unit setting: All values in the project are in dp Shape style

Effect display:

Code examples:

/ / oval
/ / rectangle
/ / the rounded
// Rounded (top left rounded)
/ / half rounded corners
// Solid rounded border (line parameter 1: border width parameter 2: border color)
DevShapeUtils.shape(DevShape.RECTANGLE).line(1, R.color.colorAccent).radius(10).into(view);
// Dash rounded border (dashLine parameters 1: dash width parameters 2: dash color parameters 3: dash width parameters 4: dash gap width)
DevShapeUtils.shape(DevShape.RECTANGLE).dashLine(1, R.color.colorPrimary, 5.5).radius(10).into(view);
// Gradient (default linear gradient up and down)
DevShapeUtils.shape(DevShape.RECTANGLE).gradient(R.color.colorAccent, R.color.colorPrimary).into(view);
// gradientLinear (gradientLinear parameter 1: gradient direction parameter 2: gradient color, the number of colors must be two or more)
DevShapeUtils.shape(DevShape.RECTANGLE).gradientLinear(DevShape.TOP_BOTTOM, R.color.colorAccent, R.color.colorPrimary).into(view);
(gradientSweep parameter 1: gradientSweep color, the number of colors must be two or more)
DevShapeUtils.shape(DevShape.OVAL).gradientSweep(R.color.colorAccent, R.color.colorPrimary).into(view);
// gradientRadial parameter 1: radiation range parameter 2: gradient color, the number of colors must be two or more)
DevShapeUtils.shape(DevShape.OVAL).gradientRadial(30, R.color.colorAccent, R.color.colorPrimary).into(view);
Copy the code Selector style

Effect display:

Code examples:

// Touch background color change (selectorBackground parameter 1: touch color parameter 2 normal color)
// Touch background color and font color changes (selectorColor parameter 1: Touch color parameter 2 Normal color)
        .selectorColor("#ffffff"."# 000000")
// Touch the rounded background and font color change
Drawable pressedDrawable = DevShapeUtils.shape(DevShape.RECTANGLE).solid(R.color.colorAccent).radius(10).build();
Drawable normalDrawable = DevShapeUtils.shape(DevShape.RECTANGLE).solid(R.color.colorPrimary).radius(10).build();
        .selectorColor("#ffffff"."# 000000")
Copy the code

3. Feedback

If you have any questions or good suggestions, please send them to: Issue, [email protected] or [email protected]

About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.