Pages

Thursday, June 30, 2016

Android Toggle Example

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToggleButton"
        android:checked="false" />

    <ToggleButton
        android:id="@+id/toggleButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="@string/toggle_turn_on"
        android:textOff="@string/toggle_turn_off"
        android:checked="true" />

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

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="72dp">

        <ToggleButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Off"
            android:id="@+id/ton"
            android:checked="false" />

        <ToggleButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/On"
            android:id="@+id/toff"
            android:checked="true" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click"
            android:id="@+id/click" />
    </LinearLayout>

</LinearLayout>
MainAcivity.java
package in.kantapp.androidtoggleexample;


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

public class MainActivity extends AppCompatActivity {
    private ToggleButton t1, t2, tb1, tb2;
    private Button b1, b2;

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

    public void onDisplayeClick() {
        t1 = (ToggleButton) findViewById(R.id.toggleButton1);
        t2 = (ToggleButton) findViewById(R.id.toggleButton2);
        b1 = (Button) findViewById(R.id.btnDisplay);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer result = new StringBuffer();
                result.append("toggleButton1 : ").append(t1.getText());
                result.append("\ntoggleButton2 : ").append(t2.getText());

                Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void onClick() {
        tb1 = (ToggleButton) findViewById(R.id.ton);
        tb2 = (ToggleButton) findViewById(R.id.toff);
        b2 = (Button) findViewById(R.id.click);

        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer onoff = new StringBuffer();

                onoff.append("Toggle 1 is  :  ").append(tb1.getText());
                onoff.append("\nToggle 2 is:  ").append(tb2.getText());

                Toast.makeText(getApplicationContext(), onoff.toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
string.xml
<resources>
    <string name="app_name">Android Toggle Example</string>
    <string name="toggle_turn_on">Turn On</string>
    <string name="toggle_turn_off">Turn Off</string>
    <string name="btn_display">Display</string>
    <string name="On">Light ON</string>
    <string name="Off">Light Off</string>
</resources>

No comments:

Post a Comment