keep button drawable image dimensions as original - android

I created an app where I use Google Books API in it.
As part of the attributions they require, I need to add to each query results the following button:
Now, I wanted to use that Image as a background to a button but I got this results:
As you can see it seems to be stretched with bad quality.
The xml I used is:
<ImageButton
android:id="#+id/tv_Link"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_alignStart="#+id/imagecard"
android:layout_alignEnd="#+id/imagecard"
android:layout_alignBottom="#+id/imagecard"
android:background="#drawable/gbs_preview_button"
android:elevation="50dp"
android:fontFamily="#font/assistant_semibold"
android:gravity="center"
android:textColor="#color/colorLightPurple"
android:textSize="8sp" />
In order to fix it, I decided to use bitmap:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:src="#drawable/gbs_preview_button"
android:tileMode="disabled"/>
The problem is that it became small like this (good quality, but small):
This dimension is too small since they require the height to be 24dp.
Is there any way I can add this image to the button, to make it 24dp height and still to keep the quality of the image?
Thank you

As you want to hardcode the height to 24dp, then you can also hardcode the width to a value that keeps the aspect ratio of the original image size which is (88 x 31).
So, set the width to 68dp instead of wrap_content

Related

Keeping the size of ImageView constant for different size images

I have a standard image in my app for every song that doesnt have an album cover. Now, for the few that do have in there metas saved an album cover I want to replace the "standart" image.
The problem however is, that the new image changes the size of my layout. It basically now adjusts to the new images' dimensions. I wish that would not be the case. I simply want to replace my old image with a new one ( both are circles ) without altering the size at all.
This didnt work for me:
vh.CoverArt.SetImageBitmap(output);
Thanks for your help! :)
Sure:
<ImageView
android:paddingTop="5dp"
android:src="#drawable/btn_musicalnote"
android:id="#+id/musical_note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp" />
When you use wrap content then your view will expand according to the size of resource dimensions you use. In your case ( Imageview size will increase or decrease according to the size of image you use in it.)
To set a fix size of your imageview define height and width attributes manually. For example below code will produce an imageview of size 40X40 dp.
<ImageView
android:paddingTop="5dp"
android:src="#drawable/btn_musicalnote"
android:id="#+id/musical_note"
android:layout_width="40dp"
android:layout_height="40dp"
android:paddingBottom="5dp" />
You can change height and width attributes according to your need.

ImageView Scaling: fix height variable width with maxWidth

I am going to show some images, but my constraint is as title. I want to make all images have same height but according to their width they must have different width. I just do not want to lose any part of the image when scaling. here is my code:
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/ImageView_IssueFirstLookActivity_magas"
android:layout_width="wrap_content"
android:layout_height="170dp"
android:layout_marginLeft="32dip"
android:layout_weight="2"
android:maxWidth="40dp"
android:adjustViewBounds="true"
android:scaleType="fitStart" />
I tried all I could with different values of android:scaleType but get nowhere.
here is a figure that what I want but when I play with those properties it dose not scale it as I want for example some time it dose not show image width completely and it crops it badly. Another try for example is android:scaleType="fitStart" which cause the width of my images to be fine but the heights are not fit anymore. Any idea?
andriod ImageView attributes does not support the requirement you said just by statically choosing combination of width,height and saleType in the layout xml.
Instead you can do it in the java code by actually finding the width for the image for a given fixed height and setting them programattically to each view in dp(make sure you do the pixel to dp calculation for achieving right output, salable across devices)
If you don't mind an extra dependency, you can do it with Picasso
Picasso.get()
.load(R.drawable.my_image_resource)
.placeholder(R.drawable.ic_image_placeholder)
.error(R.drawable.ic_broken_image)
.resize(0, (int) imagefixedHeight)
// optional:
// .centerInside()
.into(myImageView);

Maintain circle image ratio for button on varying screen resolutions

