Android ImageButton: image with tranparent background - android

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!

Related

how to set image as background button

I am new in Android development.I created a simple project with a simple button.I'd like to set image as background (see image). I've used android:src="#drawable/id_image" and android:background="#drawable/id_image2" but the image does not appear on the entire surface of button.Any help would be appreciated
Android, you can use android.widget.ImageButton to display a normal “Button“, with a customized background image.
<ImageButton
android:id="#+id/top_bar_btn_right_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/ic_filter" />
You might use android:src and android:background for adding two images.
when you add your button , you just go to the menu of properties and choose background / drawable and choose your image
You can try something like this
<RelativeLayout
android:layout_width="250dp"
android:layout_height="400dp"
android:background="your_background_goes_here"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:centerInParent="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="your_icon_above_background"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text_for_your_image"/>
</RelativeLayout>

How to remove the square border from a image button

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.

Android - How do I remove the ugly borders on imagebuttons?

http://i.imgur.com/WCGjcOe.png
Pretty straight up question - How do I remove those grey borders?
The pictures (.png files) are just the stars, text and the white around it. Then when I add them as imagebuttons they add this ugly grey borders that I really don't want.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/starButton1"
android:src="#drawable/star_pink_1"
android:onClick="starButton1Clicked"
android:layout_below="#+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
You have to define the xml attribute background to Your ImageButton:
android:background="#android:color/transparent"
what You see is the original button, Your src is just overlayed.
try this
android:background="#null"
this will make your imagebutton with no background.
Both codes work fine for your query :
android:background="#null"
OR
android:background="#android:color/transparent"
Use this
style="?android:attr/borderlessButtonStyle"

how change button Icons in android?

i wish to adjust icons in my application that i'm about to devolp.
this is the link of the tutorial that i'm folowing .
"https://www.youtube.com/watch?v=lkadcYQ6SuY"
this is the example given and created in the tutorial .
now i don't have the reputation yet to post pictures to show you what i want BUT
simple i want arrows for back and forward (custom created) instead of textbuttons
anyone who can help me ... ?
You can use ImageButton, put your icon into drawable folder(if it doesnt exist, create it), and set it to your imageButton
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/yourImgName" />
You can create a simple Button and set its Background for picture.
<Button
android:id="#+id/button"
android:background="#drawable/help"
android:contentDescription="#string/help"
android:gravity="bottom"
android:onClick="showHelp" />
Or you can create ImageButton :
<ImageButton
android:id="#+id/sync"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:contentDescription="#string/sync"
android:background="#android:color/transparent"
android:src="#drawable/sync" />
try the below code
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/your_image_name"/>

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);

Categories

Resources