Home > DeveloperSection > Articles > Button Controls in Android Application

Button Controls in Android Application


Android Android 
Ratings:
2 Comment(s)
 3723  View(s)
Rate this:

Button Controls in Android Application

In this article I am going to explain how to create and use a button control in an Android Application. I am going to create three button (New, Save, Print), when a user clicked on the button the toast message will be display.

·         Start a New Project, named ButtonDemo.

·         Open res/values/strings.xml and add a string in the resources tag.

<string name="onClick">onClick</string>

·         Open res/layout/main.xml and add three button as shown below:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="horizontal"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

    <Button android:id="@+id/btnNew"       

              android:layout_width="150px"       

              android:layout_height="wrap_content"

              android:onClick="onClick" />

    <Button android:id="@+id/btnSave"       

              android:layout_width="150px"       

              android:layout_height="wrap_content"

              android:onClick="onClick" />

    <Button android:id="@+id/btnPrint"       

              android:layout_width="150px"       

              android:layout_height="wrap_content"

              android:onClick="onClick" />

</LinearLayout>

·         Open a activity file and add the following code in that activity:

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

 

public class FormActivity extends Activity {

   

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        Button btnNew=(Button)findViewById(R.id.btnNew);

        Button btnSave=(Button)findViewById(R.id.btnSave);

        Button btnPrint=(Button)findViewById(R.id.btnPrint);

        btnNew.setText("New");

        btnSave.setText("Save");

        btnPrint.setText("Print");

    }

    public void onClick(View v) {

    switch(v.getId())

    {

     case R.id.btnNew:

     Toast.makeText(FormActivity.this, "You clicked New", Toast.LENGTH_SHORT).show();                                   

     break;

                    

     case R.id.btnSave:

     Toast.makeText(FormActivity.this, "You clicked Save",Toast.LENGTH_SHORT).show();
    
break;

                          

     case R.id.btnPrint:

     Toast.makeText(FormActivity.this, "You clicked Print",Toast.LENGTH_LONG).show();
    
break;

     }

   }

}

·         Run the application

Your output should look like below:

Button Controls in Android Application

You can click on button, it will display a toast message.


change text color when checkbox is checked

By javed salat on   4 years ago
Hello, This is javed .
I want to change font color of checkbox when checkbox gets checked please help me
I have tried by findById().setTextColor()  but it is giving error of color should be in integer....
so how can i do this ?

change text color when checkbox is checked

By Rohit Kesharwani on   4 years ago
Hi,

You can change the font color of checkbox something like below:

final CheckBox checkbox=(CheckBox)findViewById(R.id.checkbox);
       
        checkbox.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if(((CheckBox)v).isChecked())
                {
                    Toast.makeText(CheckBoxActivity.this, "Selected", Toast.LENGTH_SHORT).show();
                    checkbox.setTextColor(Color.BLUE);
                }
                else
                {
                    Toast.makeText(CheckBoxActivity.this, "Not Selected", Toast.LENGTH_SHORT).show();
                    checkbox.setTextColor(Color.WHITE);
                }
            }
        });

It won't not give any error.

Don't want to miss updates? Please click the below button!

Follow MindStick