Radio Button Arrangement in row and column
Step1
è Create new
android application “CustomRadioArrangement “
è Give
Activity name as “RadioButtonDemo”
è Set Layout
name as “main”
Step2
è Copy below
code and past in main.xml file
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".RadioButtonDemo" >
<RadioGroup
android:id="@+id/radiogroup"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
Step3
è Copy below
code and past in RadioButtonDemo.java file
package
com.example.customradioarrangement;
import
android.os.Bundle;
import
android.app.Activity;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.view.ViewGroup;
import
android.widget.LinearLayout;
import
android.widget.RadioButton;
public class RadioButtonDemo
extends Activity {
//Globally
declared
int oldId = 0;
int newId = 0;
RadioButton
radio1;
@Override
protected void onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//This count
variable is taken for setting a unique id to radio button
int count = 0;
//Below for
loop prepares 5 rows for radio button
for(int row=0; row <
5; row++)
{
//Declare a dynamic Linear Layout,
with Horizontal Orientation
LinearLayout
ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
//Below for loop prepares 2 column for
each rows
for(int i = 0; i <
2; i++)
{
//Below code
is for creating Dynamic radio button and set it's attributes like id,text and
so on
RadioButton
rdbtn = new RadioButton(this);
rdbtn.setId(count);//hear we set
count as id
rdbtn.setText("test
" + rdbtn.getId());
rdbtn.setTag(count);
rdbtn.setOnClickListener(radio_listener_for_all_radio);
ll.addView(rdbtn);//hear radio
button is added to linear layout
count ++;
}
// hear Linear layout is add to radio
group which define in main.xml file
((ViewGroup)findViewById(R.id.radiogroup)).addView(ll);
count ++;
}
}
//onclick
listener for radio button
OnClickListener
radio_listener_for_all_radio = new OnClickListener
(){
public void onClick(View v)
{
RadioButton rb = (RadioButton) v;
oldId = newId;
//Radio button tag value is set as
newId
newId = (Integer) rb.getTag();
//if user click same radio button
again and again then oldId and newId becomes same
// in such a case we have to keep
radio button selected and set oldId to 0
if( oldId == newId )
{
radio1 = (RadioButton) findViewById(newId);
radio1.setChecked(true);
oldId = 0;
}
else
{
//oldest radio button is unchecked
through oldId
radio1 = (RadioButton) findViewById(oldId);
radio1.setChecked(false);
}
}
};
}
Finally outpour screen is:
