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.
Related
I am trying to set a background image for my app. I have the image in the Drawable section.
I have created an ImageView and am trying to set the src to that file.
However, the image doesn't show up anywhere in the Resources window that opens.
I also tried setting the image as src manually through the XML. But then the image won't display.
The name of the image satisfies all conventions (all lowercase characters, no special characters, etc).
I've used images before but I can't wrap my head around this particular case.
XML code:
<ImageView
android:id="#+id/backgroundImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:src="#drawable/weatherbackground" />
I'm not setting the image through Java code.
Any help would be greatly appreciated.
Thanks.
I'm using Android Studio 1.2.2 I made a form activity and I want to insert an image. Where should I put the image? What changes I have to do in my mainactivity.java
file?
You can use Android's ImageView tag. You need to write it inside .xml file of your activity. You can take reference of it from android's devloper's website.
In Andorid studio you can not directly drag & drop images.
It will be good for you to take reference of any example.Here is the ImageView xml code.
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/android" />
You can now make this ImageView reference in your MainActivity (Java) like below:
ImageView imageView;
imageView = (ImageView) findViewById(R.id.imageView1);
Take a look at the docs: http://developer.android.com/guide/topics/resources/drawable-resource.html
Commonly, you will want to use some of the qualifiers by screen density and provide each image at different resolutions, docs here, this way lets Android pick up the best image size and scale it to match the phone screen.
Personally i use -nodpi and perform scalation by code, this way lets only have one copy of each image, and, gives you max control to position and size your images.
Hope this helps.
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.
I'm trying to change my application data source to the sdcard so that it doesn't take too much internal memory.
However, I had an Image that was dynamically loaded from the resource and displayed in an ImageView, now I load the image from the sdcard whenever I need it.
I had been using imageBox.setImageResource(imageID) and I changed to imageBox.setImageDrawable(imageDrawable).
The problem is that now Android resizes my image and it doesn't fit where it should.
Why is it resizing the image this way? And how can I stop it?
Thanks in advance :)
Add the tag android:scaleType="fitXY" to your ImageView's layout XML.
Ex.
<ImageView
... several settings ...
android:scaleType="fitXY"/>
Edit:
If your image is simply too big to fit inside the ImageView - it will have to do some kind of scaling. There are some other options as described in the API:
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Try the following for ImageView:
android:layout_height="fill_parent"
android:layout_width="fill_parent"
No Scale Type.
The ImageView Should be within Fixed Size (in dp) Parent View.
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.