Customizing Android checkbox - android

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.

Related

Android Studio and PNG vector images

Creating vector images in Photoshop (for transparent backgrounds). When I add them as the image source for my ImageButtons, and run the app, the image displays with a white background.
Can't find any useful info on why the images are not coming out transparent through the app.
Are there some additional steps/coding required to make vector images work properly? I can't see why.
Example code, as requested:
<ImageButton
android:id="#+id/btnGameUp"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/arrowright"
android:adjustViewBounds="false"
android:clickable="true"
android:cropToPadding="false"
android:padding="0dp"
android:scaleType="fitXY"
android:layout_marginLeft="270dp"
android:layout_marginTop="260dp" />
No one came to help us, but we managed to find the solution on our own after hours of crying and holding each other for comfort.
In the layout xml file, you need to add the following line to each vector object:
android:background="#android:color/transparent"
That's it! Tested and working perfectly. Hope this helps someone.

Drawables by country 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.

Background image crashes android

My application crashes when i put background line like in this code. When i delete this line my application works fine and also when i use another photo it also works. What's the problem?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/table2" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
The background for the main screen works fine also. But in the second page, it is problamatic.
If you are using only drawable folder to store all images.then change storage folder for high resolution images in drawable-xhdpi folder or higher.
Hope this helps.
Thanks to you all. I changed the dimensions from 2500x1900 to 1280x920, now it works.
Make sure about the image path if it is in drawable folder..put it inside Mipmap folder in your project directory.
I think you should check the size of your image anything above 300kb may tend to constantly crash your app. You can always use image compression Softwares to reduce the size of your image.

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