Convert Figma logo to code with AI

niniloveyou logoStateButton

一个可以用代码设置selector背景(按下去背景颜色更改,样式变化等等)的button, 再也不用写selector了

1,302
224
1,302
3

Top Related Projects

PHP routing class. Lightweight yet flexible. Supports REST, dynamic and reversed routing.

1,080

A lightweight and simple object oriented PHP Router

Fast request router for PHP

A fast & flexible router

Quick Overview

StateButton is an Android library that provides a customizable button with multiple states. It allows developers to easily create buttons that can change their appearance based on different states such as normal, pressed, or disabled, without the need for complex XML drawables or multiple image resources.

Pros

  • Simplifies the process of creating multi-state buttons in Android
  • Reduces the need for multiple drawable resources, potentially decreasing app size
  • Offers a high degree of customization for button appearance
  • Supports both text and icon buttons

Cons

  • Limited documentation and examples available
  • Not actively maintained (last update was several years ago)
  • May not be compatible with the latest Android development practices or libraries
  • Lacks support for some modern Android features like Material Design 3

Code Examples

  1. Creating a basic StateButton:
<com.niniloveyou.statebutton.StateButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me"
    app:normalBackgroundColor="#3498db"
    app:pressedBackgroundColor="#2980b9"
    app:disabledBackgroundColor="#bdc3c7"
    app:radius="5dp"/>
  1. Setting up a StateButton with an icon:
<com.niniloveyou.statebutton.StateButton
    android:layout_width="48dp"
    android:layout_height="48dp"
    app:normalIcon="@drawable/ic_normal"
    app:pressedIcon="@drawable/ic_pressed"
    app:disabledIcon="@drawable/ic_disabled"
    app:radius="24dp"/>
  1. Programmatically changing StateButton properties:
val stateButton: StateButton = findViewById(R.id.stateButton)
stateButton.setNormalBackgroundColor(Color.RED)
stateButton.setPressedBackgroundColor(Color.DARK_RED)
stateButton.setDisabledBackgroundColor(Color.GRAY)
stateButton.setRadius(10f)

Getting Started

  1. Add the JitPack repository to your project's build.gradle file:
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
  1. Add the dependency to your app's build.gradle file:
dependencies {
    implementation 'com.github.niniloveyou:StateButton:1.0.0'
}
  1. Use the StateButton in your XML layout or create it programmatically in your Activity or Fragment.

Competitor Comparisons

PHP routing class. Lightweight yet flexible. Supports REST, dynamic and reversed routing.

Pros of AltoRouter

  • More comprehensive routing solution for PHP applications
  • Supports RESTful routing and dynamic route patterns
  • Well-documented with extensive examples and use cases

Cons of AltoRouter

  • Focused solely on routing, lacks UI component functionality
  • May be overkill for simple projects or static websites
  • Requires more setup and configuration compared to StateButton

Code Comparison

StateButton (Android XML):

<com.github.niniloveyou.StateButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:normalTextColor="#ffffff"
    app:pressedTextColor="#000000"
    app:text="Button" />

AltoRouter (PHP):

$router = new AltoRouter();
$router->map('GET', '/user/[i:id]', function($id) {
    // Handle user profile
});
$match = $router->match();

Summary

AltoRouter is a PHP routing library offering flexible URL mapping and handling, while StateButton is an Android UI component for creating customizable buttons with different states. AltoRouter excels in web application routing but lacks UI features, whereas StateButton focuses on enhancing Android button functionality without addressing routing concerns.

1,080

A lightweight and simple object oriented PHP Router

Pros of router

  • More versatile: Handles routing for web applications, not limited to button states
  • Active development: Last updated more recently, with ongoing contributions
  • Larger community: More stars, forks, and contributors on GitHub

Cons of router

  • Higher complexity: Requires more setup and configuration
  • Less focused: Not specialized for a single UI component like StateButton
  • Steeper learning curve: May take longer to implement for simple use cases

Code Comparison

StateButton usage:

StateButton stateButton = new StateButton(this);
stateButton.setStateTextColor(Color.WHITE);
stateButton.setStateBackgroundColor(Color.BLUE);
stateButton.setStateText("Normal", "Loading", "Complete");

router usage:

$router = new \Bramus\Router\Router();
$router->get('/hello/(\w+)', function($name) {
    echo 'Hello ' . htmlentities($name);
});
$router->run();

Summary

While StateButton focuses on providing an easy-to-use button component with multiple states, router is a more comprehensive routing solution for web applications. StateButton is simpler to implement for specific UI needs, while router offers greater flexibility for handling various routes and requests in a web application. The choice between the two depends on the specific requirements of your project.

Fast request router for PHP

Pros of FastRoute

  • Highly performant PHP routing library optimized for speed
  • Supports advanced routing patterns and parameter matching
  • Well-documented with extensive usage examples

Cons of FastRoute

  • Limited to routing functionality, not a full-featured UI component
  • Requires PHP knowledge and integration with a web application framework
  • Less suitable for mobile or desktop application development

Code Comparison

FastRoute:

