Drawables by country Android - android

I'm translating my apps to a few languages but now I'm struggling with a problem! I a few images with text on it, there is a way to provide drawable resources using country code? If I add the drawable to my apps using the country code It will increase the size of the app? I don't wan't have an app that is now 1MB to be 4 or 5 just because of those images.
Thank You!

Equal you set diferent languages with the res directories values-es values-fr values ...
you can set differents drawables for each location with the directories:
drawable-es drawable-fr ...
http://developer.android.com/training/basics/supporting-devices/languages.html
Remember that the extra information of the directories has specials importances and you should put in the correct positions http://developer.android.com/guide/topics/resources/providing-resources.html
For example, drawable-es-hdpi
The problem of the size of your apk i imagine that you don't need to set all your images diferents for all the countries, isn't it? Normally you only need configure a few images

Yes, it is possible to provide resources per country. Check out this answer for details on how to do it: Localization and drawables
It will definitely increase the size of your app. It is, like everything else in software development, a compromise between performance (or quality of images) and resouces (in this case, size of apk). To avoid increasing the size of your app exponentially, you can try to provide resources per locale instead of country, or simply, provide only the hdpi version.

If you don't want to increase the size of your apk with the different drawables, you can code a custom ImageView and set the text over it with a String (that will be translated) or put a textView on top of the ImageView setting them inside a relativelayout like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
>
<ImageView
android:id="#+id/yImage"
android:layout_width="fill_parent"
android:layout_height="25dp"/>
<TextView
android:id="#+id/yTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/yImage"
android:layout_alignLeft="#+id/yImage"
android:layout_alignRight="#+id/yImage"
android:layout_alignTop="#+id/yImage"
android:background="#color/transparent"
android:lines="2"
android:textColor="#color/Black"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
It would be easy also with a frame layout.

Related

load images from assets through XML

I use in my application more than 150 images, and 30 icons ! so I find it a little bit messy to put those images all together in Drawable folder, and as it is impossible to have sub-folders in drawable , I am trying to load my images from the Assets folder.
So to load images from assets programmatically its OK.
MY QUESTION is how to access Assets through XML?!
with drawable we use this :
<ImageView
android:id="#+id/img"
android:layout_width="25dp"
android:layout_height="25dp"
android:src=" **#drawable/ic_rate** "
/>
So what is the equivalent using assets.
Despite the fact that you should use drawables, you can define your own asset_drawable attribute as string than override the ImageViewto look for that atttibute and load the bitmap from assets. Your xml will be look like this
<com.foo.bar.AssetImageView
android:id="#+id/img"
android:layout_width="25dp"
android:layout_height="25dp"
app:asset_drawable="some/path/asset"
/>
You don't. An ImageView expects a drawable. Not all assets are drawables.
Drawables let you use images of different screen densities. Just use those. If you're struggling to stay organized, perhaps you could try using prefixes (e.g., prefix icons with ic). Another option would be to create a library project exclusively for icons, though it may be overkill.

TextView Background Resource

I implemented the following TextView in a RelativeView:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/bg"/>
The problem is that the size will be set dynamically to 15% of the screen while the application is running. But when I do this the background ressource doesn't get the size of the TextView. It has the original size of 150x150 px. What can I do to avoid this?
(I don't want to take a smaller ressource because it doesn't look well then)
Try using another scaleType, for example android:scaleType="fitXY".
Also you can try making a nine-patch image. To do this, go to your Android SDK folder - tools - draw9patch application. Then you will have options to open your image and then save it as nine-patch image. Reference it as usual in your layout.

Why are ?Images Compressed when Displayed on Android Phones/Emulator?

Essentially I want an HD background on a layout, but instead of being a pristine image, it shows up extremely compressed.
Example:
Source Image:
Android Emulator version:
I thought maybe that it was just the emulator, but it looks that bad on the phone itself. I know these are high quality Samsung Galaxy S phones (these are the ones that came with Avatar preinstalled after all).
Does anyone know how to load images without compression? Source files are uncompressed pngs.
here's the code for what it's worth:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="1" android:background="#drawable/background" android:id="#+id/blank">
</LinearLayout>
Figured it out: The problem is the image is automatically converted into RGB565 colorspace from AGB888. To prevent this, make sure the image has an alpha channel and is in a "raw" directory instead of the "drawable" directory.
The aapt may compress images when it builds the apk, but that compression is supposed to be lossless. The problem is probably somewhere else. Here are two possibilities.
If your image resource is being loaded from res/drawable on a device (or emulator) that is not mdpi, then it will be scaled as described here. One solution is to put the resource in the res/drawable-nodpi folder. Another is to provide several, density-specific images.
Another problem will arise if your background image does not match the size of the view. The view will automatically scale the image to fit the view size. You can prevent this by defining the background as an XML Bitmap with a gravity set to something that does not scale (such as center).

Customizing Android checkbox

I am trying to partially change the images for Android's checkboxes. Following the tutorial here, I have done the following experiment:
<CheckBox
android:checked="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<CheckBox
android:checked="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:button="#android:drawable/btn_check_on"
android:background="#android:drawable/btn_check_label_background"/>
<CheckBox
android:checked="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:button="#drawable/cb_on"
android:background="#drawable/cb_background"/>
For the third checkbox, I have copied the images btn_check_on.png and btn_check_label_background.9.png from Android's SDK to the project's res/drawable and rename them to cb_on.png and cb_background.png. While I expected the three checkboxes to have identical appearances, surprisingly, the third checkbox is larger than the first two. Can anybody explain why? How can I fix this problem?
Try renaming your image cb_background.png in cb_background.9.png
.9 means that the image is a nine-patch image which is done to rescale itself automatically.
I think that why the third image is larger.
For more information about nine-patch: here
My guess would be that you only copied one resolution of the drawables. You need to copy all of them (mdpi, hdpi,ldpi) to the appropriate res folder (drawable-mdpi, drawable-hdpi, drawable-ldpi)
#Jokahero is right about the 9-patch, and #jkhouw1 is right about the resolutions. For checkboxes, you will also need to know about StateListDrawable.

Why are my images compressed?

I'm using layout xml file for the UI. But the images are compressed and the qualities have lost in some level.
My code is like this:
<ImageView
android:layout_width="480px"
android:layout_height="717px"
android:layout_x="0px"
android:layout_y="45px"
android:scaleType="fitXY"
android:src="#drawable/e4" />
The drawable is actually 480x717.
What's the problem here? Is it due to the fitXY?
Of course it is.
You say you want some dimensions, but then you say you want to scale it.
So it scales it.

Categories

Resources