Android Button Control

0
471

A Button is a normal push button which is also one of the often used UI control. To use a button at the run time of an application, the user needs to either press it or click it to perform a specific action linked to it.

Also Read: Android AutoCompleteTextView Control

In the above image, you can easily see dial button, cancel button, and OK button. The two of them are encircled so that you can easily identify them.

Various Attributes of Button Control
We have compiled a list of important attributes related to the button control. For the complete list of Button control attributes, you can visit the official Android document. Well, other than attributes, you will also be introduced to the major associated methods. With the help of these methods, you will be able to change the value of all the attributes at the run time.

The following list of Button control attributes is inherited from the android.widget.TextView class.

S.No. Attribute Description
1 android:text * This attribute accepts the text to be displayed on the screen.
2 android:autoText * This attribute lets the user enter text in the provided field.
* It has the ability to auto-correct a few of the common spelling mistakes.
3 android:editable * This attribute depicts that the provided field can accept the input value from the user.
4 android:drawableRight * This attribute accepts drawable as its value which is then be displayed to the right of the text.
5 android:drawableBottom * This attribute accepts drawable as its value which is then be displayed below the text.

The following list of Button control attributes is inherited from the android.view.View class.

S.No. Attribute Description
1 android:id * This attribute accepts the identifier (id) name for the associated view.
2 android:onClick * This is a method which gets invoked whenever the view is clicked.
3 android:visibility * If set, this attribute decides the initial visibility of a view.
4 android:background * This attribute accepts a drawable value which will later be set as the view’s background.
5 android:contentDescription * The value of this attribute is present in the text format which will brief about the content of the view.

To understand the use of above-mentioned attributes, let’s take a look at the following example. Here, you will get to know about the step-to-step procedure for the creation of an Android application by the use of Linear layout and Button control.

Step 1: Start by opening the Android Studio IDE. Create an Android application and save it as myapplication. Keep this app under the com.example.w3school.myapplication package. To understand this step more clearly, take a look at the already explained Hello World Example chapter.

Step 2: Add a click event to the main activity file, which is MainActivity.java. To find this file, look into the src/ directory. The existing content of the file will be containing the fundamental lifecycle callback methods. 

package com.example.w3school.myapplication;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast; 

public class MainActivity extends ActionBarActivity
{
   Button b1,b2,b3;   

   @Override
   protected void onCreate(Bundle savedInstanceState)
{
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);      
      b1=(Button)findViewById(R.id.button);
      b1.setOnClickListener(new View.OnClickListener()
      {
         @Override
         public void onClick(View v)
         {
            Toast.makeText(MainActivity.this,”YOUR MESSAGE”,Toast.LENGTH_LONG).show();
         }
      });
   }
} 

Step 3: Update the activity_main.xml file with the content provided below. You can easily locate this file by tracing the res/layout/ path. Well, in this file, you will be including the required button control. 

<?xml version=”1.0″ encoding=”utf-8″?>
<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:paddingLeft=”@dimen/activity_horizontal_margin”
   android:paddingRight=”@dimen/activity_horizontal_margin”
   android:paddingTop=”@dimen/activity_vertical_margin”
   android:paddingBottom=”@dimen/activity_vertical_margin”
   tools:context=”.MainActivity”>   

   <TextView
      android:id=”@+id/textView1″
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:text=”Button Control”
      android:layout_alignParentTop=”true”
      android:layout_centerHorizontal=”true”
      android:textSize=”30dp” />      

   <TextView
      android:id=”@+id/textView2″
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:text=”W3School”
      android:textColor=”#ff87ff09″
      android:textSize=”30dp”
      android:layout_below=”@+id/textView1″
      android:layout_centerHorizontal=”true” />      

   <ImageButton
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:id=”@+id/imageButton”
      android:src=”@drawable/abc”
      android:layout_below=”@+id/textView2″
      android:layout_centerHorizontal=”true” />      

   <EditText
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:id=”@+id/editText”
      android:layout_below=”@+id/imageButton”
      android:layout_alignRight=”@+id/imageButton”
      android:layout_alignEnd=”@+id/imageButton” />      

   <Button
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:text=”Button”
      android:id=”@+id/button”
      android:layout_alignTop=”@+id/editText”
      android:layout_alignLeft=”@+id/textView1″
      android:layout_alignStart=”@+id/textView1″
      android:layout_alignRight=”@+id/editText”
      android:layout_alignEnd=”@+id/editText” />      
</RelativeLayout> 

Step 4: In strings.xml file, you need to define the new string constants to be used in your application. You can find this file in the res/values/ directory.

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>
   <string name=”app_name”>myapplication</string>
</resources> 

Step 5: Make sure that you leave the AndroidManifest.xml file as it is. The default content of the file will look like the following.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
   package=”com.example.saira_000.myapplication” >      

   <application
      android:allowBackup=”true”
      android:icon=”@drawable/ic_launcher”
      android:label=”@string/app_name”
      android:theme=”@style/AppTheme” >      

      <activity
         android:name=”com.example.guidemo4.MainActivity”
         android:label=”@string/app_name” >      
         <intent-filter>
            <action android:name=”android.intent.action.MAIN” />
            <category android:name=”android.intent.category.LAUNCHER” />
         </intent-filter>      
      </activity>      
   </application>
</manifest>

Step 6: Now, you just need to run your myapplication on the Android Studio IDE. We are assuming that you have already created your AVD during the environment setup. Well, to run this application, just identify the Run button from the toolbar and click on it. The Studio will initiate the process by installing your application on the AVD. If everything falls in the right place then you will be displayed with the final output on the Emulator window.