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="#fffed4" > <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="horizontal" android:layout_below="@id/tv" android:padding="15dp" > <RadioButton android:id="@+id/rb_coldfusion" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ColdFusion" android:paddingRight="15dp" /> <RadioButton android:id="@+id/rb_flex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Flex" android:paddingRight="15dp" /> <RadioButton android:id="@+id/rb_flash" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Flash" android:paddingRight="15dp" /> </RadioGroup> </RelativeLayout> MainActivity.java
package com.cfsuman.me.androidcodesnippets; import android.graphics.Color; import android.os.Bundle; import android.app.Activity; 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); simpulan RadioGroup rg = (RadioGroup) findViewById(R.id.rg); simpulan RadioButton rb_coldfusion = (RadioButton) findViewById(R.id.rb_coldfusion); simpulan RadioButton rb_flex = (RadioButton) findViewById(R.id.rb_flex); simpulan RadioButton rb_flash = (RadioButton) findViewById(R.id.rb_flash); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (rb_coldfusion.isChecked()) { rb_coldfusion.setTextColor(Color.BLUE); rb_flex.setTextColor(Color.BLACK); rb_flash.setTextColor(Color.BLACK); } if (rb_flex.isChecked()){ rb_coldfusion.setTextColor(Color.BLACK); rb_flex.setTextColor(Color.BLUE); rb_flash.setTextColor(Color.BLACK); } if (rb_flash.isChecked()){ rb_coldfusion.setTextColor(Color.BLACK); rb_flex.setTextColor(Color.BLACK); rb_flash.setTextColor(Color.BLUE); } } }); } }
- How to change RadioButton background color
- How to use RadioGroup onCheckedChanged
- How to set selected index of 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 display a title for RadioGroup
- How to change CheckBox text and background color
- How to use CheckBox CheckedChangeListener and ClickListener
Komentar
Posting Komentar