Thursday, 27 August 2015

RadioButton1 in Android

activity_main.xml
----------------------
<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"
   
tools:context=".MainActivity"
   
android:orientation="vertical"
   
android:id="@+id/gddd">


    <
RadioGroup
       
android:layout_width="fill_parent"
       
android:id="@+id/radiogroup1"
       
android:layout_height="wrap_content"
       
android:layout_marginTop="25dp">
        <
RadioButton
           
android:layout_width="fill_parent"
           
android:id="@+id/radio1"
           
android:text="Android"
           
android:layout_height="wrap_content" />
        <
RadioButton
           
android:layout_width="fill_parent"
            
android:id="@+id/radio2"
           
android:text="Ios"
           
android:layout_height="wrap_content" />
        <
RadioButton
           
android:layout_width="fill_parent"
           
android:id="@+id/radio3"
           
android:text="Windows"
            
android:layout_height="wrap_content" />
        <
RadioButton
           
android:layout_width="fill_parent"
           
android:id="@+id/radio4"
           
android:text="Blackberry"
           
android:layout_height="wrap_content" />
        <
RadioButton
           
android:layout_width="fill_parent"
           
android:id="@+id/radio5"
           
android:text="Symbain"
           
android:layout_height="wrap_content" />
        <
RadioButton
           
android:layout_width="fill_parent"
            
android:id="@+id/radio6"
           
android:text="Bada"
           
android:layout_height="wrap_content" />

</
RadioGroup>
</
LinearLayout>
MainActivity.Java
-------------------
package com.android.anil.radiobutton1;



import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.Toast;



public class MainActivity extends AppCompatActivity {

    RadioGroup rg;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        rg=(RadioGroup)findViewById(R.id.radiogroup1);

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override

            public void onCheckedChanged(RadioGroup group, int checkedId) {

                int id=rg.getCheckedRadioButtonId();

                RadioButton rb=(RadioButton)findViewById(id);

                String  s1=rb.getText().toString();

       Toast.makeText(getApplicationContext(),"Mobile is : "+s1,5000).show();


            }

        });

    }
}
Output is :
------------------

















No comments:

Post a Comment