implements RadioGroup .OnCheckedChangeListener

Description

The following code shows how to implements RadioGroup.OnCheckedChangeListener.

Example

Layout xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout/*from  w  ww .j a  v a2s  .  com*/
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <RadioGroup android:id="@+id/orientation"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5px">
        <RadioButton
            android:id="@+id/horizontal"
            android:text="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <RadioButton
            android:id="@+id/vertical"
            android:text="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </RadioGroup>
    <RadioGroup android:id="@+id/gravity"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5px">
        <RadioButton
            android:id="@+id/left"
            android:text="left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <RadioButton
            android:id="@+id/center"
            android:text="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <RadioButton
            android:id="@+id/right"
            android:text="right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </RadioGroup>
</LinearLayout>

Java code


package com.java2s.myapplication3.app;
//w  w  w  .  ja  v a  2 s.  c om
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.text.TextWatcher;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.EditText;

public class MainActivity extends Activity
        implements RadioGroup.OnCheckedChangeListener {
    RadioGroup orientation;
    RadioGroup gravity;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_main);

        orientation=(RadioGroup)findViewById(R.id.orientation);
        orientation.setOnCheckedChangeListener(this);
        gravity=(RadioGroup)findViewById(R.id.gravity);
        gravity.setOnCheckedChangeListener(this);
    }

    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (group==orientation) {
            if (checkedId==R.id.horizontal) {
                orientation.setOrientation(LinearLayout.HORIZONTAL);
            }
            else {
                orientation.setOrientation(LinearLayout.VERTICAL);
            }
        }
        else if (group==gravity) {
            if (checkedId==R.id.left) {
                gravity.setGravity(Gravity.LEFT);
            }
            else if (checkedId==R.id.center) {
                gravity.setGravity(Gravity.CENTER_HORIZONTAL);
            }
            else if (checkedId==R.id.right) {
                gravity.setGravity(Gravity.RIGHT);
            }
        }
    }
}
implements RadioGroup .OnCheckedChangeListener




















Home »
  Android »
    Android UI »




UI Basics
Action Bar
Animation
Button
Canvas
CheckBox
Clock Date Picker
Dialog
EditText
Event
Fragment
Gesture
GridView
ImageView
Layout
ListView
Map
Menu
Model
OpenGL
ProgressBar
RadioButton
Spinner
Tab
TextView
Thread
Toast
Video
View
WebView