Aligned is a tiny library for Android that makes putting text on baseline infinitely easier.
At the moment it does only one thing (and does it freaking rad!) — provides a
TextView implementation that allows setting arbitrary leading and some other metrics on the text, helping you to place it nicely on the Material 8dp grid, pixel-perfect, cross-device¹, effortless!
How to use
First, add a dependency in your
build.gradle, given that you have
jcenter() in your repositories:
For Maven and alternative configurations check Bintray page.
Now you can use
com.actinarium.aligned.TextView in your layouts. It’s just like
TextView, but offers three additional properties:
app:leading— forces the lines to have specified leading. You probably want to take the values from the spec;
app:firstLineLeading— allows to set special leading for the first line, otherwise
app:leadingwill be used. You may want to use this instead of top margins.
app:lastLineDescent— specifies the distance from the baseline of the last line to the bottom of the view. Set this to a multiple of 4dp so that the bottom bound of the view also lands on the grid, and the next view is not misaligned.
<com.actinarium.aligned.TextView xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/label" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="@style/TextAppearance.AppCompat.Body1" android:textSize="14sp" app:leading="16sp" app:firstLineLeading="24sp" app:lastLineDescent="8sp" />
com.actinarium.aligned.TextView is a thin direct subclass of
android.widget.TextView and both have the same short name, to avoid using full-qualified names you can just cast the view to
TextView in your Java code unless you need the getters/setters for leading and descent properties:
import android.widget.TextView; // ... TextView myAlignedLabel = (TextView) findViewById(R.id.label); myAlignedLabel.setText(...);
Alternatively, you can use
com.actinarium.aligned.Utils.setExactMetrics() method to programmatically adjust these properties of existing
TextViews in your layouts.
The library is licensed under Apache 2.0 License, meaning that you can freely use it in any of your projects.
The full license text is here.
A personal appeal
If you already feel that this library is a godsend that will save you lots of time, please consider adding me on Google+ and/or Twitter. I am about to publish a free no-ads aesthetically awesome productivity app soon, and it will make me super-happy if you are one of the first to hear and try it out.
Also feel free to check Rhythm, my other typesetting adoration lib (psst, it’s used in the sample here to draw grids and bounds).
¹ — In case you find it to be not really cross-device, please open an issue.