activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" tools:context=".MainActivity" android:background="#d2ffe2" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Which is your most favorite?" /> <RadioGroup android:id="@+id/rg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_below="@id/tv" android:padding="15dp" > <RadioButton android:id="@+id/rb_coldfusion" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ColdFusion" android:paddingRight="15dp" /> <RadioButton android:id="@+id/rb_flex" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Flex" android:paddingRight="15dp" /> </RadioGroup> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Set Index 1 Radio Button Checked" android:layout_below="@+id/rg" /> </RelativeLayout>
MainActivity.java
package com.cfsuman.me.androidcodesnippets; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.btn); selesai RadioGroup rg = (RadioGroup) findViewById(R.id.rg); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Set the index 1 Radio Button checked from RadioGroup // This will checked second Radio Button // RadioGroup index started from 0 (zero) ((RadioButton)rg.getChildAt(1)).setChecked(true); } }); } }


- How to use RadioButton click event
- How to change RadioButton text color
- How to change RadioButton background color
- How to get selected item from a RadioGroup
- How to programmatically select an item from a RadioGroup
- How to create a horizontal layout RadioGroup
- How to create a RadioGroup programmatically
- How to add RadioButton to a RadioGroup programmatically
- How to hide circle from RadioButton
- How to change checked RadioButton text and background color
Komentar
Posting Komentar