Android App Development With AndroidCource: single item selection.
Showing posts with label single item selection.. Show all posts
Showing posts with label single item selection.. Show all posts

Monday, 21 August 2017

RadioButton Example in Android

RadioButtonActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:id="@+id/activity_radio_button"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:paddingBottom="@dimen/activity_vertical_margin"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              android:orientation="vertical"
              tools:context="com.bhaumik.programmingstudy.RadioButtonActivity">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Select Fruits Item"
            android:textAppearance="?android:textAppearanceLarge"
            android:layout_gravity="center_horizontal"
    />
    <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/apple"
                android:text="Apple"
                android:textSize="20dp"
                android:layout_gravity="center_horizontal"
                android:onClick="SelectFruit"
        />
        <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/orange"
                android:text="Orange"
                android:textSize="20dp"
                android:layout_gravity="center_horizontal"
                android:onClick="SelectFruit"
        />
        <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/cheery"
                android:text="Cheery"
                android:textSize="20dp"
                android:layout_gravity="center_horizontal"
                android:onClick="SelectFruit"
        />
    </RadioGroup>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hello"
            android:id="@+id/tv_radio_result"
            android:textAppearance="?android:textAppearanceLarge"
            android:layout_gravity="center_horizontal"
            android:enabled="false"/>

</LinearLayout>
RadioButtonActivity.java
package com.bhaumik.programmingstudy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

public class RadioButtonActivity extends AppCompatActivity {

    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button);
        textView = (TextView)findViewById(R.id.tv_radio_result);
    }
    public void SelectFruit(View view)
    {
        boolean checked = ((RadioButton)view).isChecked();
        switch (view.getId())
        {
            case R.id.apple:
                if (checked)
                {
                    textView.setText("You Select Apple...");
                    textView.setEnabled(true);
                }
                else
                {
                    textView.setEnabled(false);
                }
                break;
            case R.id.orange:
                if (checked)
                {
                    textView.setText("You Select Orange...");
                    textView.setEnabled(true);
                }
                else
                {
                    textView.setEnabled(false);
                }
                break;
            case R.id.cheery:
                if (checked)
                {
                    textView.setText("You Select Cheery...");
                    textView.setEnabled(true);
                }
                else
                {
                    textView.setEnabled(false);
                }
                break;
        }
    }
}

Ads