Android display image on a button programmatically

In Android, there are two ways to display images on buttons. You can set the images to buttons in the xml layout file that you placed the Button widgets and you set the images to buttons programmatically in code. In this tip, I am going to show you how to set the images to buttons programmatically in code.
You can accomplish this task by using the setCompoundDrawablesWithIntrinsicBounds( Drawable left, Drawable top, drawable right, Drawable bottom) method. With the setCompoundDrawablesWithIntrinsicBounds, you can display an image to the left, above, to the right, or below the text of the button.

Now let's start with an example program. You will create a new Android project. Then modify the activity_main.xml file to add four Buttons as shown below.

<LinearLayout 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=".MainActivity"
   android:orientation="vertical"
   android:background="#28def0"
   >

   <Button
  
     android:id="@+id/bt1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="#f8f8f8"
      android:layout_marginBottom="10dp"
    
     >
  
   </Button>
   <Button
  
     android:id="@+id/bt2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="#f8f8f8"
     android:layout_marginBottom="10dp"
     >  
   </Button>
  
   <Button  
     android:id="@+id/bt3"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="#f8f8f8"
     android:layout_marginBottom="10dp"
     >
  
   </Button>
   <Button
  
     android:id="@+id/bt4"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="#f8f8f8"
    
       >
  
   </Button>

</LinearLayout>


In the onStart method of the MainActivity class, you need to write code to make references to the four buttons, add text and images to the buttons. The images are set to the buttons are Drawable type. So you need to create Drawable objects referencing to the images in the res/drawable folder. You can use the getResources().getDrawable() method to do this task. Here is the code written in the onStart method to show an image to left of the text, above, to right of the text, and below the text.

   public void onStart(){
   super.onStart();
   Button bt1= (Button)findViewById(R.id.bt1);
   Button bt2= (Button)findViewById(R.id.bt2);
   Button bt3= (Button)findViewById(R.id.bt3);
   Button bt4= (Button)findViewById(R.id.bt4);
   //set text to buttons
   bt1.setText("Search");
   bt2.setText("Search");
   bt3.setText("Search");
   bt4.setText("Search");
  
   Drawable icon=this.getResources(). getDrawable( R.drawable.iconsearchtr);
   //show icon to the left of text
   bt1.setCompoundDrawablesWithIntrinsicBounds( icon, null, null, null );
   //show icon above the text
   bt2.setCompoundDrawablesWithIntrinsicBounds( null, icon, null, null );
   //show icon to the right of text
   bt3.setCompoundDrawablesWithIntrinsicBounds( null, null, icon, null );
   //show icon below the text
   bt4.setCompoundDrawablesWithIntrinsicBounds( null, null, null, icon );

}

Android-display-image-on-a-button-programmatically

Posted by: Dara | post date: 06-10-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.