[] Define and render UI specs on top of your Android UI

Related tags

Utility dspec
Overview

dspec

A simple way to define and render UI specs on top of your Android UI.

Usage

Enclose the target UI with a DesignSpecFrameLayout, usually the root of your view hierarchy. Get the associated DesignSpec instance with getDesignSpec and then you can:

  • Toggle baseline grid visibility with setBaselineGridVisible(boolean).
  • Change baseline grid cell width with setBaselineGridCellSize(int).
  • Change baseline grid color with setBaselineGridColor(int).
  • Toggle keylines visibility with setKeylinesVisible(boolean).
  • Change keylines color with setKeylinesColor(int).
  • Add keylines with addKeyline(int, From).
  • Toggle spacing markers visibility with setSpacingsVisible(boolean).
  • Change spacing markers color with setSpacingsColor(int).
  • Add spacing marker with addSpacing(int, int, From).

You can also specify the design spec in a raw JSON resource like:

{
    "baselineGridVisible": true,
    "baselineGridCellSize": 8,
    "keylines": [
        { "offset": 16,
          "from": "LEFT" },
        { "offset": 72,
          "from": "LEFT" },
        { "offset": 16,
          "from": "RIGHT" }
    ],
    "spacings": [
        { "offset": 0,
          "size": 16,
          "from": "LEFT" },
        { "offset": 56,
          "size": 16,
          "from": "LEFT" },
        { "offset": 0,
          "size": 16,
          "from": "RIGHT" }
    ]
}

Because DesignSpec is a Drawable, you can simply add it to a ViewOverlay if you're running your app on API level >= 18:

DesignSpec designSpec = DesignSpec.fromResource(someView, R.raw.some_spec);
someView.getOverlay().add(designSpec);

Download

Download the latest JAR or grab via Gradle:

compile 'org.lucasr.dspec:dspec:0.1.1'

or Maven:

<dependency>
  <groupId>org.lucasr.dspec</groupId>
  <artifactId>dspec</artifactId>
  <version>0.1.1</version>
</dependency>

License

