Saturday, 8 October 2016

RadioButton

activity_main.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:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   tools:context="com.kiranapp.MainActivity">

 <RadioGroup
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/radiogroup"
    android:weightSum="2"
    android:layout_marginTop="25dp">
<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/radio1"
    android:text="Android"
    android:layout_gravity="center"
    android:checked="false" />
<RadioButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/radio2"
   android:text="IOS"
   android:layout_gravity="center" />
<RadioButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/radio3"
   android:text="PhoneGap"
   android:layout_gravity="center" />
</RadioGroup>
<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Course"
   android:id="@+id/button"
   android:layout_gravity="center_horizontal"
   android:layout_marginTop="39dp"/>
</LinearLayout>

MainActivity.java :-

package com.kiranapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    RadioGroup radioGroup;
    RadioButton radioButton;
    Button button;
@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    radioGroup=(RadioGroup)findViewById(R.id.radiogroup);
    button=(Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener(){
@Override
    public void onClick(View v) {
    int id=radioGroup.getCheckedRadioButtonId();
    radioButton=(RadioButton)findViewById(id);
    String s=radioButton.getText().toString();
    Toast.makeText(getApplicationContext(),"COURSE IS :"+s,6000).show();
     }
    });
   }
}

Output :-

RadioButton
RadioButton fig:1




















RadioButton
RadioButton fi:2




















RadioButton
RadioButton fig:3

No comments:

Post a Comment