Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project ToggleButtonDemo.
2.) Put the following code snippet in res/layout/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">
<TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
<ToggleButton android:text="ToggleButton" android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="yello" android:textOff="red">
</ToggleButton>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
<ToggleButton android:text="ToggleButton" android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="yello" android:textOff="red">
</ToggleButton>
</LinearLayout>
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. ToggleButtonDemo. Enter following information:
Project name: ToggleButtonDemo
Build Target: Android APIs2.1
Application name: ToggleButtonDemo
Package name: com.app.ToggleButtonDemo
Create Activity: ToggleButtonDemo
On Clicking Finish ToggleButtonDemo code structure is generated with the necessary Android Packages being imported along with ToggleButtonDemo.java. ToggleButtonDemo class will look like following:
package com.app.ToggleButtonDemo; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ToggleButton;
public class ToggleButtonDemo extends Activity implements OnClickListener
{
ToggleButton tg;
TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.tv);
tv.setBackgroundColor(0xfff00000);
tg = (ToggleButton) findViewById(R.id.toggleButton1);
tg.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if((tg.isChecked()))
{
System.out.println("checked");
tv.setBackgroundColor(0xffffff00);
}
else
{
System.out.println("Unchecked");
tv.setBackgroundColor(0xfff00000);
}
}
}
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ToggleButton;
public class ToggleButtonDemo extends Activity implements OnClickListener
{
ToggleButton tg;
TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.tv);
tv.setBackgroundColor(0xfff00000);
tg = (ToggleButton) findViewById(R.id.toggleButton1);
tg.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if((tg.isChecked()))
{
System.out.println("checked");
tv.setBackgroundColor(0xffffff00);
}
else
{
System.out.println("Unchecked");
tv.setBackgroundColor(0xfff00000);
}
}
}
This comment has been removed by the author.
ReplyDelete