Add image icon inside to button in android

Create combine image(drawable) with text button in android.

Android buttons are also used to hold image so developer can add image through drawable folder in android applications. With this feature you can add images at left side, right side into the button. So image shows inside the button with smooth gesture. So here is the complete step by step tutorial for Add image icon inside to button in android .

android-project-download-code-button

How to Add image icon inside to button in android.

Image icon can add via android:drawableLeft=”@drawable/your image name” tag . This tag will set image at left side of your button. If you want to add image icon at the right side of button then use this tag android:drawableRight=”@drawable/your image name” . Put all the images in res->drawable folder.
Code for MainActivity.java file.

package com.android_examples.com.buttonwithimageinside;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.buttonwithimageinside.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Button with left side image inside"
 android:drawableLeft="@drawable/ic_launcher" />

 <Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignLeft="@+id/button1"
 android:layout_below="@+id/button1"
 android:drawableRight="@drawable/ic_launcher"
 android:text="Button with right side image inside" />

</RelativeLayout>

Screenshot:

Add image icon inside to button in android

Click Here To Download Add image icon inside to button in android project.

Leave a Reply

Your email address will not be published. Required fields are marked *