Mauro Stancato
Mauro Stancato

Reputation: 577

How can I change the activity's background color from other activity?

I need to change the background colors of MainActivity ConstraintLayout. I have a second activity called selectcolors where I have buttons that when they are clicked it would change the MainActivity background color.

I need a method to change the background color of main activity from the second activity's each button of each color respectively.

    package com.example.kidscalculator;

    import androidx.appcompat.app.AppCompatActivity;

    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    public class selectcolors extends AppCompatActivity {
    private TextView textSelect;
    private Button buttonWhite;
    private Button buttonYellow;
    private Button buttonRed;
    private Button buttonBlue;
    private Button buttonGreen;
    private Button buttonOrange;
    private Button buttonPurple;
    private Button buttonBlack;
    private Button buttonGrey;
    private Button buttonOrangeLight;

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

            textSelect = (TextView)findViewById(R.id.textSelect);
            buttonWhite = (Button)findViewById(R.id.buttonWhite);
            buttonYellow = (Button)findViewById(R.id.buttonYellow);
            buttonRed = (Button)findViewById(R.id.buttonRed);
            buttonBlue = (Button)findViewById(R.id.buttonBlue);
            buttonGreen = (Button)findViewById(R.id.buttonGreen);
            buttonOrange = (Button)findViewById(R.id.buttonOrange);
            buttonPurple = (Button)findViewById(R.id.buttonPurple);
            buttonBlack = (Button)findViewById(R.id.buttonBlack);
            buttonGrey = (Button)findViewById(R.id.buttonGrey);
            buttonOrangeLight = (Button)findViewById(R.id.buttonOrangeLight);

            buttonWhite.setOnClickListener(new View.OnClickListener() {
                @Override
        public void onClick(View v) {
               //Set Main Activity ConstraintLayout Background White
        }
    });

    buttonYellow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             //Set Main Activity ConstraintLayout Background Yellow
        }
    });

    buttonRed.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //etc.
        }
    });

    buttonBlue.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    buttonGreen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    buttonOrange.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    buttonPurple.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    buttonBlack.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    buttonGrey.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    buttonOrangeLight.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        }
    });
}
}

Upvotes: 0

Views: 1393

Answers (3)

Ichigo Kurosaki
Ichigo Kurosaki

Reputation: 3843

You can use code as follows

From Mainactivity start the selectcolors activity as follows:

Intent intent  = new Intent(this,selectcolors.class);
startActivityForResult(intent,1234);

Now in selectcolors activity whenever you select any color i.e your onClick.. just add following snippet

Intent intent = new Intent();
intent.putExtra("selectedColor",<Your selected color>); // Suppose you are passing color as #aabbcc i.e hex string
setResult(1234,intent);

Now in mainactivity write following snippet

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
      if(requestCode == 1234){
         String yourSelectedColor = data.getStringExtra("selectedColor");
         Intent myIntent = new Intent(this,Mainactivity.class);
         myIntent.putExtra("selectedColor",yourSelectedColor);
         finish(); // Finish this activity
         startActivity(myIntent);
        
      }

}

And then finally in onCreate of main in onCreate()

if (getIntent().getExtras() != null || getIntent().getExtras().containsKey("selectedColor")) {
 bgcolor= getIntent().getExtras().getString("selectedColor");
(layout_id).setBackground(Color.parseColor(bgcolor));

}

Upvotes: 1

Shahriar Zaman
Shahriar Zaman

Reputation: 938

Why a whole another activity for this simple purpose? Use a dialog instead.

The Dialog way:

  1. create xml layout with color buttons as you have created the activity layout.
  2. in MainActivity inside onCreate() find the root ConstraintLayout view. For example ConstraintLayout constraintLayout = findViewById(R.id.rootConstraint_MainActivity);
  3. Create a dialog class as follow:
public class DialogColorPicker extends DialogFragment implements View.OnClickListener{
    private static final String TAG = "ColorPickerDialog";
    private static int colorRed = Color.RED;
    private static int colorGreen = Color.GREEN;
    private static int colorYellow = Color.YELLOW;
    private static int colorBlue = Color.BLUE;
    
    private DialogColorPickerListener listener;

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        View view = View.inflate(getContext(), R.layout.fragment_color_picker, null);
        Button btn_red = view.findViewById(R.id.btn_red_fragment);
        Button btn_green = view.findViewById(R.id.btn_green_fragment);
        Button btn_yellow = view.findViewById(R.id.btn_yellow_fragment);
        Button btn_blue = view.findViewById(R.id.btn_blue_fragment);
        
        btn_red.setOnClickListener(this);
        btn_green.setOnClickListener(this);
        btn_yellow.setOnClickListener(this);
        btn_blue.setOnClickListener(this);
    

        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setView(view)
                .setTitle("Choose Color");
        
        return builder.create();
    }
    
    @Override
    public void onClick(View v) {
        switch(v.getId()) {
        case R.id.btn_red_fragment:
            listener.onConfirm(colorRed);
            break();
        case R.id.btn_green_fragment:
            listener.onConfirm(colorGreen);
            break();
        case R.id.btn_yellow_fragment:
            listener.onConfirm(colorYellow);
            break();
        case R.id.btn_blue_fragment:
            listener.onConfirm(colorBlue);
            break();
    }
    
    public void setListener(DialogColorPickerListener listener) {
        this.listener = listener;
    }

    public interface DialogColorPickerListener {
        void onConfirm(int selectedColor);
    }
}
  1. in MainActivity.class outside the onCreate()
public void openColorPickerDialog(View v) {
        DialogColorPicker colorPicker = new DialogColorPicker();
        colorPicker.setListener(new DialogColorPicker.DialogColorPickerListener(){
            @Override
            public void onConfirm(int selectedColor) {
                //Handle your color change from here;
                constraintLayout.setBackgroundColor(selectedColor);
            }
        });
        colorPicker.showNow(getSupportFragmentManager(), "ColorPicker");
    }

Hope this will work. And better way to handle.

Upvotes: 0

akshay choukimath
akshay choukimath

Reputation: 97

One thing you can do is. In selectcolors class for every button assign id or name so that main activity knows which color is clicked. And use intent to go to MainActivity.

In selectcolors.class

for all the button clicks

Intent intent = new Intent(getApplicatioinContext(),MainActivity.class); intent.putExtra("color","Red"); startActivity(intent)

similary for all the buttons.

In MainActivity.class

String bgcolor = "";

in onCreate()

if (getIntent().getExtras() != null || getIntent().getExtras().containsKey("color")) { bgcolor= getIntent().getExtras().getString("color");}

if (bgcolor.equals("red")){ (layout_id).setBackground(context.getResources().getDrawable(R.drawable.red)); }

Do same for all other buttons as well.

Upvotes: 1

Related Questions