skip to Main Content

I have an activity with a button "Select color" and a edittxt (insert hex code for color). When user hit the button the backgroundcolor of the resourcefile must change to hex code. The color is now set on XML file. How can I change this XML file with user input?

package com.gade.tabletcolor;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class PickColor extends AppCompatActivity {

    View view;
    EditText addColor;

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

        ImageButton button = findViewById(R.id.imageBtn11);
        Button AddColor = findViewById(R.id.colorBtn);
        addColor = findViewById(R.id.txtColor);
        view = this.getWindow().getDecorView();

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

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

                setContentView(R.layout.pickcolor);


            }
        });

    }

    public void nextpage(){
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);
    }
}

2

Answers


  1. View someView = findViewById(R.id.screen);
    View root = someView.getRootView();
    root.setBackgroundColor(getResources().getColor(color.white));
    

    or

    yourView.setBackgroundColor(Color.parseColor("#ffffff"));
    
    Login or Signup to reply.
  2. Adding to the previous answer.

    AddColor.setOnClickListener(new View.OnClickListener() {
      @Override
         public void onClick(View v) {
                   yourView.setBackgroundColor(Color.parseColor(<editText>.parseInputToString()));
                }
            });
    
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search