How to remove the square border from a image button - android

I have an image button shape like a 'star' and i what the clickable area to be only the 'star' but instead of that i have a transparent square clickable area around the 'star', can anyone please tell me how to remove the square bcoz the next 'star' it's close and the square overlaps the first 'star'.
thank you so much
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:layout_weight="1"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="190dp"
android:layout_marginBottom="180dp">
<ImageButton
android:id="#+id/star"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="#drawable/star"
/>

Instead of ImageButton you can user ImageView and make in clicable.

Set
android:background=#android:color/transparent
and insert
android:src=#drawable/star
Or just use an ImageView instead, because you can have OnClickListener on both, therefore they could also function as described in your question.

You can use:
android:background="#null"
in the imageButton tag.

Related

Buttons background turns to blue color (not same as original)

my problem seems to be easy but I have no idea how to solve it.
I have a few buttons and all of them have a background image. The problem is, all images turns into blue color
It looks like this:
Original photo:
Here's a part of my xml code:
<Button
android:id="#+id/memory_button1"
android:layout_weight="1"
android:maxWidth="30dp"
android:maxHeight="30dp"
android:onClick="#{()->viewModel.showTile(1)}"
android:background="#{viewModel.button001}">
</Button>
Icons url:
https://www.flaticon.com/packs/restaurant-225
Better approach to use ImageView as button with ripple click effect would be
<ImageView
android:id="#+id/memory_button1"
android:layout_weight="1"
android:maxWidth="30dp"
android:maxHeight="30dp"
android:background="?selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:onClick="#{()->viewModel.showTile(1)}"
android:src="#{viewModel.button001}"/>
Also you can even wrap it in a CardView
If you want to have the only image in your button. you can replace it with ImageView so that you can have full control of how the image is being drawn.
<ImageView
android:id="#+id/memory_button1"
android:layout_weight="1"
android:maxWidth="30dp"
android:maxHeight="30dp"
android:onClick="#{()->viewModel.showTile(1)}"
android:src="#{viewModel.button001}">
</ImageView>
You will option for scale type for ImageView's so that you can render image without stretching it.

Drawable inside Button is getting enlarged

For some reason, I need to use a button with both icon and text. So I can't use ImageButton and thus I go for a solution to set the drawableTop of a normal Button.
The Button size is 140dp in square shape (please refer to the screenshot below)
The I was planning to use was 125x125 px and the asset itself is clear and crisp.
However, this 125px asset somehow being enlarged by the button, like the screenshot shown below.
The device is an xhdpi device.
As you can see, the icon inside the square button is blurry which looks like being somehow enlarged
Here I pasted my button XML
<Button
android:id="#+id/button_call_us"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_margin="#dimen/main_menu_button_margin"
android:adjustViewBounds="true"
android:background="#drawable/custom_button"
android:drawablePadding="-20dp"
android:drawableTop="#drawable/button_call_us_icon"
android:lines="2"
android:paddingTop="0dp"
android:scaleType="fitXY"
android:text="CALL US"
android:textColor="#color/white"
android:textSize="6pt" />
The android:background="#drawable/custom_button" is the purple colour background without any patterns.
The android:drawableTop="#drawable/button_call_us_icon" is the icon.
Except android:scaleType="fitXY", I have also tried centerInside and other options, still not getting the ideal result.
My question is:
Why the drawable inside a Button being enlarged? Is there any way to stop it?
Thanks
Since it's a button, and you want a drawable inside the button. Your probable solution would be to use an ImageButton which actually implements drawable properties. You can then call android:scaleType="fitCenter" and set some padding too
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/android_button" />
However, to get more freedom in terms of desing, you could use a simple layout instead of the button, something on the lines of this, just treat the LinearLayout as you would with a button, in terms of adding the onclicklistener:
<LinearLayout android:orientations="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:background="#CCC"
android:padding="8dp"
android:gravity="center">
<ImageView android:src='#drawable/ic_launcher'
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:text="Lorem Ipsum"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
Try this code
Drawable img = getResources().getDrawable( R.drawable.img );
img.setBounds( 0, 0, (int)(img.getIntrinsicWidth()),
(int)(img.getIntrinsicHeight()) );
button.setCompoundDrawables(null, img, null, null);

ImageButton behind ImageView and offset

So I'm working on making a design for my app and I need to put a imagebutton directly below an imageview. But since my imageview has a border around it with a drop shadow I need to hide (shift up) maybe 10 pixels of my imagebutton behind my imageview. Here is a quick drawing of what I want.
I hope that makes sense. I've been messing around with all kinds of different arrangements but I cant get what I want. Any help would be greatly appreciated.
First of all use a RelativeLayout. Add first the ImageButton and then the ImageView, in this way the ImageView will be on top of your ImageButton. Then you should set on your ImageButton the following :
<ImageButton
....
android:layout_below="#id/ImageViewId"
android:layout_marginTop="-10px" />
Use relative layout as said. Refer
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
for more positions and easy design.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Imageview"
android:layout_marginTop="-16dp" />
<ImageView
android:id="#+id/Imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:adjustViewBounds="true"
android:contentDescription="#string/description_logo"
android:src="#drawable/imagename" />
</RelativeLayout>

Android ImageButton: image with tranparent background

i want to write an application where the user can change the color scheme. I have Imagebuttons and the images on the buttons are pngs and have transparent background. but when i place them on the buttons they are not transparent anymore. could anybody help me with some code?
thanks!
Anne
Are you ensuring that android:background="#null" is set? Otherwise you'll have the gray button background. For example:
<ImageButton
android:background="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_transparent_png"
/>
write your layout for ImageButton as:
<ImageButton
android:src="#drawable/your_image"
android:id="#+id/your_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"/>
Hope this helps!

Android ImageButton with Image and Text

I want to have a button that has an image on top and some text on bottom. Both the image and text are decided during runtime, so I want to be able to combine ImageButton's setImageBitmap and Button's setText for each button.
Any ideas?
Thanks
Chris
Surround both image Button and text View code in .... to override text on image button's background. i.e:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="69dp"
android:layout_marginTop="96dp"
android:background="#drawable/icon"
android:scaleType="fitXY" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Droid"
android:textColor="#ff0000"
android:textSize="24dip" >
</TextView>
</FrameLayout>
I finally found what I was looking for:
setCompoundDrawablesWithIntrinsicBounds lets me assign a bitmap/drawable for a Button, at the same time letting me use setText.
For eg: To set a bitmap image on top of the button, do something like
button.setCompoundDrawablesWithIntrinsicBounds(null, new BitmapDrawable(bitmapimage), null, null);
I don't think there is an easy way to do that.
I would create a custom-component.

Categories

Resources