How to set R.drawable.ic_myicon to be seen programmatically - android

I would like to set drawable in my image to adapter.
I have my files in /res/drawable/.
In layout xml file I can add like:
android:drawableLeft="#drawable/ic_myicon"
but when I want to do it programmatically.
R.drawable.ic_myicon not seen (not such files) and I don't have any of my created icons there, only some defaults
android.R.drawable and R.id.~ does not work. I have only access to .png not .xml(vectors)

Related

Fill color of vector drawables that are icons in dialogs or toolbars

What if I would want to use vector drawables as icons in dialogs or logos in toolbars? Then I would use them like this:
alertDialogBuilder.setIcon(R.drawable.my_vector_drawable);
toolbar.setLogo(R.drawable.my_vector_drawable);
toolbar.setNavigationIcon(R.drawable.my_vector_drawable);
...
Am I right?
But here comes up a question: how can I change their fill color without modifying vector xml files?
For instatnce I can change fill color of any vector that is in view by using 'tint' tag in xml or 'setColorFilter()' method in code.
If you want change fill color not change xml file you must create Drawable instances of this file. This instance get you method to change your file.
for example:
Drawable myIcon = ContextCompat.getDrawable(this,R.drawable.my_vector_drawable);
myIcon.setColorFilter(ContextCompat.getColor(this, R.color.yourcolor));
alertDialogBuilder.setIcon(myIcon);
toolbar.setLogo(myIcon);
toolbar.setNavigationIcon(myIcon);

How to change a resource icon in a imageview with another resource icon

I have been reading some post about changing an image in an ImageView.
But everyone asks for setting an ImageView with a custom image, and I want to set a system image inside it.
I made this:
ledicon.setImageDrawable(getResources().getDrawable(R.drawable.presence_busy));
But Eclipse tells me that presence_busy is not recognized.
Why?
I am using presence_online in the XML file,
android:src="#android:drawable/presence_online"
and all works perfectly.
Try this:
ledicon.setImageDrawable(getResources().getDrawable(android.R.drawable.presence_busy));
(When accessing system resouces you have to look them um in android.R not your own.)

android drawable selection custom

To better explain: For example i have two languages available. Thats why i will have image for English and image for Russian. Both of them i reference in one drawable xml file.
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/image_eng"></item>
<item android:drawable="#drawable/image_ru"></item>
</selector>
What type of drawable xml files i should use ?
How can i set and detect state and switch image ? Basicly it works like android:state_
If I understand correctly, you want to use one image when the phone is in English, and the other when the phone is in Russian. You should use the language modifiers on the drawable folder, not in the drawable name.
So, you would have a folder named drawable-en, which would contain your image for English (drawable-en/image.png, for example). Then, have another folder named drawable-ru which contains your Russian version of the image, but it must have the same name (drawable-ru/image.png).
Then, in your layout XML (or wherever you're referencing the drawable) just reference #drawable/image ... Android will automatically choose the appropriate image.
Please read how Android resource Localization work.
You don't have to use selector for that , or force anything.
just put your image file in the following folders:
res/drawable-en
res/drawable-ru
and in your code you can use:
Drawable draw = getResources().getDrawable(R.drawable.image);
The app will select the suitable one based on the phone's language

Changing tab color on Android works partially

I've been trying to change tab colors of my android app. But i can't get it done. I've copied all the needed images and repainted them in Photoshop. But this is all i get.
What i first did was copy the tab_indicator.xml from the drawable folder in android-sdk\platforms\android-8\data\res\drawable.
Then i copied all the images that are referenced in that xml file to my own drawable-hdpi folder. I copied those image from the android-sdk\platforms\android-8\data\res\drawable-hdpi folder.
I re-painted the tab_unselected.png file like this:
Orignal file (tab_unselected.9.png):
New file:
But as you can see in the top screenshot, only the borders are colored. The tab itself just remains black when unselected.
What do i have to do to style that tab without the use of any Java code. I don't mind adding any XML files or editting .png files.
BTW, i added a custom TabHost xml file in my layout folder and added android:background="#drawable/tab_indicator" to the TabWidget tag.
if you wanna change only tab color you not required to use png files. Take a look here, hope it will be helpfull http://joshclemm.com/blog/?p=136

Setting a picture for button i made in XML (Android)?

I'm having this question about setting pictures for Buttons in XML :
I made some .png pictures I want to set as pictures instead of the buttons that I get in default when i create my android apps.. Now I loaded in the files into the drawable folder.. And how do I write in XML to get the right thing?
Is setting the button visible to false and adding a picture as background for it an option?
Because that's all the ways I thought about. Or is there a better way to do this kind of thing?
what I understood is that you want to set the picture in background. Do it like this in your Button tag:
android:background="#drawable/yourpicturename"
Add the png to the drawable folder that is under the res folder in your project (if you are using eclipse). Then in your XML file simply use android:background="#drawable/filename". Make sure to omit the file extension
You can use any of:
drawableTop, drawableLeft, drawableRight, drawableBottom
For example,
<Button android:drawableRight="#drawable/some_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Some text" />
The image some_image.png will be positioned to the right of your text in this case.

Categories

Resources