Skip to content Skip to sidebar Skip to footer

Changing Background Color With Radio Buttons Android

I am attempting to change the background of a tab of my application by selecting a Radio Button from a RadioGroup, however I am not sure how to go about this. So far I have Favs.ja

Solution 1:

hi use below code for changing the background according to radio button selection

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutandroid:layout_width="fill_parent"android:orientation="vertical"android:id="@+id/LinearLayout"xmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="fill_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="What is your favorite color?"android:padding="3dip"/><RadioGroupandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:id="@+id/Group1"android:orientation="vertical"><RadioButtonandroid:id="@+id/radio_red"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Red" 
                    /><RadioButtonandroid:id="@+id/radio_yellow"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Yellow" /></RadioGroup></LinearLayout>

Activity

publicclassChangeextendsActivity {


    /** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final LinearLayout ll=(LinearLayout) findViewById(R.id.LinearLayout);


        finalRadioButtonradio_red= (RadioButton) findViewById(R.id.radio_red);
        finalRadioButtonradio_yellow= (RadioButton) findViewById(R.id.radio_yellow);
        radio_red.setOnClickListener(newOnClickListener() {

            publicvoidonClick(View v) {
                ll.setBackgroundColor(Color.RED);

            }
        });

 radio_yellow.setOnClickListener(newOnClickListener() {

            publicvoidonClick(View v) {
                ll.setBackgroundColor(Color.YELLOW);

            }
        });

    }
}

Post a Comment for "Changing Background Color With Radio Buttons Android"