https://github.com/drewhamilton/inlinedimens
Android dimension types as inline classes with easy conversions.
https://github.com/drewhamilton/inlinedimens
android conversion dimensions
Last synced: about 1 year ago
JSON representation
Android dimension types as inline classes with easy conversions.
- Host: GitHub
- URL: https://github.com/drewhamilton/inlinedimens
- Owner: drewhamilton
- License: apache-2.0
- Created: 2019-06-24T18:28:17.000Z (about 7 years ago)
- Default Branch: main
- Last Pushed: 2021-06-09T18:25:17.000Z (about 5 years ago)
- Last Synced: 2025-03-27T02:43:13.067Z (over 1 year ago)
- Topics: android, conversion, dimensions
- Language: Kotlin
- Homepage:
- Size: 534 KB
- Stars: 55
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Inline Dimens
[](https://github.com/drewhamilton/InlineDimens/actions?query=workflow%3ACI+branch%3Amain)
Never confuse px, dp, and sp at runtime again. Inline Dimens offers a separate inline class for each
dimen type, ensuring compile-time unit confidence without any runtime cost. Extension functions
provide the ability to interact with the Android framework directly with these unit-safe types, and
to convert between them easily.
## Download
[](https://maven-badges.herokuapp.com/maven-central/dev.drewhamilton.inlinedimens/inlinedimens)
Inline Dimens is available on Maven Central. It is experimental, and the API may undergo breaking
changes before version 1.0.0.
To use Inline Dimens, include the following in your Gradle dependencies:
```groovy
implementation "dev.drewhamilton.inlinedimens:inlinedimens:$version"
```
## Usage
Ensure inline classes are enabled in your build.gradle file:
```groovy
android {
// ...
kotlinOptions {
// ...
freeCompilerArgs += ["-XXLanguage:+InlineClasses"]
}
}
```
Then, work directly with unit-safe types in your code. For example, get a `TextView`'s text size in
the unit of your choice:
```kotlin
val scaledTextSize = textView.textSizeSp
```
And convert between different units at will:
```kotlin
val textSizePx = scaledTextSize.toPx()
```
**Note:** Inline classes don't play nicely with Java source code, so this library is unusable in
Java source.
## License
```
Copyright 2019 Drew Hamilton
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.
```