DSL for constructing the drawables in Kotlin instead of in XML

Overview

Android Drawable Kotlin DSL CircleCI

DSL for constructing the drawables in Kotlin instead of in XML

Examples

Shape drawables

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#199fff" />
    <stroke
        android:width="2dp"
        android:color="#444444" />
</shape>

replace it with

shapeDrawable {
  shape = GradientDrawable.OVAL

  solidColor = Color.parseColor("#199fff")

  stroke {
    width = dip(2)
    color = Color.parseColor("#444444")
  }
}

State selectors

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true">
        <shape>
            <solid android:color="@android:color/black" />
        </shape>
    </item>

    <item android:state_pressed="true">
        <shape>
            <solid android:color="@android:color/holo_red_dark" />
        </shape>
    </item>

    <item>
        <shape>
            <solid android:color="@android:color/white" />
        </shape>
    </item>
</selector>

replace it with

stateListDrawable {

  checkedState {
    shapeDrawable {
      solidColor = Color.BLACK
    }
  }

  pressedState {
    shapeDrawable {
      solidColor = Color.RED
    }
  }

  defaultState {
    shapeDrawable {
      solidColor = Color.WHITE
    }
  }
}

Layer drawables

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="oval">
            <stroke
                android:width="5dp"
                android:color="#000000" />
        </shape>
    </item>

    <item>
        <shape android:shape="oval">
            <stroke
                android:width="2dp"
                android:color="#ffffff" />
        </shape>
    </item>

</layer-list>

replace it with

layerDrawable(

    shapeDrawable {
      shape = GradientDrawable.OVAL

      stroke {
        width = ctx.dip(5)
        color = Color.parseColor("#000000")
      }
    },

    shapeDrawable {
      shape = GradientDrawable.OVAL

      stroke {
        width = ctx.dip(2)
        color = Color.parseColor("#ffffff")
      }
    }
)

Install

repositories {
    maven { url 'https://jitpack.io' }
}

compile "com.github.infotech-group:android-drawable-dsl:0.3.0"

Contribute

We haven't covered 100% of the XML DSL, contributions are very welcome

Please write a test for every new tag you add, we (hopefully) made it easy to do

You might also like...
Regular expression DSL on Kotlin

Examples Characters Construct Equivalent Matches x character(Char) The character x \\ character('\\') The backslash character \0n octal(OctalValue(7))

Kotlin Object Notation - Lightweight DSL to build fluid JSON trees

