Search code examples
androidbackground-imageandroid-constraintlayoutandroid-radiobutton

How to change constraint layout background image using java in android


I'm trying to change the constraint layout background image. On click of either one of the two radio buttons the background image should get changed. I've attached the code below. I've set a OnCheckedChangeListener on the radioGroup.

Java file

package com.mitwpu.practicallab_6_2_2020;

import android.support.constraint.ConstraintLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class Change_Background_Image_Activity extends AppCompatActivity {

    RadioGroup radioGroup;
    RadioButton radioButtonTom;
    RadioButton radioButtonJerry;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change__background__image_);

        radioGroup=(RadioGroup)findViewById(R.id.radioGroupId);

        radioButtonTom=(RadioButton)findViewById(R.id.radioButtonTom);

        radioButtonJerry=(RadioButton)findViewById(R.id.radioButtonJerry);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.radioButtonTom : {
                        //R.drawable.tom ->image1 
                        break;
                    }
                    case R.id.radioButtonJerry : {
                        //R.drawable.jerry->image2

                        break;
                    }
                }
            }
        });

    }
}

xml file

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/backgroudId"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Change_Background_Image_Activity">

    <RadioGroup
        android:id="@+id/radioGroupId"
        android:layout_width="274dp"
        android:layout_height="448dp"
        android:layout_marginStart="79dp"
        android:layout_marginTop="116dp"
        android:layout_marginEnd="58dp"
        android:layout_marginBottom="167dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <RadioButton
            android:id="@+id/radioButtonTom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Tom" />

        <RadioButton
            android:id="@+id/radioButtonJerry"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Jerry" />
    </RadioGroup>
</android.support.constraint.ConstraintLayout>

how to change the background image once the radio button is clicked


Solution

  • constraintId.setBackground(ContextCompat.getDrawable(this, R.drawable.some_drawable))