Question

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.java:

import android.app.Activity;
import android.os.Bundle;

public class Favs extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.favs);
  }
}

which points to this .xml file -> favs.xml

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

I understand that the android:onClick="onClick" needs to be there however I have no idea what to do after this.

Was it helpful?

Solution

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

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android: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">
   <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="What is your favorite color?" 
            android:padding="3dip"/>
   <RadioGroup android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:id="@+id/Group1"
               android:orientation="vertical">
       <RadioButton android:id="@+id/radio_red"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Red" 
                    />
       <RadioButton android:id="@+id/radio_yellow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Yellow" />
    </RadioGroup>
</LinearLayout>

Activity

public class Change extends Activity {


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

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


        final RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red);
        final RadioButton radio_yellow = (RadioButton) findViewById(R.id.radio_yellow);
        radio_red.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                ll.setBackgroundColor(Color.RED);

            }
        });

 radio_yellow.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                ll.setBackgroundColor(Color.YELLOW);

            }
        });

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top