Friday, December 17, 2010

Toggle Button Test

The code has been written in two files
a) main.xml
b) toggleActivity.java

The code is as follows

a) main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ToggleButton 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textOn="Checked"
    android:textOff="Unchecked"
    android:id = "@+id/toggle"/>
    />
</LinearLayout>



b) toggleActivity


package org.example.toggle;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class toggleActivity extends Activity implements OnClickListener{
   
    ToggleButton tb;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tb = (ToggleButton)findViewById(R.id.toggle);
        tb.setOnClickListener(this);
    }
   
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(tb.isChecked())
        {
            Toast.makeText(this, "Toggle Button is checked", Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(this, "Toggle Button is unchecked", Toast.LENGTH_LONG).show();
        }
    }
}



Screenshots :


1) Toggle On 




2) Toggle Off











No comments:

Post a Comment