Thursday, 18 May 2017

Working with button

The android.widget.Button is subclass of TextView class and CompoundButton is the subclass of Button class.

                                            android button
There are different types of buttons in android such as RadioButton, ToggleButton, CompoundButton etc.

Button represents a push-button widget,Push-buttons can be pressed, or clicked, by the user to perform an action.

Now we will see how to implement button in our android project.

We can implement button in 3 ways.

                                           1.Normal way.
                                           2.Anonymous inner type.
                                           3.onclick method.

1.Normal way

The generated code for the ui components will be like this: 

activity_main.xml


<?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:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent">

<Button
android:id="@+id/click_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"/>



</RelativeLayout>


Now write the code to implement button in activity.

MainActivity.java


package sravani.com.buttonexample;

import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button click_button;
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); click_button = (Button) findViewById(R.id.click_button); click_button.setOnClickListener(this); }
@Override 
public void onClick(View view) { Toast.makeText(this, "Button is working", Toast.LENGTH_SHORT).show(); }}




2.Anonymous inner type


we need not change anything in activity_main just a small change in Activity class.

package sravani.com.buttonexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button click_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        click_button = (Button) findViewById(R.id.click_button);
        click_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "Button is working", Toast.LENGTH_SHORT).show();
            }
        });
    }

  
}

Here we are passing OnClickListener parameter directly to the setOnClickListener() instead of implementing Activity class.



3.onClick Method



Instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick attribute. For example:  
       
activity_main.xml


<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/click_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="selfDestruct"
android:text="@string/button"/>

</RelativeLayout>

MainActivity.java

package sravani.com.buttonexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button click_button;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        click_button = (Button) findViewById(R.id.click_button);

    }

    public void selfDestruct(View v) {
        Toast.makeText(MainActivity.this, "on click method working", Toast.LENGTH_SHORT).show();
    }
}

To implement button listener we have 3 ways depending on requirement we can use it,for example if user want more buttons 
in the app then oblivously he can move to 1st method.If he needs only one button  then just he implement onclick method or anonymous OnClickListener.

No comments:

Post a Comment