Kotlin Object Notation Lightweight kotlin MPP DSL for building JSON trees Setup Just drop the dependency in your commonMain sourceSet kotlin { sourc

Kotlin parser library with an easy-to-use DSL
Kotlin parser library with an easy-to-use DSL

Pratt Library for parsing expressions and a beautiful Kotlin DSL Just define your operators and operands with the Kotlin DSL and the parser is ready!

Kotlin DSL for Junit5

Kupiter is Kotlin DSL for Junit5. Current API is only for dynamic tests. Get it repositories { maven { url 'https://jitpack.io' } } dependencies

Kotlin-dsl-sample - Preferences project on android

kotlin-dsl-example Sample preferences project on android. How to use val

GitHub Actions Kotlin DSL

GitHub Actions Kotlin DSL Work in progress! The goal is to be able to describe GH Actions in Kotlin with all its perks, like: workflow( name = "Te

A simple, classic Kotlin MVI implementation based on coroutines with Android support, clean DSL and easy to understand logic

A simple, classic Kotlin MVI implementation based on coroutines with Android support, clean DSL and easy to understand logic

Kotlin DSL inspired by bhailang.js

Kotlin DSL inspired by bhailang.js

A light weight Compose Animation library to choreograph low level Animation API through Kotlin DSL.

Koreography Choreograph your Compose Animation 💃 🕺 A lightweight Compose Animation utility library to choreograph low-level Animation API (https://d

Comments
  • Add Possibility to Provide Mask for Ripple

    Add Possibility to Provide Mask for Ripple

    Hi, Currently the content and the mask Arguments use the same Drawable when creating a RippleDrawable. There should be the possibility to provide a separate Drawable for the Mask.

    enhancement 
    opened by OFmarting 2
  • Enable specifying radius per corner, simplified some statements

    Enable specifying radius per corner, simplified some statements

    Specifying the per-corner radiuses like in xml example is really verbose and tricky, with all of this calls to setCornerRadii(floatArray!), and leads to duplicating code. But it is history now ;)

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <corners
            android:radius="10px"
            android:topLeftRadius="15px"
            android:bottomRightRadius="5px" />
        <solid android:color="#666666" />
    </shape>
    

    Now corresponds to

    shapeDrawable {
          shape = GradientDrawable.RECTANGLE
          corners (10f) {
            topLeft = 15f
            bottomRight = 5f
          }
          solidColor = Color.parseColor("#666666")
        }
    
    opened by Mishkun 2
  • try caching

    try caching

    before_cache:
      - rm -f  $HOME/.gradle/caches/modules-2/modules-2.lock
      - rm -fr $HOME/.gradle/caches/*/plugin-resolution/
    cache:
      directories:
        - $HOME/.gradle/caches/
        - $HOME/.gradle/wrapper/
        - $HOME/.android/build-cache
    
    opened by meoyawn 1
  • Add best practices

    Add best practices

    Hello. Nice DSL!

    It would be better to add some best practices: — Where one should keep drawables? At a use-site (e. g. Anko UI declaration), in extension functions, in application class? — How and when one should reuse these drawables? (e. g., stateful drawables shouldn't be reused, or you may click one item and see ripple on the another one ;)

    P. S. To avoid boxing, e. g., Float? in rectangleShape, you can use Float.NaN instead of null.

    P. P. S. GradientDrawable.solidColor's getter should be @Deprecated(level = ERROR).

    P. P. P. S. 80 methods

    Thanks for your work!

    opened by Miha-x64 4
Releases(0.3.0)
Owner
Infotech Group
Infotech Group
This program will read from your android application string.xml file and generate translated strings.xml files in your preferred languages using google sheet.

Localize your application content This program will read from your application string.xml file and generate translated strings.xml files in your prefe

DhiWise 4 Jul 29, 2022
Kotlin-client-dsl - A kotlin-based dsl project for a (Client) -> (Plugin) styled program

kotlin-client-dsl a kotlin-based dsl project for a (Client) -> (Plugin) styled p

jackson 3 Dec 10, 2022
Android + Kotlin + Github Actions + ktlint + Detekt + Gradle Kotlin DSL + buildSrc = ❤️

kotlin-android-template ?? A simple Github template that lets you create an Android/Kotlin project and be up and running in a few seconds. This templa

Nicola Corti 1.5k Jan 3, 2023
A Kotlin DSL wrapper around the mikepenz/MaterialDrawer library.

MaterialDrawerKt Create navigation drawers in your Activities and Fragments without having to write any XML, in pure Kotlin code, with access to all t

Márton Braun 517 Nov 19, 2022
{ } Declarative Kotlin DSL for choreographing Android transitions

Transition X Kotlin DSL for choreographing Android Transitions TransitionManager makes it easy to animate simple changes to layout without needing to

Arunkumar 520 Dec 16, 2022
Kotlin Dsl for Android RecyclerView

KRecyclerDsl Kotlin Dsl for Android RecyclerView Exemple Sample project recyclerView.adapter = dataClassAdapter<MyView, MyDataClass>(R.layout.my_view,

Thomas Girard 14 Mar 31, 2019
The most complete and powerful data-binding library and persistence infra for Kotlin 1.3, Android & Splitties Views DSL, JavaFX & TornadoFX, JSON, JDBC & SQLite, SharedPreferences.

Lychee (ex. reactive-properties) Lychee is a library to rule all the data. ToC Approach to declaring data Properties Other data-binding libraries Prop

Mike 112 Dec 9, 2022
Nice and simple DSL for Espresso in Kotlin

Kakao Nice and simple DSL for Espresso in Kotlin Introduction At Agoda, we have more than 1000 automated tests to ensure our application's quality and

Agoda Company Pte Ltd. 1.1k Nov 22, 2022
Lightweight Kotlin DSL dependency injection library

Warehouse DSL Warehouse is a lightweight Kotlin DSL dependency injection library this library has an extremely faster learning curve and more human fr

Osama Raddad 18 Jul 17, 2022
Code generation of Kotlin DSL for AWS CDK

Code generation of Kotlin DSL for AWS CDK

Semantic Configuration 5 Dec 24, 2022