Add image from mipmap in android drawable item part - android

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."

Related

Android splash screen stretches a logo on the full screen

I have a simple splash screen with a white background and the logo on the center of the screen. In a modern version of the Android, its work well but in 'old' versions of Android(for example API 22) my a logo is stretched on the full screen. How to make the logo splash screen look normal on all the version of Android (before API 19)?
p.s. I`m not the Android developer.
drawable/background_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white" />
<item
android:width="150dp"
android:height="50dp"
android:drawable="#mipmap/my_logo"
android:gravity="center" />
</layer-list>
values/styles.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:statusBarColor">#color/white</item>
</style>
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
<item name="android:statusBarColor">#color/white</item>
</style>
</resources>
Instead of using a simple <item/> tag, use a <bitmap/> tag inside of an <item>.
From the Layer List drawable documentation:
All drawable items are scaled to fit the size of the containing View, by default. [...] To avoid scaling items in the list, use a <bitmap> element inside the <item> element to specify the drawable and define the gravity to something that does not scale, such as "center".
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white" />
<item>
<bitmap
android:src="#mipmap/my_logo"
android:gravity="center"/>
</item>
</layer-list>
This is because the attributes android:width and android:height were introduced in API level 23. The only solution I can think of if you want to show the same image with a relative size according to the screen size is to provide alternative bitmap resources.
Here is a link to the Android Developer Guide which explains this a bit further.
First thing doesn't use a png use a vector drawable. This way you don't need a separate image for different screen size. Use this to convert image http://a-student.github.io/SvgToVectorDrawableConverter.Web/
and now in your image view in XML file set your scale type to Center Crop or Fit XY
android:scaleType="centerCrop" or use android:scaleType="fitxy"
The issue is not related to Android version. it should be related to the screen size of Devices.
To declare different layouts and bitmaps you'd like to use for the different screens, you must place these alternative resources in separate directories/folders.
This means that if you generate a 200x200 image for xhdpi devices, you should generate the same resource in 150x150 for hdpi, 100x100 for mdpi, and 75x75 for ldpi devices.
In a word, Android apps should provide 6 image sizes: ldpi (0.75x), mdpi (1.0x), hdpi (1.5x), xhdpi (2.0x), xxhdpi (3.0x), and xxxhdpi (4.0x).
What I usually do is I make sure my layout's root node is a RelativeLayout, and the first child is an ImageView with centerCrop as the scaleType.
Here's an example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:scaleType="centerCrop"
android:background="#drawable/img_background"/>
...

Android drawable item size on different resolutions

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.

Android Radio Button PNG Drawable top

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!

Android - How to implement 9 patch?

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

android eclipse are two hit state xml files needed in md and hd drawable folders

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.

Categories

Resources