I have an image button with a background image .png of a circle. I am testing it on different resolution screens and it looks different on every one. Most of them distort the circle shape by stretching it on one dimension.
What is the correct way to handle this? I am familiar with the 3 density levels needed for the highest quality image, but I think the problem is with the layout type attributes on either the image button itself or the parent container.
Snippet from main.xml...
<LinearLayout
android:id="#+id/buttonArea"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:padding="30dp">
<ImageButton
android:id="#+id/button1"
android:background="#drawable/button_inactive"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
removing the layout_weight attribute from the ImageButton fixed most cases, but not all. It seems that the padding is still changing the ratio of the circle. Scale types have no effect. Is it because my image is set as the background and not the src?
I think android:layout_weight="1" in your ImageButton is the cause of this. It will make your ImageButton the same size of your screen, no matter what size the screen is.
Try to remove that attribute. If that doesn't fix your problem, have a look at android:scaleType attribute
<ImageButton
android:id="#+id/button1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:background="#null"
android:src="#drawable/button_inactive"
android:scaleType="centerInside"/>
You can set the scaleType attribute of your ImageView, to one that keeps the aspect ratio of your image.
They all behave slightly different, so you'll have to use one that suits your needs (CenterInside is a good one to start with).
If this doesn't work, you can always specify set heigh/widths of your image (e.g. layout_width="128dp).

standard text and image size in android listview

Hi
I am reading XML from web and displaying them in a listview. The xml contain an image location so I am displaying the image as well along with text.
The image size is 48*48 px as suggested here. But when I see the list view in actual device, the image looks very small. Any idea about how to make that image a little bit big?? is there any standard image and text size??
This is my layout:-
<ImageView
android:id="#+id/logo"
android:layout_gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/name"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#2B2B2B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:layout_toRightOf="#id/logo"
/>
use dip instead of px. when you use dip, it appears more adjustable and good on different screens.
Use an ImageView like this:
Modify the width and height values to your liking, but use the dip measurement. With the scaleType property you can tell the framework to stretch your image.
Try adding
android:scaleType="center"
to your ImageView XML. This will not scale your image, but center it in your view.
It's bad practice on Android to declare layout size by pixels. This page is straight from Google on the subject: http://developer.android.com/guide/practices/screens_support.html I would do something like this
android:layout_height="wrap_content"
android:layout_width="fill_parent"
and see how that looks. Don't forget to test on multiple screen sizes before you finish. You can do that by creating different AVDs for the emulator.
You can use pixels if you really want to, but remember that its going to look different on different devices.
android:layout_height="96dip"
android:layout_width="fill_parent"

Expand clickable area of an ImageView by using padding?

I have an ImageView, and I want it to be clickable. The image itself is only 32x32 pixels, but I want the clickable region to be larger so it's easier to hit. I was hoping I could set the layout margin values to increase the size of the imageview's clickable area:
<ImageView
layout_width="32dip"
layout_height="32dip"
layout_margin="20dip" />
That doesn't work, what I could do is just recreate the image resource to have a larger transparent region around it. That's a bit annoying because it's hard to tweak if I ever need to change the clickable region. It's not just a single png either, it's a statelistdrawable so I have to resize 3 different pngs if I ever need to tweak the clickable area.
Anything else I can do?
Thanks
Use padding. layout margins are used if for inserting space outside the boundary of the view.
for equal padding on all sides
<ImageView
android:padding="20dip" />
or to set the padding on each side
<ImageView
android:paddingLeft="10dip"
android:paddingRight="15dip"
android:paddingTop="23dip"
android:paddingBottom="12dip" />
Hope that helps !
Instead of resizing the image (Peter Knego's answer) or increasing padding (Saimon's answer) I suggest to set the minWidth and minHeight attributes in combination with scaleType="center":
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="40dp"
android:minHeight="40dp"
android:scaleType="center"
android:src="#drawable/your_image" />
That makes sure that small images have at least a size of 40x40dp while the image is not up-scaled if it's smaller. If one day the image is going to be replaced with another image larger than minWidth and minHeight it will not grow larger than 40x40dp as it gets downscaled. Thus a minimum clickable dimension of 40x40dp is always guaranted and the image is always displayed nicely.
Suggestions (never tried it myself):
Create ImageView as large as you want than put image into it without scaling
ImageView.setScaleType(ImageView.ScaleType.CENTER).
Brute force approach: create new larger png that has original image centered and the rest of it is transparent.
Use like below; where width/height is your touch radius
<ImageView
android:id="#+id/editProfile"
android:layout_width="32dp"
android:layout_height="32dp"
android:padding="8dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/ic_edit"
android:scaleType="fitCenter"
/>
In the above code, I wanted my src size to be viewed as 24dp width/height. And touch radius of 32dp. so I had used padding of 8dp.

Categories

Resources