Sunday 14 June 2015

Toggle button in android -2

No comments
In this example,we learn use of toggle button in android. the toggle button is a special class to render a button which has only two states,like " On / Off "," 0 / 1 ","Yes / On","True / false" etc.It’s best switching buttons to turn on or turn off a function.let me explorer with you.here i covered two example which is displaying two different state of toggle button

import android.widget.ToggleButton;  is a special class,is allow to implement toggle button.

CREATE NEW PROJECT

Step 1: Write code into activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onToggleClicked"
        android:text="ToggleButton" />

    <TextView
        android:id="@+id/tview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:editable="false"
        android:ems="10" />

    <ToggleButton
        android:id="@+id/toggleButton2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="ToggleButton"
        android:textOff="Turn ON"
        android:textOn="Turn OFF" />

    <TextView
        android:id="@+id/tview2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:editable="false"
        android:ems="10" />


</LinearLayout>

Step 2: Write code into MainActivity.java

package dev.androidapplink.togglebuttonapp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

// create variable
ToggleButton button1;
ToggleButton button2;
TextView tview1;
TextView tview2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// load control
button1 = (ToggleButton) findViewById(R.id.toggleButton1);
button2 = (ToggleButton) findViewById(R.id.toggleButton2);
tview1 = (TextView) findViewById(R.id.tview1);
tview2 = (TextView) findViewById(R.id.tview2);

// Set message
tview1.setText("Button1 is OFF");
tview2.setText("Button2 is OFF");

// Implement state change listener
button2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Log.i("info", "Button2 is on!");
tview2.setText("Button2 is ON");
} else {
Log.i("info", "Button2 is off!");
tview2.setText("Button2 is OFF");
}
}
});
}

// set message according to toggle button state
public void onToggleClicked(View view) {
boolean on = ((ToggleButton) view).isChecked();
if (on) {
Log.i("info", "Button1 is on!");
tview1.setText("Button1 is ON");
} else {
Log.i("info", "Button1 is off!");
tview1.setText("Button1 is OFF");
}
}

}

Step 3: Now Run Your Project:





No comments :

Post a Comment

Follow me Share