I recently learnt how to make a round edged button. But I am able to make buttons only with a plain background. Now, I want to make a button that uses an image file from my computer as it's background. How do I make it?
Asked
Active
Viewed 109 times
0
-
You can check [this](http://stackoverflow.com/questions/4755871/how-to-set-image-button-backgroundimage-for-different-state) out. – AL. Mar 16 '16 at 05:20
3 Answers
0
Use this make a new Drawable Resource file inside your drawable and paste the following code
//For Displaying Transparent Rounder Border to Button or ET or TV
<?xml version="1.0" encoding="utf-8"?><!-- res/drawable/rounded_edittext.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="2dp"
android:color="#30647A" />
<corners
android:bottomLeftRadius="25dp"
android:bottomRightRadius="25dp"
android:topLeftRadius="25dp"
android:topRightRadius="25dp" />
</shape>
and to you button in your XML do this
<Button
android:id="@+id/btn_next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/data_linear"
android:layout_marginTop="@dimen/login_margin_top"
android:background="@drawable/resource_name"
android:text="@string/next_btn"
android:textColor="@android:color/white" />

Rakshit Nawani
- 2,604
- 14
- 27
0
Do this to make image as background of your button:
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
btn.setBackgroundResource(R.drawable.icon);
}
});
Second method: You can define imagebutton in your xml file and give source to it like this:
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/selector" />

Parsania Hardik
- 4,593
- 1
- 33
- 33
0
Your myButton.xml
having android:drawable
as background
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/img1" />
<shape >
<stroke android:width="2dp"
android:color="#FFFFFF"/>
<gradient
android:angle="225"
android:startColor="#DD2ECCFA"
android:endColor="#DD000000"/>
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
</layer-list>
And It's your Button using that drawable as background
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:background="@drawable/myButton"
android:text="OpenContact"
/>

Shree Krishna
- 8,474
- 6
- 40
- 68