Wednesday, December 31, 2014

How to use Button onClick event in android

Button onClick event
Android Button class represents a push-button widget. user can press or click on push button to perform an action. Button can display only text or an icon or both text and an icon. we can define button click event handler in the XML layout by adding the android:onClick attribute to the <Button> element.

Button onClick event occurs when user touches the button on touch mode or focuses upon the button with the navigation-keys or trackball and presses the suitable 'enter' key or presses down on the trackball.
activity_main.xml

<LinearLayout
    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:orientation="vertical"
    tools:context=".MainActivity"
    >

    <TextView
        android:id="@+id/text_view"
        android:text="A sample text view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        />

    <Button
        android:id ="@+id/push_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Click Me"
        android:onClick="send_task"
        />

</LinearLayout>
MainActivity.java

package com.cfsuman.me.myapplication5;

import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void send_task(View v)
    {
        TextView tv = (TextView) findViewById(R.id.text_view);
        Button btn = (Button) findViewById(R.id.push_button);
        tv.setTextColor(Color.RED);
        tv.setText("You clicked the button...");
        btn.setText("Click Me Again");
   }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


in the above example code demonstrated us how can we use android button click event. when someone click the push button, the TextView change its displayed text and text color. the push button also change it's text on click event.



Import section on java file
in the MainActivity.java file we imported extra three classes. those are

import android.widget.Button;
import android.view.View;
import android.widget.TextView;

Button click event handling code on MainActivity.java file

public void send_task(View v)
{
 TextView tv = (TextView) findViewById(R.id.text_view);
 Button btn = (Button) findViewById(R.id.push_button);
 tv.setTextColor(Color.RED);
 tv.setText("You clicked the button...");
 btn.setText("Click Me Again");
}

Extra methods on java file
in the MainActivity.java file we uses two methods named setText() and setTextColor(). setText() method allow us to set the text of a widget at run time. by using this method we changes both TextView and Button's displayed text. setTextColor() method allow us to set the text color of a widget programmatically. in this example code we changes the TextView's text color by using the setTextColor() method. setTextColor() method change the text color for all the states normal, selected and focused.


Button click event defined on xml layout file

<Button
 android:id = "@+id/push_button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:text="Click Me"
 android:onClick="send_task"
 />
More android examples