$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
    $r->addRoute('GET', '/users', 'get_all_users');
    $r->addRoute('GET', '/user/{id:\d+}', 'get_user_details');
});

StateButton:

StateButton stateButton = new StateButton(this);
stateButton.setNormalText("Normal");
stateButton.setPressedText("Pressed");
stateButton.setUnableText("Unable");

Summary

FastRoute is a high-performance PHP routing library, while StateButton is an Android UI component for creating customizable buttons with different states. FastRoute excels in web application routing, offering advanced features and optimizations. StateButton, on the other hand, provides an easy-to-use solution for creating interactive buttons in Android apps. The choice between these libraries depends on the specific project requirements and the target platform.

A fast & flexible router

Pros of klein.php

  • More comprehensive PHP routing and micro-framework solution
  • Actively maintained with regular updates and contributions
  • Extensive documentation and community support

Cons of klein.php

  • Larger codebase and potentially steeper learning curve
  • May be overkill for simple projects or single-page applications
  • Not focused on UI components or state management

Code Comparison

StateButton (Java):

public class StateButton extends Button {
    private int mState;
    private StateListDrawable mNormalDrawable;
    private StateListDrawable mPressedDrawable;
    private StateListDrawable mUnableDrawable;
    // ... (constructor and methods)
}

klein.php (PHP):

$klein = new \Klein\Klein();

$klein->respond('GET', '/users', function () {
    // Handle GET request to /users
});

$klein->dispatch();

Summary

StateButton is a Java library for creating customizable buttons with different states, while klein.php is a PHP routing and micro-framework. They serve different purposes and are not directly comparable. StateButton is more suitable for Android UI development, whereas klein.php is ideal for building PHP web applications with routing capabilities.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

StateButton

###中文版这里: https://github.com/niniloveyou/StateButton/blob/master/README_CHINESE.md

Do you hate to write a background for each button?

Do you hate to write a bunch of selector?

That's why I'm writing this view.

Here to download demo

https://github.com/niniloveyou/StateButton/blob/master/demo.apk

Screenshot

Download

   allprojects {
       repositories {
           ......
           maven { url 'https://jitpack.io' }
       }
   }
   implementation 'com.github.niniloveyou:StateButton:v1.0.0'
   

Customizable attributes

Attributedefault valuexmljava
normalTextColororiginal text colornormalTextColorsetNormalTextColor(int color)
pressedTextColororiginal text colorpressedTextColorsetPressedTextColor(int color)
unableTextColororiginal text colorunableTextColorsetUnableTextColor(int color)
strokeDashWidth0strokeDashWidthsetStrokeDash(int dashWidth, int dashGap)
strokeDashGap0strokeDashGapsetStrokeDash(int dashWidth, int dashGap)
normalStrokeWidth0normalStrokeWidthsetNormalStrokeWidth(int widht)
pressedStrokeWidth0pressedStrokeWidthsetPressedStrokeWidth(int widht)
unableStrokeWidth0unableStrokeWidthsetUnableStrokeWidth(int widht)
normalStrokeColor0normalStrokeColorsetNormalStrokeColor(int color)
pressedStrokeColor0pressedStrokeColorsetPressedStrokeColor(int color)
unableStrokeColor0unableStrokeColorsetUnableStrokeColor(int color)
normalBackgroundColor0normalBackgroundColorsetNormalBackgroundColor(int color)
pressedBackgroundColor0pressedBackgroundColorsetPressedBackgroundColor(int color)
unableBackgroundColor0unableBackgroundColorsetUnableBackgroundColor(int color)
radius0radiussetRadius(int radius) / setRadius(float[] radii)
roundfalseroundsetRound(boolean round)
animationDuration0msanimationDurationsetAnimationDuration(int duration)

Usage

If the default values of custom attribues did not meet your requirement, you can easily re-config that attributes. This is sample code that you can refer. you can also browse demo app for more details.

via xml (sample)

  • Define xmlns:app="http://schemas.android.com/apk/res-auto" on root of your xml file
<deadline.statebutton.StateButton
            android:id="@+id/stateButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="StateButton"
            
            app:radius="5dp"
            app:round="true"
            app:animationDuration="200"
            
            app:strokeDashGap="2dp"
            app:strokeDashWidth="5dp"
            app:normalStrokeWidth="2dp"
            app:pressedStrokeWidth="2dp"
            app:unableStrokeWidth="2dp"
            
            app:normalStrokeColor="@android:color/white"
            app:pressedStrokeColor="@android:color/white"
            app:unableStrokeColor="@android:color/white"
            
            app:normalTextColor="@android:color/white"
            app:pressedTextColor="@android:color/white"
            app:unableTextColor="@android:color/white"
            
            app:normalBackgroundColor="@color/colorPrimaryDark"
            app:pressedBackgroundColor="@color/colorPrimaryDark"
            app:unableBackgroundColor="@color/colorPrimaryDark"/>

Developed By

deadline

Blog : http://www.jianshu.com/users/25e80ace21b8/latest_articles

MIT License

  Copyright (c) 2016 deadline

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.