Skip to content

Instantly share code, notes, and snippets.

@ishitcno1
Last active April 20, 2024 12:34
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ishitcno1/9544243 to your computer and use it in GitHub Desktop.
Save ishitcno1/9544243 to your computer and use it in GitHub Desktop.
multiple row radio button in android.
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.edinstudio.app.samples.multiplerowradiobuttons.MainActivity">
<RadioGroup
android:id="@+id/first_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/type1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/RadioButton"
android:text="type1"
android:checked="true" />
<RadioButton
android:id="@+id/type2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/RadioButton"
android:text="type2" />
<RadioButton
android:id="@+id/type3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/RadioButton"
android:text="type3" />
</RadioGroup>
<RadioGroup
android:id="@+id/second_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/first_group"
android:orientation="horizontal">
<RadioButton
android:id="@+id/type4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/RadioButton"
android:text="type4" />
<RadioButton
android:id="@+id/type5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/RadioButton"
android:text="type5" />
<RadioButton
android:id="@+id/type6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/RadioButton"
android:text="type6" />
</RadioGroup>
<Button
android:layout_below="@id/second_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Type"
android:onClick="showType" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/vividCerise" android:state_checked="true" />
<item android:drawable="@android:color/transparent" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="vividCerise">#da1d81</color>
<color name="smokyBlack">#100c08</color>
</resources>
package com.edinstudio.app.samples.multiplerowradiobuttons;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private RadioGroup mFirstGroup;
private RadioGroup mSecondGroup;
private boolean isChecking = true;
private int mCheckedId = R.id.type1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFirstGroup = (RadioGroup) findViewById(R.id.first_group);
mSecondGroup = (RadioGroup) findViewById(R.id.second_group);
mFirstGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId != -1 && isChecking) {
isChecking = false;
mSecondGroup.clearCheck();
mCheckedId = checkedId;
}
isChecking = true;
}
});
mSecondGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId != -1 && isChecking) {
isChecking = false;
mFirstGroup.clearCheck();
mCheckedId = checkedId;
}
isChecking = true;
}
});
}
public void showType(View view) {
if (mCheckedId == R.id.type1) {
Toast.makeText(this, "type1", Toast.LENGTH_SHORT).show();
} else if (mCheckedId == R.id.type2) {
Toast.makeText(this, "type2", Toast.LENGTH_SHORT).show();
} else if (mCheckedId == R.id.type3) {
Toast.makeText(this, "type3", Toast.LENGTH_SHORT).show();
} else if (mCheckedId == R.id.type4) {
Toast.makeText(this, "type4", Toast.LENGTH_SHORT).show();
} else if (mCheckedId == R.id.type5) {
Toast.makeText(this, "type5", Toast.LENGTH_SHORT).show();
} else if (mCheckedId == R.id.type6) {
Toast.makeText(this, "type6", Toast.LENGTH_SHORT).show();
}
}
}
<style name="RadioButton">
<item name="android:button">@null</item>
<item name="android:background">@drawable/bg_toggle_checked</item>
<item name="android:textColor">@color/smokyBlack</item>
<item name="android:gravity">center</item>
<item name="android:padding">8dp</item>
<item name="android:layout_margin">8dp</item>
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment