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);    }
Markus Kauppinen
  • 2,756
  • 4
  • 16
  • 28
  • 1
    Does this answer your question? [Android: Change color of ratingbar to golden](https://stackoverflow.com/questions/32810341/android-change-color-of-ratingbar-to-golden) – Markus Kauppinen Jan 23 '20 at 11:52
  • There's some older answer too, but I guess the newer one is valid in 2020. The old one: [Android RatingBar change star colors](https://stackoverflow.com/questions/2446270/android-ratingbar-change-star-colors/17369705) – Markus Kauppinen Jan 23 '20 at 12:27
  • Problem for me is that RatingBar etc are added with code, they are not there in layout design. How can I acces those variables? – Arnolt Infern Kitler Jan 23 '20 at 13:16

1 Answers1

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);   
}
Luca Murra
  • 1,533
  • 11
  • 21