Android user Interface design: Basic button

Source: Internet
Author: User

Android user Interface design: Basic button

This article shows you the steps to create a simple button or ImageButton control in your Android application. First, you'll learn how to add a button control to your layout file. Then you'll learn how to handle user clicks on a button in two ways. Finally, we discuss some other available features of the button controls in Android.

1th Step: Create an Android app

We started by creating Android programs. You usually finish your Android app as usual. Once you have created the project and can run it, decide what screen you want to add the button control to. You may have simply created a new Android project that uses the default activity and layout (main.xml). This tutorial will use this case as an example. Once you have created your Android project, you can continue to study this article.

2nd step: Using the Button control

The Android SDK contains two simple button controls that you can use in your layouts: button (Android.widget.Button) and ImageButton (Android.widget.ImageButton). The functions of these controls are similar so we can discuss them almost together. The difference between the two controls is basically the appearance; the button control has a text label, and the ImageButton uses a drawing image resource instead. A good example of button use should be a simple button with a "save" text tag. A good example of the use of ImageButton might be a collection of music player buttons, including play p, pause and stop.

Here is an example screen that includes a button control (left) and a ImageButton control (to the right).

The Android SDK also contains some other less-known class-button controls that inherit from the two basic button types above, including Compoundbutton,radiobutton,togglebutton, and Zoombutton. To learn more about these controls, view the Android documentation. You can also create custom controls by inheriting the appropriate classes and implementing the control behavior.

3rd Step: Add a Button control to the layout

The button control is typically used as part of the active layout resource file. For example, to add a button control to the Main.xml layout resource associated with your program, you must edit the layout file. You can use the Layout Resource Designer for Eclipse, or edit the XML directly. Controls such as buttons can also be dynamically created by programs and added to your screen at run time. Simply use its class to create the appropriate control and add it to the layout in your activity.

To add a button control to the layout resource file, open the/res/layout/main.xml layout file, which is part of your Android project. Click the LinearLayout (or parent layout control, such as Relativelayout or framelayout) for which you want to add a button control. In Eclipse, you can click the parent layout in the Outline tab, and then use the Green Plus button to add a new control. Select the control you want to add--in this case, the button control.

To configure the appearance of the button control, select the control and adjust the control's properties by changing the property value in the property label. Here are some of the special attributes you'll want to know:

    • make Use the id attribute to give a button or ImageButton a unique name;
    • make Sets the text to be displayed on the button control with the literal property;
    • Use the SRC attribute to set the picture to be displayed on the ImageButton control.

The following is the content of the layout resource file used to generate the screen shown in the previous paragraph. It consists of three controls.

Relativelayout controls on the organization screen, which is two child controls, a button and a ImageButton, as follows:

<preName= "Code"><?xmlVersion= "1.0" encoding= "Utf-8"?> <relativelayoutXmlns:android= "Http://schemas.android.com/apk/res/android" android:layout_width= "Fill_parent" Android:layout_ height= "fill_parent" android:gravity= "center"> <buttonAndroid:id= "@+id/button01" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "@string/hello" android:minheight= "92DP" android:textsize= "22DP" android:onclick= "Onmybuttonclick"></Button> <imagebuttonAndroid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:src= "@drawable/skater" Android: Id= "@+id/imagebutton01" android:layout_torightof= "@+id/button01"></ImageButton> </RelativeLayout> </pre>
4th Step: Handling clicks

Now, if you run your program, the button controls are displayed, but if you click on them there will be no response. Now it's time to handle the Click event on the control. There are several ways to do this.

Let's start with a simple approach. The button and the ImageButton control have a property called OnClick (called "On Click" in the Properties panel). You can use this property to set the method name to handle the click event, and then implement this method in your activity. For example, you can set your Button control property to Onmybuttonclick. In XML, this property looks like this:

android:onclick= "Onmybuttonclick"

Then, in your activity class, you need to implement this method. It should be a public void method with a single parameter (a View object). For example, the following button click Implements a message box that is generated on the screen when the button control is clicked:

 Public void Onmybuttonclick (view view) {Toast.maketext (This, "button clicked!", Toast.length_short). Show ( ); }

When you click on the button control, the Onmybuttonclick () method is called and a message is displayed on the screen. What you can do with button buttons depends on yourself. Shows how the message is displayed when the button is clicked:

5th step: Deal with clicks--Implement Onclicklistener

Another way to implement click event Processing is to use the Setonclicklistener () method to register a new View.onclicklistener with your button control. Instead of setting the on Click property of the button control in your layout resource to a method that you must implement, you can do these things dynamically in your activity. While this may seem like a lot of extra code to write, it's important to understand at least because clicking on some controls is not the only event that needs to be handled. We're going to show you a program that applies other events, such as a long press.

To use this method, you must update your activity class to register the control click event. This is usually done through the OnCreate () method of your activity. Use the Findviewbyid () method to locate the control and then use its Setonclicklistener () method to define the behavior when it is clicked. You will need to implement the OnClick () method of the interface yourself. For example, the following code (in the active OnCreate () method) registers a click processor for our ImageButton control.

ImageButton Myimagebutton = (ImageButton) Findviewbyid (R.ID.IMAGEBUTTON01); Myimagebutton.setonclicklistener (newpublicvoid OnClick (View v) {Toast.maketext ( Basicbuttonactivity. This, "ImageButton clicked!", Toast.length_short). Show (); } });

Similarly, you can use this technique to implement long-press-click Processing by using the Setonlongclicklistener () method of the control.

Summarize

Button controls are often used in Android programs. In this quick tutorial you learned how to create two different Android button controls: button and ImageButton. You also learned several ways to implement button click event handling for these types of button controls. (Note: The original Rockux–web click here to view English.) )


Http://mobile.51cto.com/design-254382.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.