About
Home > Tutorials Home > Android > RadioButton Controls in Android Application

RadioButton Controls in Android Application

Published on November 30, -0001 12:00:00 AM

We are an online tutoring company that has laid its foundation in utilizing Multiple Intelligence methodology to understand specific needs and learning styles of a student.So that we patch a particular student to a tutor having a compatible teaching style. We have devised a smart online tutoring system to give you measurable results.Schools, cramped with students, fail to give your child the edge, the motivation and the right teacher guidance to help him find the true potential. We at Eonlinetutors.com make sure that your child gets the perfect learning environment to outperform.




Step 1 : Open the Eclipse and create a new Android project using Blank Activity application template. Give the application name "Radio Button Example in Android".

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:orientation="vertical">
	
	<RadioGroup
		android:orientation="horizontal"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:id="@+id/rg">
			
		<RadioButton>
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="horizontal"
			android:id="@+id/rb1">
		</RadioButton>
		
		<RadioButton
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="veritcal"
			android:id="@+id/rb2">
		</RadioButton>
	</RadioGroup>
</LinearLayout>

Step 2 : Copy the Following Code into your .Java File.

package com.example.radiobuttonxample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity {

	private RadioGroup rg;
	private RadioButton rb1;
	private RadioButton rb2;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		rg=(RadioGroup)findViewById(R.id.rg);
		rb1=(RadioButton)findViewById(R.id.rb1);
		rb2=(RadioButton)findViewById(R.id.rb2);

		rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {<	

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {

				if(checkedId==(R.id.rb1)) {
					rg.setOrientation(LinearLayout.HORIZONTAL);
				}
				if(checkedId==(R.id.rb2)) {
					rg.setOrientation(LinearLayout.VERTICAL);
				}
			}
		});
	}
}
ALL COMMENTS (2)
Abhishek Bedre 2017-03-28 17:25:15
I have implemented my own "jarvis"....thanks to this example.        
2019-01-03 13:22:19
It is working fine in desktop but not working in android mobile