How to give icon path to xml in android - android

I've created a directory inside drawable directory which contains two icons "empty_star.png". Now how to give address of these icons in style.xml
i tried following:
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="drawable/icons/empty_star" />
<item android:drawable="drawable/icons/empty_star" />
but it shows error.
i also tried #drwable/icons/empty_star but still getting error.

Try this:
android:drawable="#drawable/empty_star"

The resources mechanism doesn't support subfolders in the drawable directory. You need to keep that hierarchy flat. Place your images in drawable folder and use it as android:drawable="#drawable/empty_star"

Can the Android drawable directory contain subdirectories?
According to the answer posted earlier, resource mechanism does not support sub folders in drawable folder. So you should maintain flat hierarchy.

Related

Android Image Asset is not drawn

I am trying to implement a splash screen for my application, following the usual method; defining a new theme inside the styles XML file that places a drawable as windowBackground and reverting to the main theme in the Activity's onCreate().
However, I am having problems when showing the logo I developed in Photoshop: I exported it as a 144x144 PNG file, created an Image Asset with it (replacing the standard ic_launcher drawable) but when I try to visualize it inside an XML file, this image is displayed instead
Screenshot
Any ideas? I've also tried to create a vector asset from the .psd but nothing is shown either...
https://romannurik.github.io/AndroidAssetStudio/
use this link to generate your icon. It will work.
Change the code and so on
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/your_background"/>
<item android:drawable="#drawable/your_image">
</item>
</layer-list>

What is the purpose of the file "drawables.xml" under the "values" directory?

For an android studio project, I found a file named "drawables.xml" in the "values" folder
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<item name="ic_menu_camera" type="drawable">#android:drawable/ic_menu_camera</item>
<item name="ic_menu_gallery" type="drawable">#android:drawable/ic_menu_gallery</item>
<item name="ic_menu_slideshow" type="drawable">#android:drawable/ic_menu_slideshow</item>
<item name="ic_menu_manage" type="drawable">#android:drawable/ic_menu_manage</item>
<item name="ic_menu_share" type="drawable">#android:drawable/ic_menu_share</item>
<item name="ic_menu_send" type="drawable">#android:drawable/ic_menu_send</item>
</resources>
What's the purpose of this file? Why does it create another reference to an existing drawable and why not just use "#android:drawable/ic_menu_camera"?
It may be that the developer had a bit of future-proofing in mind.
The developer could have had Java code reference android.R.drawable.ic_menu_camera and have layouts or other things reference #android:drawable/ic_menu_camera. However, if at some later point, the developer needed to switch from using a platform drawable to a custom one, then all of those references would need to be changed.
The drawable aliases set up in this file allow Java code to reference R.drawable.ic_menu_camera and layouts and other things reference #drawable/ic_menu_camera. Right now, the aliases indicate that those drawables "redirect" to platform drawables. However, at some time in the future, the developer could add a custom ic_menu_camera drawable, remove the alias... and nothing else needs to change in the Java code, layouts, etc.
Few apps refer to platform drawables, and so you will not see this sort of trick used most of the time. But, for cases where apps do refer to platform resources, this aliasing approach can reduce long-term maintenance.
FWIW, I cover this more in this blog post.
This is very useful for whitelabeling. So e.g., your main app can use one drawable but a whitelabel could use another but your code can reference the drawable using a single reference.
Well In my opinion this file is useless because you already have the drawable file names.

Selector does not work with mipmap image in android

All I want is that an imagebutton which changes to another image when user press the button on. Following is my directory list. As you see I don't have drawable-hdpi, drawable-mdpi ..and others. And also I can't see mipmap-hdpi, mipmap-mdpi ..and others. My problem is I can't add selector xml for my imagebutton.
directory list
Following image is my content_main file which known as activity_main.
content_main.
My program shows the image of button but when I add android:background="#drawable/fbpressed" code piece in content_main, the program fails. it says unfortunately Login2 has stopped!
I tried lots of combination for my selector file which is fbpress.xml. like I tried android:icon="#mipmap/facebook_pressed" but it fails again and again. any idea?
Please change you selector:
<selector>
<item android:drawable="#mipmap/facebook_press" android:state_press="true">
<item android:drawable= "#mipmap/fracebook">
</selector>
Please click the Android in you screenshot , and change it to Project.It can be more clear.
Yes guys I've solved the problem. I did not know that i should change my values/styles.xml file if i add an resource file into my button. (which is fbpress.xml)
Also I changed my folder representation from Android style to Project style so that i can see my mipmap-hdpi, mdpi etc.
change from android to project
I add those lines into my styles.xml. fbpress is my resource file which is selector file.
<style name="fbpressed" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/fbpressed</item>
</style>
Following is my part of content_main.xml. I added android:background="#drawable:fbpressed" and style="#style/fbpressed" lines into this file.
imageButton
And finally my button works =) Thanks for all your helps

