I want to add an image background to my buttons (which are added dyncamilly), but the image original size looks applied by default, and it is far too big. How can I adjust my image to fit with the button size automatically ?
Drawable img = act.getResources().getDrawable(R.drawable.top);
button.setBackground(img);
Use an ImageButton http://developer.android.com/reference/android/widget/ImageButton.html
They should handle the sizing automatically
Inside your Button xml, try adding this
android:scaleType="fitCenter"
Related
i try to fit any image inside a diamond shape. My image is dynamic, how can i handle this ?
This is what I do:
and this is what I'm trying for:
You should try something like this this creates a custom shape ImageView and then you can use it as you see fit.. then you can add some edge color to have the effect you want
I am developing an app based on client-server application. I am using tablelayout, and in tablelayout am adding some textview, edit text and image( for click ). My screenshot is as follows:
as you can see in my image add button blurs. It is just a 3kb file.
final ImageView img_add_icon = new ImageView(MainScreen.this);
img_add_icon.setBackgroundResource(R.drawable.add_item_order);
and my xml file is:
<ImageView
android:id="#+id/add_to_order_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dip"/>
What can I do to stop the blurring of this image?
Please Use
setImageResource(resId)
to Sets a drawable as the content of this ImageView.
To Control how the image should be resized or moved to match the size of this ImageView
use
setScaleType(ImageView.ScaleType scaleType)
[1]: http://developer.android.com/reference/android/widget/ImageView.html#setImageResource%28int%29
[2]: http://developer.android.com/reference/android/widget/ImageView.html#setScaleType%28android.widget.ImageView.ScaleType%29
I have an ImageView that scales down when I set the layout width to fill parent. But when I change it to an ImageButton, it doesn't scale. How do I get the ImageButton to look like the ImageView?
Use the android:scaleType attribute (set to center inside or something like that).
However, even if you can get it to scale, I am not sure if you'll get the result you want. The imagebutton displays a regular button, but with an image instead of text, that is you'll get the button-borders around your image. If the imageview works as you want it to, why don't you keep it as an imageview, but set the focusable and clickable attributes on it?
As Jave suggested, set scaleType to centerCrop. And also set padding to 0dp to take out button borders.
I have seen these different approaches in setting images but I don't get the difference.
Why there two methods?
setBackgroundResource is for setting the background of an ImageView.
setImageResource is for setting the src image of the ImageView.
Given:
ImageView iv = new ImageView(this);
Then:
iv.setBackgroundResource(R.drawable.imagedata);
Will fit the image for the entire background. That means it will stretch the image to fill that background entirely even if the image size is too small.
imageView.setImageResource(R.drawable.imagedata);
Will occupy only the size of the image in ImageView.
For that you want to also set
android:layout_width="wrap_content"
android:layout_height="wrap_content"
for your ImageView. If the size of the image is smaller than the ImageView the remaining border will be left blank and the background will be shown.
SetBackdroundResource is for a drawable or color you want to set at the background of the imageview and your setImageResource is like to display on it.
so setImageResource is for add any resource to your imageview's front side. try this example and look at the difference. Android Gallery, ImageView Example
. This is a two layer effect,backside (setBackgroundResource) and frontside (setImageResource).
The method setBackgroundResource() belongs to all Views. The method setImageResource() only belongs to ImageView. You can set them both:
imageView.setBackgroundResource(R.drawable.sky);
imageView.setImageResource(R.drawable.balloons);
The setBackgroundResource() method will cause the image's width and height will be stretched to fill the size of the view. The setImageResource() method will let its image keep its aspect ratio.
My fuller answer is here.
setBackgroundResource sets the background image of an ImageView. The XML attribute is: android:background
setImageResource sets the image displayed in an ImageView. The XML attribute is: android:src
I like to use a customized (Radio)Button and I know I can use whatever image for the background with the following code inside the xml:
<RadioButton
...
android:button="#drawable/myDrawable"
../>
However: I want to use three radiobuttons, that fill the whole row. But the drawable that I use for the button, doesnt get resized depending on the sreensize, so if the drawable is too big only parts of it will show, and if it is too small, not the whole screen will be filled. If I would use ImageViews I could change the ScaleType like so:
ImageView myImageView = (ImageView) findViewById(R.id.myimageview);
myImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
But there is no such property for other Views, than the imageView. So how can I accomplish the same for (Radio)Buttons?
You will have to provide images with different sizes (see Supporting Multiple Screens)