Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi sir,
i have created two activities in which after clicking on one radio button it should go to second activity.Everything works fine.It goes to the next activity but i got a little problem.After clicking one radio button if i am trying to click another radio button then the first radio button is still showing selected..and all the radio button is showing selected after clicking another or same.
here is my code,
First XML file:activity_main.xml
XML
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android">
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <textview>
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Branch"/>

    <radiobutton>
        android:id="@+id/radioIT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Information Technology"
        android:checked="true" />

    <radiobutton>
        android:id="@+id/radioCS"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Computer Science" />

    <Button
        android:id="@+id/buttonok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ok" />

</radiobutton></radiobutton></textview></linearlayout>


Second XML file:sem.Xml

XML
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android">
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <textview>
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Semister" />


    <radiobutton>
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sem 1"
        android:checked="true" />

    <radiobutton>
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sem 2" />

    <radiobutton>
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sem 3" />

    <radiobutton>
        android:id="@+id/radioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sem 4" />

</radiobutton></radiobutton></radiobutton></radiobutton></textview></linearlayout><pre>

First class File:(MainActivity.java)
<pre>package com.radiobutton;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;

public class MainActivity extends Activity {
	RadioButton search,type;
	Button Ok;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		search = (RadioButton) findViewById(R.id.radioIT);
		type = (RadioButton) findViewById(R.id.radioCS);

		  Ok = (Button) findViewById(R.id.buttonok);
		  Ok.setOnClickListener(new View.OnClickListener() {
		        @Override
		        public void onClick(View v) {
		        if(search.isChecked()){
		            Intent createIntent = new Intent(getApplicationContext(), Semister.class); // <----- START "SEARCH" ACTIVITY
		            startActivity(createIntent);
		            
		            }
		            else
		            {
		              if(type.isChecked())
		              {
		              Intent typeIntent = new Intent(getApplicationContext(), Semister.class); // <----- START "TYPE ENTRIES OUT" ACTIVITY
		              startActivity(typeIntent);
		              }
		            }
		        }
		    });
	}


}

second class file:(Semister.java)
package com.radiobutton;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;

public class MainActivity extends Activity {
	RadioButton search,type;
	Button Ok;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		search = (RadioButton) findViewById(R.id.radioIT);
		type = (RadioButton) findViewById(R.id.radioCS);

		  Ok = (Button) findViewById(R.id.buttonok);
		  Ok.setOnClickListener(new View.OnClickListener() {
		        @Override
		        public void onClick(View v) {
		        if(search.isChecked()){
		            Intent createIntent = new Intent(getApplicationContext(), Semister.class); // <----- START "SEARCH" ACTIVITY
		            startActivity(createIntent);
		            
		            }
		            else
		            {
		              if(type.isChecked())
		              {
		              Intent typeIntent = new Intent(getApplicationContext(), Semister.class); // <----- START "TYPE ENTRIES OUT" ACTIVITY
		              startActivity(typeIntent);
		              }
		            }
		        }
		    });
	}


}
Posted
Updated 18-Dec-13 3:09am
v2

1 solution

You have to group the radio buttons into a radio group like the following example:


Android Radio Group Example
 
Share this answer
 
v2
Comments
Maciej Los 18-Dec-13 11:49am    
+5!
Peter Leow 18-Dec-13 19:10pm    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900