android layout load different image resources at orientation change

I would like to have a solution in xml or a keyword for what to search to solve my problem:
I have multiple screens. Each screen has his own xml in layout folder. For eg a part of the code :
<RelativeLayout
android:id="#+id/headerLayout"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="#drawable/header" >
<Button
android:id="#+id/btBack"
android:layout_width="55dp"
android:layout_height="33dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
android:background="#drawable/bt_back"
/>
</RelativeLayout>
now, I would like to change the "#drawable/header" value runtime when is a rotation, prefferable to configure and Android change not me in Java code. The same thing is done with
"#drawable/bt_back", which is a bt_back.xml file at drawable folder and has a selector. His content is here:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- selected state -->
<item android:drawable="#drawable/bt_back_pressed" android:state_pressed="true" android:state_selected="false"/>
<item android:drawable="#drawable/bt_back_pressed" android:state_pressed="false" android:state_selected="true"/>
<item android:drawable="#drawable/bt_back_pressed" android:state_pressed="true" android:state_selected="true"/>
<!-- unselected state (default) -->
<item android:drawable="#drawable/bt_back_normal"/>
</selector>
Now in folder what I want drawable-hdpi, drawable-ldpi, I just need to put those 2 files and the android system is auto-handling the image changes for pressed and normal state depending on user device dpi.
The same thing I would like to have for header for rotation from portrait and landscape. Eg it should alternate the header_port.png to header-land.png.
How is possible? Any ideas?
I don't wand different .xml layout for landscape, nor digg in java code.
Maybe in different folder the resources for header, described here ? port and land?
You are able to create a drawable-land-hdpi directory. Put your landscape versions in the qualified folders and android should automatically load them for you.
To elaborate on #toadzky's answer, you can create a different drawable directory for any qualifier that you find on this Android Developer page, and put images with the same name in each, and android will automatically know which one to use. The same is true for layouts in the layout (or layout-land, etc.) folder, value xml files (e.g., string resources--useful for localization--or themes), in the values folders, or any other things you normally find in the res folder.
It's also important to note that the way its precedence works (with respect to which directory it picks for resources) is, if I recall correctly, "first match after excluding all incompatible directories".
So, if you had only drawable-hdpi and drawable, and you had an mdpi device, it would eliminate all directories qualified with hdpi, and select from the remaining directories (so you'd end up with the version from the drawable folder). Further explanation is found on the linked page, under "How Android Finds the Best-matching Resource".

Android ToggleButton indicator

I'm struggling with the Android ToggleButton because I try to change the green indicator light. In the Android SDK folder I've found the file drawable\btn_toggle_bg.xml where it says
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+android:id/background" android:drawable="#android:drawable/btn_default_small" />
<item android:id="#+android:id/toggle" android:drawable="#android:drawable/btn_toggle" />
</layer-list>
I copied this file to the drawable folder of my project and changed the last item to
<item android:id="#+android:id/toggle" android:drawable="#drawable/btn_toggle" />
and then took the original btn_toggle.xml from the Android SDK, copied into the drawable folder, too. btn_toggle.xml is this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#drawable/btn_toggle_off" />
<item android:state_checked="true" android:drawable="#drawable/btn_toggle_on" />
</selector>
So then I asumed that it would be necessary to create btn_toggle_off.png and btn_toggle_on.png. At last I added the line
<ToggleButton
...
android:background="#drawable/btn_toggle_bg" />
but finally, the ToggleButton looks completely strange.
What I noticed are files called btn_toggle_off.9.png and btn_toggle_on.9.png in the SDK but I couldn't find a reference to these files although they look exactely like the original indicator.
Can you help me? :)
Without completely re-styling the toggle button widget, the resources you are trying to replace should probably be nine patch PNGs like the originals are.
Copy btn_toggle_off.9.png and btn_toggle_on.9.png into your project and modify them, or use the draw9patch tool to properly add the nine patch metadata to your own images.
Refer to the following documentation for an explaination of Android's nine patch drawables:
http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
http://developer.android.com/guide/developing/tools/draw9patch.html
The ".9" part of the filename must be retained, but is not referenced in XML resources. So btn_toggle_on.9.png is referred to as simply "#drawable/btn_toggle_on".

Categories

Resources