I am applying my own background on edittext to achieve same solid color of underline without changing it's thickness on focus.
Next to my xml file I can see in brackets it's specified as xhdpi (see image attached)
When i run it on different density device the underline doesn't look perfect.
Is it because the xml file is specified as xhdpi? What does it mean? How to make it scale perfectly on all densities?
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:bottom="1dp"
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape android:shape="rectangle" >
<stroke
android:width="1px"
android:color="#color/purple" />
<solid android:color="#00FFFFFF" />
<padding
android:bottom="5dp"
android:top="5dp" />
</shape>
</item>
</layer-list>
Please read Android Developers: Support different pixel densities
1. Use one resource for all densities
Select the file and press F6 (move file)
Then delete -xhdpi from the To directory field and click on OK
2. Create resource for each screen density
You will need to create other XML files with that same name and place them in the appropriate folder while modifying android:width and maybe padding to suit your taste
I did my splash screen with this tutorial and it works great:
https://www.bignerdranch.com/blog/splash-screens-the-right-way/
Basically I set up a splascreen through theme:
<style name="ThemeSplash" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/drawable_splashcreen</item>
</style>
I wanted to put a vector image inside like this: (drawable_splashcreen)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/color_background_splash_screen" />
<item android:drawable="#drawable/vector_najdiflet_logo" />
</layer-list>
The image will streched through the full screen. On API 23 it works like it should have. But on older devices it just streches. I tried width, height and even messed up with viewports but no success.
I stumbled upon the same problem.
Unfortunately there does not seem to be a possibility to make the splash screen work with just a vector drawable for pre API 23.
The problem is you can't load VectorDrawableCompat outside of the process, like in this case in your themes android:windowBackground. So what is likely happening here is, that on API 21 the Vector get's converted to a PNG to be compatible. So in the <layered-list>the converted PNG is inserted into the <item> element, which causes the bitmap to stretch to all edges, because it's missing the <bitmap> element.
So my solution is the following: Create a drawable_splashscreen.xml inside the folder drawables-v23 which looks like the following for the vector drawable.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<item android:drawable="?attr/colorPrimary"/>
<item android:drawable="#drawable/ic_splashscreen" android:gravity="center"/>
</layer-list>
Then create another drawable_splashscreen.xml but inside the regular drawables folder:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<item android:drawable="?attr/colorPrimary"/>
<item>
<bitmap android:src="#drawable/ic_splashscreen" android:gravity="center"/>
</item>
</layer-list>
Notice the <bitmap> element. So now, when the PNG is used on pre API 23 devices it will be displayed properly and won't be stretched to the whole background.
Unfortunately you also have to provide splash screen as PNG for this to work in the old APIs.
But for every device with API 23+ the vector drawable will be used.
For full screen splash try to use:
android:gravity="fill_horizontal|fill_vertical"
If not resolve maybe the solution is create separated images
for each resolution size.
Most common resolutions:
Small = 240 x 320px (ldpi)
Medium = 320 x 480px (mdpi)
Large = 480 x 800px (hdpi)
xLarge = 640 x 960px (xhdpi)
Portrait Format:
ldpi = 240 x 360px (0.75 x mdpi)
mdpi = 320 x 480px (base density)
hdpi = 480 x 720px (1.5 x mdpi)
xhdpi = 640 x 960px (2 x mdpi)
xxhdpi = 960 x 1440px (3 x mdpi)
xxxhdpi = 1080 x 1920px (4 x mdpi)
Landscape Format (inverted portrait format):
ldpi = 360 x 240px (0.75 x mdpi)
mdpi = 480 x 320px (base density)
hdpi = 720 x 480px (1.5 x mdpi)
xhdpi = 960 x 640px (2 x mdpi)
xxhdpi = 1440 x 960px (3 x mdpi)
xxxhdpi = 1920 x 1080px (4 x mdpi)
More about you can find here:
https://design.google.com/devices/
Android splash screen image sizes to fit all devices
http://vinsol.com/blog/2014/11/20/tips-for-designers-from-a-developer/
Edited solution that will make your SplashScreen look great on all APIs including API21 to API23
First of all read this article and follow the GOOD way of making a splash screen.
If your logo is distorted or wont fit and you are only targeting APIs24+ you can simply scale down your vector drawable directly in its xml file like so:
<vector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:aapt="http://schemas.android.com/aapt"
android:viewportWidth="640"
android:viewportHeight="640"
android:width="240dp"
android:height="240dp">
<path
android:pathData="M320.96 55.9L477.14 345L161.67 345L320.96 55.9Z"
android:strokeColor="#292929"
android:strokeWidth="24" />
</vector>
in the code above I am rescaling a drawable I drew on a 640x640 canvas to be 240x240. then i just put it in my splash screen drawable like so and it works great:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque"
android:paddingBottom="20dp" android:paddingRight="20dp" android:paddingLeft="20dp" android:paddingTop="20dp">
<!-- The background color, preferably the same as your normal theme -->
<item>
<shape>
<size android:height="120dp" android:width="120dp"/>
<solid android:color="#android:color/white"/>
</shape>
</item>
<!-- Your product logo - 144dp color version of your app icon -->
<item
android:drawable="#drawable/logo_vect"
android:gravity="center">
</item>
</layer-list>
my code is actually only drawing the triangle in the picture at the bottom but here you see what you can achieve with this. Resolution is finally great as opposed to the pixelated edges I was getting when using bitmap. so use a vector drawable by all means (there is a site called vectr that I used to create mine without the hasle of downloading specialized software).
EDIT in order to make it work also on API21-22-23
While the solution above works for devices runing API24+ I got really disappointed after installing my app a device running API22. I noticed that the splashscreen was again trying to fill the entire view and looking like shit. After tearing my eyebrows out for half a day I finally brute-forced a solution by sheer willpower.
you need to create a second file named exactly like the splashscreen xml (lets say splash_screen.xml) and place it into 2 folders called drawable-v22 and drawable-v21 that you will create in the res/ folder (in order to see them you have to change your project view from Android to Project). This serves to tell your phone to redirect to files placed in those folders whenever the relevant device runs an API corresponding to the -vXX suffix in the drawable folder, see this link. place the following code in the Layer-list of the splash_screen.xml file that you create in these folders:
<item>
<shape>
<size android:height="120dp" android:width="120dp"/>
<solid android:color="#android:color/white"/>
</shape>
</item>
<!-- Your product logo - 144dp color version of your app icon -->
<item android:gravity="center">
<bitmap android:gravity="center"
android:src="logo_vect"/>
</item>
For some reason for these APIs you have to wrap your drawable in a bitmap in order to make it work and jet the final result looks the same. The issue is that you have to use the aproach with the aditional drawable folders as the second version of the splash_screen.xml file will lead to your splash screen not being shown at all on devices running APIs higher than 23. You might also have to place the first version of the splash_screen.xml into drawable-v24 as android defaults to the closest drawable-vXX folder it can find for resources.
I do not think this is possible to do with vectors, for devices < API 23, since it is not possible to set the attributes android:height and android:width on the drawable.
To implement my splash screen with a centered icon, I had to export the vector of my logo to .png's for each screen size, and embed a bitmap within the layer-list:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<item android:drawable="#drawable/splash_screen_background" />
<item
android:left="20dp"
android:right="20dp">
<bitmap
android:gravity="center"
android:scaleType="centerInside"
android:src="#drawable/logo_rasterised" />
</item>
</layer-list>
Ideally I would not like to have any bitmap images at all in my resources, but at least the splash screen is the only place where I have had to use bitmaps.
Use bitmap and define you image in the src. Set gravity to center
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<item android:drawable="#color/color_background_splash_screen"/>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/ic_logo_splash"/>
</item>
</layer-list>
The simplest method I found to solve this is by using a vector asset.
This will ensure that the apk size is not big too. If you're not doing this or don't have an svg file for your logo, you should manually change the DP intensities of pictures and add them manually. You may also use plugins that do that automatically for you.
In case you go with the vector file and still end up getting a zoomed image, add in android:top, left right and bottom to get your logo size right.
Dont put your image in window:Background, instead add as android:src for an imageview for the layout of the SplashScreenActivity.
Android studio 2.0 Preview 3b
Hello,
I have created the following layout that I want to use for a background for my app. I am using the layer-list and I want to display a bowl of peas in 2 locations. Everything looks ok in the preview, but when I run on genymotion or some cheap Chinese devices the image stretches across the screen. However, on the Android AVD, everything looks fine and on my Nexus 5 (real device) everything works ok.
This is what I want and this is how it's displayed in the AVD and Nexus 5. As you can see there is no problem.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:centerX="0.5"
android:centerY="0.3"
android:endColor="#08e25b"
android:gradientRadius="300dp"
android:startColor="#b7e9c9"
android:type="radial" />
</shape>
</item>
<item
android:width="48dp"
android:height="48dp"
android:left="350dp"
android:top="400dp">
<bitmap android:src="#drawable/peas" />
</item>
<item
android:width="68dp"
android:height="68dp"
android:drawable="#drawable/peas"
android:left="-20dp"
android:top="480dp" />
</layer-list>
I have placed peas.png file in drawable-nodpi and just add the width and height in the layer-list
And when I run on the genymotion and some cheap smart devices I get the following:
Just quick summary.
Nexus 5 real device and AVD devices works ok
Genymotion and cheap smart devices doesn't display correctly
I am just confused in what I should believe. I have tried to use the bitmap as well to see if that would make any difference.
Many thanks for any suggestions.
Update your layer-list as follows
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:centerX="0.5"
android:centerY="0.1"
android:endColor="#08e25b"
android:gradientRadius="300dp"
android:startColor="#b7e9c9"
android:type="radial" />
</shape>
</item>
<item
android:width="48dp"
android:height="48dp"
android:bottom="68dp"
android:right="-20dp">
<bitmap
android:gravity="bottom|right"
android:src="#drawable/peas" />
</item>
<item
android:width="68dp"
android:height="68dp"
android:bottom="-20dp"
android:left="-20dp">
<bitmap
android:gravity="bottom|left"
android:src="#drawable/peas" />
</item>
</layer-list>
Place your images in all density folders (xxhdpi, xhdpi, hdpi).
The system picks image resources based on screen resolution.
I read some document, it seems that that mipmap replace drawable when minimum API is 21.
Can I delete the folder drawable ?
There are only four folder mipmap-hdpi,mipmap-mdpi,mipmap-xhdpi and mipmap-xxhdpi when I generate a new project with Android Studio V1.3, do I need add a folder mipmap for store normal picture and other resources?
BTW, there is a file border_ui.xml in the folder drawable in my a project, can I remove the file from the folder drawable to mipmap?
border_ui.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="0.8dp" android:color="#000000" />
<solid android:color="#ffffff" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
</shape>
drawable/
For bitmap files (PNG, JPEG, or GIF), 9-Patch image files, and XML files that describe Drawable shapes or Drawable objects that contain multiple states (normal, pressed, or focused). See the Drawable resource type.
mipmap/
For app launcher icons. The Android system retains the resources in this folder (and density-specific folders such as mipmap-xxxhdpi) regardless of the screen resolution of the device where your app is installed. This behavior allows launcher apps to pick the best resolution icon for your app to display on the home screen. For more information about using the mipmap folders, see Managing Launcher Icons as mipmap Resources.
See details by click here
This is my Gradient drawable xml file and expected output for nexus 7 but the gradient is not correct ( Linings between shades )!
It look like this
Correct Output is in phone.
Is there any pixel format definition?
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="45"
android:endColor="#00a484"
android:startColor="#00407e"
android:type="linear" />
</shape>