Copyright 2014 Lucas Rocha

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.
Comments
  • Add option to split the baseline grid.

    Add option to split the baseline grid.

    It's not uncommon for a design to call for bottom-anchored content, such as buttons, snackbars, drawers, etc. Such content should be grid-aligned internally, but its grid should originate at the bottom of the screen and may not align with the top-anchored grid.

    This change adds an option to split the grid at a given point on the screen. There will then be two grids, one originating at TOP and one at BOTTOM. If the grids do not align, a "gutter" will be left between them to indicate the odd space. The gutter will always be narrower than the baseline grid size.

    dspec_split_grid-2

    opened by loganj 5
  • Refactor DesignSpecFrameLayout to draw on top of content.

    Refactor DesignSpecFrameLayout to draw on top of content.

    The problem with using onDraw() in a ViewGroup is that this method actually draws _underneath_ the child view contents. This is an acceptable scenario for a basic layout with lots of transparency, but as soon as your view hierarchy starts including any significant amount of background images or colors, the grid becomes completely invisible.

    To properly draw _over_ the content in a ViewGroup, override dispatchDraw() and call your custom code after super. A nice side-effect of this is that you no longer has to disable the drawing optimization in ViewGroup either.

    opened by devunwired 3
  • DesignSpec as Drawable.

    DesignSpec as Drawable.

    What about Drawable adapter to design spec? I see following use case for it.

    public class DesignSpecDrawable extends Drawable {
        private final DesignSpec mDesignSpec;
    
        public DesignSpecDrawable(DesignSpec designSpec) {
            mDesignSpec = designSpec;
        }
    
        @Override
        public void draw(Canvas canvas) {
            if (mDesignSpec != null) {
                mDesignSpec.draw(canvas);
            }
        }
    
        /* ... */
    }
    
    setContentView(R.layout.main_activity);
    /* Resolve views here */
    
    View decorView = getWindow().getDecorView();
    mDesignSpec = DesignSpec.fromResource(decorView, R.raw.main_activity_spec);
    DesignSpecDrawable drawable = new DesignSpecDrawable(mDesignSpec);
    decorView.getOverlay().add(drawable); 
    

    The main advantage that is dspec now controlled from the code. It means we can now easily make it conditional, like:

    if (BuildConfig.USE_DSPEC) {
      // ...
    }
    

    So it can be stripped from production builds with ease.

    However, there is one problem ViewOverlays are only available since API 18 (JB 4.3). Although, we can still use it below this API level by setting it as background, or ImageView or so on, which is not so convient.

    opened by pepyakin 2
  • Support for mathematical calculations

    Support for mathematical calculations

    Adding support for basic calculations in the size and offset fields would improve readablilty:

    {
        "keylines": [
            {
                "offset": 56,
                "from": "TOP"
            }, {
                "offset": 56 + 24,
                "from": "TOP"
            }, {
                "offset": 56 + (2 * 24),
                "from": "TOP"
            }
        ]
    }
    
    opened by martynhaigh 0
  • Support for percentage widths

    Support for percentage widths

    It's be great to have percentage values supported in the offset and size fields.

    For example:

    {
    "keylines": [
        {
            "offset": 50%,
            "from": "LEFT"
        }, {
            "offset": 50%,
            "from": "TOP"
        },
    }
    
    opened by martynhaigh 0
Owner
Lucas Rocha
Mobile UI platform at Facebook. Formerly at Mozilla, GNOME, litl, and Nokia.
Lucas Rocha
A beautiful set of predefined colors and a set of color methods to make your Android development life easier.

Colours is a port of the Colours Library for iOS made by my good friend Ben Gordon. You can find that project here. Installation Maven Central Colours

Matthew York 634 Dec 28, 2022
Android library to easily serialize and cache your objects to disk using key/value pairs.

Deprecated This project is no longer maintained. No new issues or pull requests will be accepted. You can still use the source or fork the project to

Anup Cowkur 667 Dec 22, 2022
Small Android library to help you incorporate MVP, Passive View and Presentation Model patterns in your app

DroidMVP About DroidMVP is a small Android library to help you incorporate the MVP pattern along with Passive View and Presentation Model (yes, those

Andrzej Chmielewski 225 Nov 29, 2022
Utility for detecting and notifying when your Android app goes background / becomes foreground

Foredroid Utility for detecting and notifying when your Android app goes background / becomes foreground. API-level 14+. Usage: Initialise Foreground

Steve Liles 151 Nov 29, 2022
A library to quickly and easily enable multiple monitoring & support platforms for your mobile apps

You have a small team. Setting up crash reporting tools, event tracking tools, and log management services is not what you want to spend your hours do

Percolate 65 Aug 8, 2022
SharedPreference Library to save all types including your custom types and observe them if need be.

A SharedPreference Library that can be used to store all types including your custom classes and observe them too if needed.

Ehma Ugbogo 18 Nov 10, 2021
High level parsing to ensure your input is in the right shape and satisfies all constraints that business logic requires.

Parsix High level parsing to ensure your input is in the right shape and satisfies all constraints that business logic requires. It is highly inspired

null 190 Oct 16, 2022
Catch `dd`, `ddd`, `dump`, `sleep` and `ray` functions in your code

catch-debug-code Template ToDo list Create a new IntelliJ Platform Plugin Template project. Get familiar with the template documentation. Verify the p

Moath Alhajri 1 Apr 29, 2022
Android Shared preference wrapper than encrypts the values of Shared Preferences. It's not bullet proof security but rather a quick win for incrementally making your android app more secure.

Secure-preferences - Deprecated Please use EncryptedSharedPreferences from androidx.security in preferenced to secure-preference. (There are no active

Scott Alexander-Bown 1.5k Dec 24, 2022
Speed up your Android development

Caffeine Speed up your Android development! A collection of utility classes that help make Android development faster (and safer!) Examples No more un

Percolate 414 Nov 14, 2022
Secure Preference Manager for android. It uses various Encryption to protect your application's Shared Preferences.

Secure-Pref-Manager ##Secure Preference Manager is a simple Library to help you protect your Shared Preferences. Secure Preference Manager for android

Prashant Solanki 72 Nov 25, 2022
adds an option to the Android Sharesheet that allows you to save files to your device.

Save On Device adds an option to the Android Sharesheet that allows you to save files to your device. Download Get the app from the Google Play Store

null 24 Nov 29, 2022
TSBattery a new way to save your battery avoid cancer apps hacker it.

TSBattery TSBattery a new way to save your battery avoid cancer apps hacker it. TSBattery 是一个旨在使 QQ、TIM 变得更省电的开源 Xposed 模块 Get startted 此模块支持原生 Xposed

Fankesyooni 375 Jan 2, 2023
compaKTset is a small library aimed at providing you with the most memory efficient Set implementation for any particular data type of your choosing.

compaKTset is a small library aimed at providing you with the most memory efficient Set implementation for any particular data type of your choosing.

Ignat Beresnev 3 Nov 16, 2021
Tell you how to manage your blackhorse

Tell you how to manage your blackhorse

Andong LIAO 19 Nov 9, 2022
PhotoSync - app to backup photos to your own computer

PeopleInSpace Minimal Kotlin Multiplatform project with SwiftUI, Jetpack Compose

Kyle McBurnett 0 Jan 2, 2022
Simple Mobile Tools 172 Dec 26, 2022
gRPC and protocol buffers for Android, Kotlin, and Java.

Wire “A man got to have a code!” - Omar Little See the project website for documentation and APIs. As our teams and programs grow, the variety and vol

Square 3.9k Dec 31, 2022
General purpose utilities and hash functions for Android and Java (aka java-common)

Essentials Essentials are a collection of general-purpose classes we found useful in many occasions. Beats standard Java API performance, e.g. LongHas

Markus Junginger 1.4k Dec 29, 2022