• About Us
  • Classroom Training
  • Online Training
    • Self Paced Courses
      • HTML and CSS For Beginners (with HTML5)!
      • Javascript for Beginners
      • C Programming: iOS Development Starts Here!
      • Objective C For Beginners
      • PHP MySQL For Beginners
      • Web Development Code Camp
    • Live Instructor Courses
      • HTML and CSS with HTML 5 (Live)
      • Javascript for Beginners (Live)
      • C Programming for Beginners (Live)
      • Objective C for Beginners(Live)
      • Android Development Code Camp (Live)
      • iOS Development Code Camp (Live)
      • HTML5 & Advanced Client Side Development (Live)
      • Actionscript for Beginners (Live)
  • Forum
  • Testimonials
  • Contact Us
    • Actionscript
    • Android
    • C/C++/Objective C
    • HTML/CSS
    • iOS
    • Java
    • Javascript
    • PHP
Home > Android > Android Development: Creating Custom Buttons

Android Development: Creating Custom Buttons

Posted by Mark Lassoff on November 21st, 2011 | No Comments

In this Android development tutorial, Mark will show you how to create a custom button and place it within your Android application. If you’ve been working with Android development, you might be surprised how easy it easy to produce and use a custom button. The custom button graphic can have three states, and Mark will you show you the XML to display the button and Java code to make the button execute a task.

main.xml

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/custom_button" android:id="@+id/btnTv"></Button>
</LinearLayout>

custom_button.xml

1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/btn_over"
   	android:state_pressed="true" />
   <item android:drawable="@drawable/btn_normal" />
</selector>

CustomButtonExampleActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package tv.learntoprogram.customButton;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class CustomButtonExampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        Button btn = (Button)findViewById(R.id.btnTv);
 
        btn.setOnClickListener(new View.OnClickListener() {
 
			@Override
			public void onClick(View v) {
				Toast.makeText(getApplicationContext(), "You pressed the custom button",Toast.LENGTH_SHORT).show();
 
			}
		});
 
    }
}

Leave a Reply

Click here to cancel reply.

Join our mailing list

Free video tutorials, course information and more!

* required

Email Address *

First Name

Last Name:

Email Marketing Software by VerticalResponse
Apple Store Facebook Google+ LinkedIn RSS Feed Twitter YouTube
Provided by The Social Links

Like Us!

Live Online Course

Online event registration powered by Eventbrite

Self Paced Course

What is ?

We train software, web and mobile developers. Our classes geared to the way adults learn are available online, or in-person at your office or training center.

New Android Book

Android Programming Code Camp by Mark Lassoff. Coming in 2012. Finally, a true beginners book on Android programming and development. No Experience required.

Email me when the book is available

On Learning

  • Top Languages Used in Web Development
  • How To Learn a Programming Language: 5 Tips
©LearnToProgram.tv 2012 .All rights reserved.