How can I change color of specific layout component?

0

How can I change color of star components in this layout? Can it be done it below code or do I have to work in xml?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(R.style.Custom);
    ViewGroup layout = getLayout(getIntent().getStringExtra("key1"));
    if (layout == null) return;
    layout.addView(new ProgressBar(this));
    layout.addView(new RadioButton(this));
    layout.addView(new RatingBar(this));
    layout.addView(new CheckBox(this));
    layout.addView(new Switch(this));
    layout.addView(new SeekBar(this));
    layout.setBackgroundColor(Color.parseColor("#3333ff"));
    layout.setBackgroundColor(0xffff0000);
    setContentView(layout);    }
android
asked on Stack Overflow Jan 23, 2020 by Arnolt Infern Kitler • edited Jan 23, 2020 by Markus Kauppinen

1 Answer

0

You should have a reference of your layout components:

private ProgressBar progressBar;
private RadioButton radioButton;
private RatingButton ratingButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(R.style.Custom);
    ViewGroup layout = getLayout(getIntent().getStringExtra("key1"));
    if (layout == null) return;

    progressBar = new ProgressBar(this);
    radioButton = new RadioButton(this);
    ratingButton = new RatingButton(this);

    layout.addView(progressBar);
    layout.addView(radioButton);
    layout.addView(ratingBar);

    progressBar.setBackgroundColor(...);
    radioButton.setBackgroundColor(...);
    ratingButton.setBackgroundColor(...);   
    //And so on, with the appropriate method for each View

    layout.setBackgroundColor(Color.parseColor("#3333ff"));
    layout.setBackgroundColor(0xffff0000);
    setContentView(layout);   
}
answered on Stack Overflow Jan 24, 2020 by Luca Murra

User contributions licensed under CC BY-SA 3.0