You must be developing app for that Android device in your pocket but wait, what happens when you try to run the same code on tablet or an Android mobile phone of different size or say an Android TV? Boom! The dimensions messes up. 😫
So, here is the solution to your problem! 😇
This is a very simple library which works with your sp and dp values for almost all Android devices. All you need to do is install this library into your project and call it with your sp (Example : 10sp) or dp (Example : 10dp). Once you specify this according to this libary, the layouts will automatically adjust itself according to the devices! 😮
Don't believe me? Here is the screenshots on Android TV, Nexus 5x and Nexus One 😌
- Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://www.jitpack.io' }
}
}
- Step 2. Add the dependency
dependencies {
compile 'com.github.adhishlal:asdp:1.1'
}
And it's done. Just give the dimensions as @dimen/_<your value>asdp
and it will take the sizes automatically!
<TextView
android:id="@+id/tvExample"
android:layout_width="@dimen/_50asdp"
android:layout_centerInParent="true"
android:layout_margin="@dimen/_5asdp"
android:layout_height="@dimen/_50asdp"
android:layout_gravity="center" />