Android Custom Button

Android Custom Button

In this Android Tutorial for beginners we are going to see about android basic widgets. We have already seen about how to display words using TextView, changin its color, face properties, setting up action listener and background for our button, and using Toast to display a notification in the previous tutorials. But now we are going to discuss about Android ImageButton – a dedicated button for image usage.

Image Button is a subclass of TextView, so everything discussed in the preceding section in terms of formatting the face of the button still holds.

The properties are already seen in the previous Android TextView Tutorial.

ImageButton — Shows a button having an image (rather than text) which can be pressed or even clicked by the user. Automatically, an ImageButton appears like a normal Button, using the regular button background which changes colour throughout various button states. The image on top of the button is actually determined possibly by the android:src attribute within the XML component or maybe through the setImageResource(int) method.
    <ImageButton
        android:id="@+id/pinkBtn"
        android:layout_width="112dp"
        android:layout_height="30dp"
        android:contentDescription="@string/desc"
        android:src="@drawable/pink_btn" />

Below you can find the description of each properties.

  • android:id=”@+id/pinkBtn” — Unique Id of the Button used to refer later in our java code.
  • android:layout_width=”112dp” — Width of the Button [dimension]
  • android:layout_height=”30dp” — Height of the Button [dimension]
  • android:contentDescription=”@string/desc” — You are required to set a description text string for the ImageView.
  • android:src=”@drawable/pink_btn” — the src is the image source which you need to display.


android button

For this tutorial you need to create Three ImageButton. Paste the following code in your main.xml layout button, and play with the property values till you get your desire result.

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >
    <ImageButton
        android:id="@+id/pinkBtn"
        android:layout_width="112dp"
        android:layout_height="30dp"
        android:contentDescription="@string/desc"
        android:src="@drawable/pink_btn" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <ImageButton
        android:id="@+id/blueBtn"
        android:layout_width="112dp"
        android:layout_height="30dp"
        android:contentDescription="@string/desc"
        android:src="@drawable/blue_btn" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <ImageButton
        android:id="@+id/yellowBtn"
        android:layout_width="112dp"
        android:layout_height="30dp"
        android:contentDescription="@string/desc"
        android:src="@drawable/yellow_btn" />
</LinearLayout>
AndroidImageButtonActivity
package com.vimaltuts.android.androidwidget;
import android.R.anim;
import android.R.color;
import android.R.layout;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class AndroidImageButtonActivity extends Activity {
    private ImageButton pink;
	private ImageButton blue;
	private ImageButton yellow;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        pink=(ImageButton)findViewById(R.id.pinkBtn);
        blue=(ImageButton)findViewById(R.id.blueBtn);
        yellow=(ImageButton)findViewById(R.id.yellowBtn);
        pink.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				 findViewById(R.id.myLayout).setBackgroundColor(Color.parseColor("#8d395b"));
			}
		});
        blue.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				 findViewById(R.id.myLayout).setBackgroundColor(Color.parseColor("#3b597a"));
			}
		});
        yellow.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				 findViewById(R.id.myLayout).setBackgroundColor(Color.parseColor("#e09a00"));
			}
		});
    }
}


android button

  • Create three ImageButton local field and set reference to the corresponding ImageButton by using the findViewById.
  • Set onClickListener to each ImageButton and place the desire code inside it’s onClick() method.
  • Here we are going to change the backgound color of the layout screen as the button is clicked.

To set BackgroundColor to our current layout from the java code we need to set id to our layout in the xml file (main.xml).


android button

To be able to point out the various button states (focused, selected, and so on.), you are able to specify an alternative graphic for every state just like the one we performed in the previous tutorial Android Button Tutorial E.g., A simple way to get this done is by using an XML drawable “selector.”

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:drawable="@drawable/button_pressed" /> <!-- pressed -->
     <item android:state_focused="true"
           android:drawable="@drawable/button_focused" /> <!-- focused -->
     <item android:drawable="@drawable/button_normal" /> <!-- default -->
 </selector>

Save the actual XML document within your project res/drawable/ folder and after that reference it as being a drawable for your ImageButton. Android will certainly on auto-pilot replace the graphic depending on the status of your button along with the related graphics described within the XML.

The arrangement of the components is extremely important because they’re evaluated in order. For this reason the actual “normal” button graphic shows up last, mainly because it is only going to be used right after android:state_pressed as well as android:state_focused have together evaluated false.

In this Android Custom Button Tutorial we have seen about the Image Button from basic to advance, from setting background image to onclick listener. In the next tutorial we are going to see about the CheckBox.

6 comments

  1. I’m new in Android. Thank you! Great! You help me!

  2. very useful tutorial..
    thanx…….

  3. Very nice Tutorials we are providec thanx.

  4. i needed to use Scalable custom buttons for my new project.
    its exactly what i was looking for.

    thanx

  5. Awesome tutorial for customized button

Leave a Reply

Scroll To Top

Foolow Me on Google Plus

VimalTuts on Twitter
62 people follow VimalTuts
meoluoi_abhijeetLukus_Q7silly_piminkyuuuMillerHeengmomanvedpawar