Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to change backgraund color View on select radio button in android?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 314
    Comment on it

    Here I have created change backgraund color View app. when we select any radio button then it will change the backgraund color. In the below example i have use four radio button on diffrent colour id. You can use the below code to change backgraund color view in Android.
    Step(1)-MainActivity class-

    public class MainActivity extends Activity implements ColorFragment.OnColorChangeListener {
        LinearLayout linearLayout;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
           linearLayout =(LinearLayout) findViewById(R.id.main_layout_id);
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            ColorFragment colorFragment = new ColorFragment();
            fragmentTransaction.add(R.id.fragment_container, colorFragment);
            fragmentTransaction.commit();
    
        }
    
    
        @Override
        public void colorChanged(String color_name) {
            if (color_name.equals("RED")) {
                linearLayout.setBackgroundColor(Color.RED);
            } else if (color_name.equals("BLUE")) {
                linearLayout.setBackgroundColor(Color.BLUE);
    
            } else if (color_name.equals("GREEN")) {
                linearLayout.setBackgroundColor(Color.GREEN);
    
            } else if (color_name.equals("GRAY")) {
                linearLayout.setBackgroundColor(Color.GRAY);
    
            }
        }
    
    }
    

    Step(2)-ColorFragment class-

    public class ColorFragment extends Fragment {
        RadioGroup Color_Group;
       OnColorChangeListener onColorChangeListener;
    
    
        @Override
        public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
            View view;
            view = inflater.inflate(R.layout.color_fragment_layout,container,false);
            Color_Group = (RadioGroup)view.findViewById(R.id.color_group);
            Color_Group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
    
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
    
    
    
    
    
    
                    switch (checkedId)
                    {
                        case R.id.red_id:
                            onColorChangeListener.colorChanged("RED");
                            break;
                        case R.id.blue_id:
                            onColorChangeListener.colorChanged("BLUE");
                            break;
                        case R.id.green_id:
                            onColorChangeListener.colorChanged("GREEN");
                            break;
                        case R.id.gray_id:
                            onColorChangeListener.colorChanged("GRAY");
                            break;
                }
                }
    
            });
            return view;
    
        }
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            try {
                onColorChangeListener= (OnColorChangeListener)  activity;
            } catch (Exception ex) {}
    
        }
    
    
    
    
    
        public interface OnColorChangeListener
        {
            public void colorChanged(String color_name);
        }
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: