I create a new ImageButton via the "Resource Chooser" and I end up with this code:
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginLeft="14dp"
android:layout_marginTop="94dp"
android:src="#drawable/mybutton" />
Now, I want to make that button way bigger, so I do the following changes:
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginLeft="14dp"
android:layout_marginTop="94dp"
android:src="#drawable/aries" />
I also change the android:src="#drawable/mybutton" to android:background="#drawable/mybutton" to get rid of the grey button around my image and stretch the image instead.
Problem:
My problem is that when stretching out the image it still uses the file from the drawable-mdpi (as it should), so I end up with a stretched low resolution image. If I change the image from the drawable-mdpi folder with a high resolution one it looks great, but I am pretty sure that this is not the best solution out there as I would have to put an image with bigger resolution in each folder.
Sound like what you need is a Nine-Patch Drawable.
A NinePatch is a PNG image in which you can define stretchable regions that Android scales when content within the View exceeds the normal image bounds. [...] when the View grows to accomodate the content, the Nine-Patch image is also scaled to match the size of the View.
Try Setting the background to null:
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/ic_launcher" />
Related
I am using ImageButton in my custom bottom navigation. And I have set icons of 32*32 size as src to them.
here is xml
<ImageButton
android:id="#+id/bottom_nav_profile"
android:layout_width="match_parent"
android:layout_height="50sp"
android:layout_weight="1"
android:src="#drawable/btm_nav_user_act"
android:text="Profile"
android:adjustViewBounds="true"
android:scaleType="center"
android:background="#drawable/bottom_nav_bg"
android:textColor="#000"/>
When I view it on my phone they look pixelated. I'm fine them being smaller but I dont want them to be pixelated. How can I make adujustment so that they look sharper.
Here are the icons
It seems that your image is very small to begin with, you have 2 options:
Remove the hardcoded height (50dp), your original image may be smaller than this, forcing its height may be reducing its quality on certain devices.
Have the same drawable you're using but for different phone densities. I'm guessing you only have one version of your drawable image in the drawable folder, on certain phones with certain densities, this image may display badly (just as it may be displayed just fine on certain other phones).
Lets make custom Image Button with linear layout and imageview
<LinearLayout
android:id="#+id/id"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/btm_nav_user_act"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile"
android:textColor="#000"/>
</LinearLayout>
Now use linear layout as a button.
How can I prevent the ImageButton from stretching the image?
Using this snippet:
<ImageButton
android:src="#drawable/prediction"
android:background="?android:attr/selectableItemBackground"
android:gravity="center_horizontal"
android:scaleType="centerInside"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:contentDescription="some description"
android:text="#string/predict"
local:MvxBind="Click ExecuteQueryCmd" />
I get the following effect:
Image is 64x64 png.
All I want is for the ImageButton to respect native width and length, hence resolution, of the image it is being assigned. Recommendations based on a similar post is to use a Button, set its background to an image then use custom selectors but this sounds like a hack. Or is this the proper way to do this?
It work for me with
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:contentDescription="some description"
android:scaleType="centerInside"
android:src="#drawable/ci_icon"
android:text="#string/predict"
local:MvxBind="Click ExecuteQueryCmd" />
Put your image in android:src attribute
Set the width of the ImageButtons to fill_parent and use scaletype fitStart for the images that hug the left margin, and fitEnd for the ones on the right. Should do the trick, at least as far as your example image goes. You may have some spacing issues if the proportional width of the images exceed the screen width, but it should work for you.
Do this:
<ImageButton
android:src="#drawable/prediction"
android:background="?android:attr/selectableItemBackground"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:contentDescription="some description"
android:text="#string/predict"
local:MvxBind="Click ExecuteQueryCmd" />
I have a 9-patch image and what I want to do is to use it to "seperate" the adMob ad from the app. I want to draw a light blue line exactly where the ad is.
This is the 9-patch:
I added it to layout as ImageView and this is the XML code:
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/adView"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:src="#drawable/ab_transparent_example" />
And this is the result:
But what I want to get is the 9-patch to take the size of the screen, to make a full blue line from left to right of the screen. I've never worked with 9-patch before, what am I doing wrong?
The diagram below is a pretty good visual explanation of what a 9-patch is. As for your specific problem, you should set the 9-patch as the background to the image.
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/adView"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#drawable/ab_transparent_example" />
First you need to know is that 9-patch is image as any other, regarding layout placing it. The difference is in way 9- patch image scale.
In this situation, try to set layout_width to "fill_parrent" or "match_ parent"
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/adView"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:src="#drawable/ab_transparent_example" />
I am trying to achieve something that on the face of it I thought would be easy but have researched loads and still struggling.
I am setting an ImageView that will act as a background where parts of the image are areas that buttons should sit on top. For example the background looks a bit like this:
This is set to fitCenter so that it scales with the correct aspect ratio on all screen types like this:
Now I am trying to place ImageButtons over the white areas like so:
I am trying this by aligning each Imagebutton to the top left of the background imageView and then setting margins to aset it in the right place. This works for some screen types, but when the background gets resized because the device is smaller than the background image for example I am finding that the buttons on top dont resize with it so I get the effect like the following:
Here is the type of layout I am trying (with just one button here):
<RelativeLayout android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/outer" xmlns:tools="http://schemas.android.com/tools">
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/imageView1" android:src="#drawable/no_pills_pane" android:scaleType="fitCenter" android:adjustViewBounds="true" android:layout_centerVertical="true" android:layout_centerHorizontal="true"/>
<ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/imageButton1" android:src="#drawable/pill_pink" android:scaleType="fitXY" android:background="#null" android:layout_marginTop="50dp" android:layout_marginLeft="44dp" android:layout_alignTop="#id/imageView1" android:layout_alignLeft="#id/imageView1"/>
</RelativeLayout>
I'm sure there must be an easier way to go about this, any help would be greatly appreciated.
Thanks
Simon
If all you need is to center ImageButton within ImageView, simply set alignBottom, alignLeft, alignRight, alignTop to ImageView
android:layout_alignBottom="#+id/imageView1"
android:layout_alignLeft="#+id/imageView1"
android:layout_alignRight="#+id/imageView1"
android:layout_alignTop="#+id/imageView1"
I have a layout problem, I want an image to be in the middle of two buttons. So I created the two buttons and defined a weightSum of 1. I then set the weight of the two buttons to .40 and the image to .20 so that the image would be a small logo in the middle of the two buttons. The problem is the image is stretched out and the buttons are pushed aside.
<Button
android:id="#+id/buttonClear"
style="#style/ButtonText"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".40"
android:background="#drawable/custombuttonred"
android:text="#string/clear"
android:textSize="50sp" />
<ImageView
android:id="#+id/myimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".20"
android:src="#drawable/imageid" />
<Button
android:id="#+id/buttonBackspace"
style="#style/ButtonText"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".40"
android:background="#drawable/custombuttonred"
android:text="#string/backspace"
android:textSize="20sp" />
I had also faced the same issue. I used the button insted of ImageView and set background image to it.
This code: http://pastebin.com/3P9BWEcQ
Works well for me
I think you could use dp for resizing the image for both height & width.Say if you ar image is in original size of 400px width & 200 px height,just resize according to the screen size i.e if you are device in which you are testing currently could only hold half of the image size then android:layout_width="200dp" android:layout_height="100dp".If you follow resizing using dp image will get resized to both the smaller & larger device you are using.Make sure that when you have any larger devices screen than the one currently you are testing,just try to have the image as large as possible to get the best results.