Reputation: 7391
I made an android application in which the background color of the activity is changing with the click on the RadioButtons in the Activity.
I'm elaborating my question with help of images :
On opening the app :
So we have got 3 RadioButtons on click of any of the RadioButtons the background color of the activity should change to the respective color.
=> when I click on any of the radio button first time then the background color is reflected properly. eg:
After clicking on Green the background color changes to green.
=> But after that whenever I click on any of the button the background color doesn't reflect instantly. eg:
I clicked on Blue but background color is still Green.
=> Now when I click on other radio buttons then the background color of the previous clicked radio buttons gets reflected in the Activity. eg:
After I click on red, then I got the background color as blue.
So, how can I fix this issue. I want the background color to be changed immediately after the radio button is checked.
The MainActivity.java file is :
package com.aupadhyay.assignment2;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
RadioGroup radioGroup;
RadioButton redRadioButton, greenRadioButton, blueRadioButton;
LinearLayout linearLayout;
public void initLinearLayout()
{
linearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
}
public void initRadioGroup()
{
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
redRadioButton = (RadioButton) findViewById(R.id.redRadioButton);
greenRadioButton = (RadioButton) findViewById(R.id.greenRadioButton);
blueRadioButton = (RadioButton) findViewById(R.id.blueRadioButton);
redRadioButton.setOnCheckedChangeListener(this);
greenRadioButton.setOnCheckedChangeListener(this);
blueRadioButton.setOnCheckedChangeListener(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initRadioGroup();
initLinearLayout();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId())
{
case R.id.redRadioButton:
linearLayout.setBackgroundColor(Color.RED);
break;
case R.id.greenRadioButton:
linearLayout.setBackgroundColor(Color.GREEN);
break;
case R.id.blueRadioButton:
linearLayout.setBackgroundColor(Color.BLUE);
break;
}
}
}
The activity_main.xml file is :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearLayout1"
android:padding="10dp"
android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/radioGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/redRadioButton"
android:text="Red"
android:layout_weight="1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/greenRadioButton"
android:text="Green"
android:layout_weight="1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/blueRadioButton"
android:text="Blue"
android:layout_weight="1"/>
</RadioGroup>
</LinearLayout>
Upvotes: 0
Views: 77
Reputation: 6160
try to use setOnClickListener
instead of setOnCheckedChangeListener
redRadioButton.setOnClickListener(this);
greenRadioButton.setOnClickListener(this);
blueRadioButton.setOnClickListener(this);
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.redRadioButton:
linearLayout.setBackgroundColor(Color.RED);
break;
case R.id.greenRadioButton:
linearLayout.setBackgroundColor(Color.GREEN);
break;
case R.id.blueRadioButton:
linearLayout.setBackgroundColor(Color.BLUE);
break;
}
}
Upvotes: 1