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
Related
Hy,
I am using the next background colour #android:color/transparent for a ImageButton in the layout.xml. Could or should I externalize it in the colors.xml resources file?
Thanks
You could but it depends on your needs.
If you use that color in many different parts of your application it's best to place it in an external xml file. Sometimes you need to change the style of the whole application and it's easier to change the definition of that color instead of changing the color in many different places. If you're only using it in one single place then it is OK to leave it out of the external file.
EDIT:
<color name="my_background_color">#android:color/transparent</color>
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
Hello I have a drawable myshape.xml, it contains a <shape> and I cannot set an android:id to shapes.
In my code I want to set the background of a view to this file using
catAll.setBackgroundDrawable(getResources().getDrawable(R.id......???));
where myshape.xml does not show up in my R file because it has no id. and I cannot set id to object.
In my XML I do set the shape by simply typing the drawable resource name. But I need to do this programmatically.
You don't need to get the drawable yourself. Use this instead:
catAll.setBackgroundResource(R.drawable.myshape);
For future reference, if you do wish to get the drawable keep in mind that drawables live in the R.drawable namespace. So your code would became:
getResources().getDrawable(R.drawable.myshape);
This is akin to what you do in your XML:
#drawable/myshape
instead of
#id/myshape
The question is really old but googles first hit references to this thread.
So getDrawable(id) is deprecated.
Short solution (kotlin)
yourView.background = ContextCompat.getDrawable(context, R.drawable.your_ressource_id)
For more, please read this: https://stackoverflow.com/a/29146895/4420355
I have added spanish and french to my app but some of the wording is longer in spanish then english. how can i change the textsize when the values-es/string.xml file is accessed
You can use the dimens.xml resource file for this purpose. In your case you'll probably want to create a file called res/values-es/dimens.xml, and possibly also a -fr version. You can specifify the default values in res/values/dimens.xml (or res/values-en/dimens.xml, if you want to be more specific).
Example grabbed from the More Resource Types section on developer.android.com:
dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textview_height">25dp</dimen>
<dimen name="textview_width">150dp</dimen>
<dimen name="ball_radius">30dp</dimen>
<dimen name="font_size">16sp</dimen>
</resources>
Apply in xml
<TextView
android:layout_height="#dimen/textview_height"
android:layout_width="#dimen/textview_width"
android:textSize="#dimen/font_size"/>
Or in code
float fontSize = getResources().getDimension(R.dimen.font_size);
There are also solutions here on SO that use a iterative/recursive process to shrink the text size of a TextView to 'fit' in its bounding box (using a custom view), but I'd say above is a more robust approach, especially if you're considering adding more languages in the future.
The above explanations are correct, but they don't fully explain how to do it.
When you open your project in Android Studio, then it automatically shows this project in the "Android" mode. You need to click on the "Android" tab in the top, left corner of Android Studio and select "Project". Then you need to go into "app>src>main>res". Then you need to right-click on the "res" folder and from the menu that comes up, select "New>Android resource directory". A dialogue will come up, and for Directory name: type in values-es and click OK.
This will create a folder for all Spanish Locale values. And then you can right-click on this values-es folder to create dimens.xml, string.xml, color.xml, ...etc. files that will be used whenever Spanish Locale is selected in the phone.
If you've already created a string.xml file for Spanish Locale through the graphical user interface, then the values-es folder with string.xml file will already be in the Project, when you go there. And in this case, you just need to right-click on the values-es folder to create the dimens.xml file there for Spanish Locale.
You would need to specify a different layout file in layout-es. That way when Android pulls from values-es/string.xml, it'll load the different layout-es/yourfile.xml. That layout file can then specify a theme, style, or text size on the views.
I am teaching myself Android using Eclipse, the Android plug-in, and Sams "Teach Yourself Android Development" book. I have this weird little problem. I've been able to create xml files in the res/values directory that hold strings and color values (colors.xml and strings.xml). I have been able to reference these values in the properties of my Android screens (the xml in res/layout), for example setting the "Text" and "Text color" properties with references like "#string/topTitle" and "#color/titleColor," where topTitle and titleColor are defined in the xml files.
BUT: when I create a file called "dimens.xml" and have font sizes in it, Eclipse correctly puts this file in res/values, but when I try to reference these values, e.g. "#dimension/titleFont" I get an error "No resource found that matches the given name." I've tried lots of different names, I've tried "#dimens" instead of the type, still nothing. If I go into the layout xml file and set it explicitly to a font size, e.g. 22pt, it works.
So Eclipse recognized my "dimens.xml" file when I made it well enough to put it in res/values, and lets me edit it, and shows it full of (dimension) values. It just doesn't recognize my referring to it in other xml files.
The book I'm using doesn't actually show a dimension example so I must be doing something wrong. I checked the Android docs but couldn't see any problem.
Any help appreciated. Thanks.
The correct way to refer to a dimension variable (stored in your dimens.xml (don't think the name here really matters though, it's what's inside that does)) from another xml file is like this:
"#dimen/nameOfVariable"
Notice that it is neither dimension, dimensions or dimens, but dimen!
If you look inside your xml file where you have your values, this will make sense as dimen is the name of the xml elements storing dimension values:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<dimen name="someDimension">5dp</dimen>
<dimen name="anotherDimension">10dp</dimen>
</resources>