I've created some 9-patch image files using Photoshop and the Draw9Patch tool, but as I tried to implement the images, it seems like a bust. I was following this tutorial (http://www.devahead.com/blog/2011/08/creating-a-custom-android-button-with-a-resizable-skin/). Any reason this may not work? I pasted the 4 images to the drawable-hdpi, and created a new values XML called "buttons.xml" and I placed the code in that XML file:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/default_disabled" android:state_enabled="false"/>
<item android:drawable="#drawable/default_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/default_focused" android:state_focused="true"/>
<item android:drawable="#drawable/default_normal"/>
</selector>
"default_(state)" are the names of the files (with .9.png extension). And I attempted to set the background of a button to android:background="#drawable/buttons"
Where did I go wrong?
Remember, different devices have different resolutions and densities.
In your case, you have put those drawables in drawable-hdpi, so you need device with HDPI density to work, otherwise you can put those drawables in drawable folder, so all devices (i.e. mdpi, hdpi, xdpi, etc) can use those drawables.
Read about multiple screen resolutions and densities here:
http://developer.android.com/guide/practices/screens_support.html
Related
Below is my drawable code. I want add image from mipmap in drawable item instead use of drawable.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/drawable_selected_indicator"
android:state_selected="true"/>
<item android:drawable="#drawable/drawable_default_indicator"/>
</selector>
You can use #mipmap/fileName in your XML resources or R.mipmap.fileName in java/kotlin.
You can use #mipmap/filename to use an image from mipmap folder.
<item android:drawable="#mipmap/drawable_selected_indicator"
android:state_selected="true"/>
According to this wiki-https://github.com/codepath/android_guides/wiki/Working-with-the-ImageView
"Starting with Android 4.3, there is now an option to use the res/mipmap folder to store "mipmap" images. Mipmaps are most commonly used for application icons such as the launcher icon. To learn more about the benefits of mipmaps be sure to check out the mipmapping for drawables post.
Mipmap image resources can then be accessed using the #mipmap/ic_launcher notation in place of #drawable. Placing icons in mipmap folders (rather than drawable) is considered a best practice because they can often be used at resolutions different from the device’s current density. For example, an xxxhdpi app icon might be used on the launcher for an xxhdpi device."
Is it possible to get correctly scaled item drawable in android layer-list item?
I prepared something like this:
bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/splash_bg" />
<item
android:gravity="center"
android:drawable="#drawable/logo_white">
</item>
</layer-list>
The sizes for drawable/logo_white vector has: 200dp width and 100dp height. It looks nice for small devices, but on tablet this logo is too small.
Is it possible to make it responsive for bigger screens?
Many thanks for help.
You can use attribute width and height in API level 23 and higher:
<item
android:width="#dimen/your_size" android:height="#dimen/your_size"
android:drawable="#drawable/logo_white"/>
And create different dimens files, for normal phones, tab, etc
Android has multiple folder system for multiple resolutions.
layout-hdpi,
layout-xhdpi, etc.
and same goes for drawable folder too. So by implementing same filename but edited xml based on dimension for different folder you can make in responsive. You might find other different technique for this.
I have a Radio Group. 5 Radio Buttons. The Radio Button's DrawableTop are set as, android:drawableTop="#drawable/Timeline_icon_drawable"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#mipmap/explore_icon_pressed" />
<item android:drawable="#mipmap/explore_icon" />
</selector>
How do I make the png smaller?
click on this link.. and download this 9 PATCH RESIZER..
9PATCH-RESIZER
It is specifically designed for developers of android. when you will drag any image on this software it will create every version of drawable images such as
xxxhdpi, xxhdpi, xhdpi, hdpi, mdpi, ldpi.
after customizing png or icon with this software you can use it comfortably in your app.
hope it helps!
first, sorry if my english is not correct, (i'm french) and I've not find the solution for my problem in french websites, so i try here !
In my app, I want to show 7 Buttons with graphisms I created with the GIMP.
So I use, in the XML code for my Buttons (here for my first button) :
android:background="#drawable/change_my_button1"
-change_my_button1 is an XML file :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false"
android:state_enabled="false"
android:drawable="#drawable/button-1" />
<item android:state_pressed="true" android:drawable="#drawable/button1-2" />
<item android:state_enabled="true"
android:drawable="#drawable/button1" />
If I press the button, its aspect change.
All of that works perfectly with my button1, button2 and button3.
But when I apply this method for my button4, the app crashs and i have a message "Unfortunatly, the application my_app has stoped"
I thought my images "button4" and "button4-2" are wrong but no ! When I delete the code line
android:background="#drawable/change_my_button1"
from the XML code of my first Button, then the image for my fourth Button works perfectly...
Maybe the res/drawable/ folder may contain only a restrict number of images ?
I've tried to put those two images in several res/drawable/ folders, but it's not working...
I hope you'll understand my problem...
Please help me ! That is a pain in the ass for me...
Thanks ! :)
Your resources are to big and you get OutOfMemoryError. You have to scale your resources for screen resolution which you support. I mean xxhdpi xhdpi hdpi etc. Probably all your resources are in drawable folder. It is wrong. They should be in drawable-xxhdpi, drawable-xhdpi, etc folders with proper size with proportions:
hdpi = mdpi * 1.5
xhdpi = mdpi * 2
xxhdpi = mdpi *3
I know in order to design for android devices you can make 2 images named the same thing with different resolutions and put them in the md and hd drawable folders. Android will call the correct version based on the devices screen density.
My question is this: If I have an xml file that defines a clickable image like so:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/listitemorange" />
<item android:state_pressed="true"
android:drawable="#drawable/orange_click" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/listitemorange" />
</selector>
I have the listitemorange and the orange_click images in both the md and hd folders, but do I need to have a copy of the xml file itself in both folders, or can I just keep in it in the md folder and it will still know to call the hd images when on an hd screen?
Please let me know if that doesn't make sense... thanks!
if you put the xml in the drawable folder, and the image in drawable-md&drawable-hd, everything will be okay.
but do I need to have a copy of the xml file itself in both folders, or can I just keep in it in the md folder and it will still know to call the hd images when on an hd screen?
As far as I understand it, you can just put one copy of that xml in the mdpi folder and Android will use it even when the device is hdpi. In other words, it will look for resources in the mdpi folder if it can't find them in the hdpi folder.
See... Supporting Multiple Screens
EDIT: This paragraph at the end of the 'Using configuration qualifers' effectively covers it.
Be aware that, when the Android system picks which resources to use at runtime, it uses certain logic to determing the "best matching" resources. That is, the qualifiers you use don't have to exactly match the current screen configuration in all cases in order for the system to use them. Specifically, when selecting resources based on the size qualifiers, the system will use resources designed for a screen smaller than the current screen if there are no resources that better match (for example, a large-size screen will use normal-size screen resources if necessary). However, if the only available resources are larger than the current screen, the system will not use them and your application will crash if no other resources match the device configuration (for example, if all layout resources are tagged with the xlarge qualifier, but the device is a normal-size screen).
The answer from silentnuke is correct in your case if you're only providing density-specific resources for hdpi and mdpi. Be aware that if you also provide resources for ldpi, the logic will fall through to the drawable-ldpi folder before checking the default drawable folder.
I tend to either use only the default folders or, if I'm catering for device density, only the density-specific folders and avoid using both approaches. Just my preference and way of working.