Set custom width height image drawable in Android button - android

This is what I want to do: customize the image size because it's just a little too big.
I am creating an Android application and in a button, I have the image and text. Now I want to customize the size of the image to fit on the screen. However, I have a difficulty manipulating the image. If I adjust the width and height, it is of the button's. I wanted to adjust the image size. Is this possible?
Here's the code snippet of the button:
<Button
android:id="#+id/marketBtn"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_weight="1"
android:background="#drawable/unselected_button"
android:drawableTop="#drawable/market_icon_0"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:paddingTop="5dp"
android:text="#string/task_bar_lbl_market"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFFFFF"
android:textSize="10sp" />
Do you have any idea how to do it? Or what are the possible approaches to fulfill this task?

Use nine-patch.
you can set stretchable area and padding area.
http://developer.android.com/tools/help/draw9patch.html

Related

How to use icons in ImageButton without it getting pixelated?

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 to adjust image for custom imagebutton?

I created custom triangle button, from code which was given by wonderful user so, here :
How to create layout with buttons like on this picture? ( BUTTON THAT HAVE ACTUALLY SHAPE OF TRIANGLE, NOT MASK )
And i came in level, for adjusting design. I tried to make 9-patch img, also with normal, but it looks for inner button like this:
And then bump into finding way for adjusting that image into triangle custom button, and found this solution:
Android - Image Button Scale
But what i got is not good for me. Here what i tried :
<FrameLayout
android:layout_width="270dp"
android:layout_height="270dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/custom_border" >
<custom_button.TriangleButton
android:id="#+id/btn_bottom"
android:layout_width="250dp"
android:layout_height="200dp"
android:text="Second Button"
android:textColor="#000"
android:layout_gravity="center_horizontal"
android:gravity="bottom|center_horizontal"
android:padding="13dp"/>
<custom_button.TriangleButton
android:id="#+id/btn_top"
android:layout_width="130dp"
android:layout_height="110dp"
android:text="First Button"
android:textColor="#000"
android:layout_gravity="center_horizontal"
android:gravity="bottom|center_horizontal"
android:layout_margin="55dp"
android:padding="30dp"
android:onClick="callAdditionActivityButton"
android:src="#drawable/triangle"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
</FrameLayout>
I have on mind to create triangle (with my art) in rectangle (normal image) to fit center of rectangle, in way, down_left_corner, center_top, down_right_corner, respectivly created in code. And around triangle make transparent. How to achieve this? Main issue how to adjust picture, second is transparent background...
EDIT: My image's size is: 502 x 302, bit depth 32
MY desired picture for adjusting, i want this circle to be in center in custom button :

android ImageButton do not stretch image

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

Android: layout with imageView re-sizing buttons wrongly

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.

Limit the text width of TextView (Not the background image)

I have a TextView with a background that declared as:
<TextView
android:id="#+id/questionText"
android:layout_width="250dp"
android:layout_height="100dp"
android:gravity="center"
android:textSize="26px"
android:textColor="#FFFFFF"
android:background="#drawable/blue"
/>
the problem is that the background image is like a frame, so i want the
text inside to have a margin from the background image dimensions.
Is this possible?
10X alot,
have a gr8 weekend :)
What you want sounds like padding.
<TextView
android:id="#+id/questionText"
android:layout_width="250dp"
android:layout_height="100dp"
android:gravity="center"
android:textSize="26px"
android:textColor="#FFFFFF"
android:background="#drawable/blue"
android:padding="5dp"
/>
This would put a 5 pixel border around the actual text showing the background through.
Incidentally, layout_margin is also a parameter and is used to put space between the entire view, background included, and other views.
I'd recommend to make 9-patch drawable. It defines where the text can be without specifying padding in the control.

Categories

Resources