Button

Button, ToggleButton, RadioGroup, RadioButton, CheckBox Demo.

/res/layout/main.xml file


<?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/TextView01" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/hello" />

	<CheckBox android:id="@+id/CheckBox01" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="please check me" />
	<ToggleButton android:id="@+id/ToggleButton01"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:text="Toggle" android:textOn="Enabled" android:textOff="Disabled" />

	<RadioGroup android:id="@+id/RadioGroup01"
		android:layout_width="wrap_content" android:layout_height="wrap_content">
		<RadioButton android:id="@+id/RadioButton01"
			android:layout_width="wrap_content" android:layout_height="wrap_content"
			android:text="red"></RadioButton>
		<RadioButton android:id="@+id/RadioButton02"
			android:layout_width="wrap_content" android:layout_height="wrap_content"
			android:text="green"></RadioButton>
		<RadioButton android:id="@+id/RadioButton03"
			android:layout_width="wrap_content" android:layout_height="wrap_content"
			android:text="blue"></RadioButton>

	</RadioGroup>
	<Button android:id="@+id/Button01" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="Clear All" />
</LinearLayout>

Activity file code

package com.androidwork.buttonsdemo;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class ButtonsDemoActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		final Button basic_button = (Button) findViewById(R.id.Button01);
		basic_button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Toast.makeText(getApplicationContext(), "Button clicked",
						Toast.LENGTH_LONG).show();
				TextView tv = (TextView) findViewById(R.id.TextView01);
				tv.setText("Button Clicked");
			}
		});
		final CheckBox cb = (CheckBox) findViewById(R.id.CheckBox01);
		cb.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				TextView tv = (TextView) findViewById(R.id.CheckBox01);
				tv.setText(cb.isChecked() ? "checked" : "unchecked");
			}
		});
		RadioGroup group = (RadioGroup) findViewById(R.id.RadioGroup01);
		final TextView tv1 = (TextView) findViewById(R.id.TextView01);

		group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				Toast.makeText(getApplicationContext(), "inside", 15).show();
				if (checkedId != -1) {
					Toast.makeText(getApplicationContext(),
							"it is : " + checkedId, Toast.LENGTH_LONG).show();
					RadioButton rb = (RadioButton) findViewById(checkedId);
					Toast.makeText(getApplicationContext(),
							"it is : " + rb.getText(), Toast.LENGTH_SHORT)
							.show();
					if (rb != null) {
						if (rb.getText().toString().equals("red")) {
							tv1.setText("You chose: " + rb.getText());
							tv1.setTextColor(Color.RED);
						}
						if (rb.getText().toString().equals("green")) {
							tv1.setText("You chose: " + rb.getText());
							tv1.setTextColor(Color.GREEN);
						}
						if (rb.getText().toString().equals("blue")) {
							tv1.setText("You chose: " + rb.getText());
							tv1.setTextColor(Color.BLUE);
						}
					}
				} else {
					tv1.setText("Choose 1");
				}
			}
		});

		Button b = (Button) findViewById(R.id.Button01);
		b.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				RadioGroup rg = (RadioGroup) findViewById(R.id.RadioGroup01);
				rg.clearCheck();
			}
		});
		
	}
}

output

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s