I am trying to change the color solid shapein mine Fragment.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/icon_circle_background">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
...>
<solid android:color="@color/md_cyan_400"/>
</shape>
</item>
<item
...
</item>
</layer-list>
I cannot access it from my code, and I have already tried the following stack overflow answers:
Programmatically customize android shape color
How to change shape color dynamically?
Change the color of a solid form at run time inside Drawable xml used as background
None of them worked or were pretty outdated.
Fragment where I try to change color:
public class GameFragment extends Fragment {
private Theme currentTheme;
private Circle currentCircle;
private int currentOrganisationId;
private List<Card> circleCards;
private List<ImageButton> circleButtons;
private int viewHeight;
private int viewWidth;
private int marginCard;
private int[] tableStarts;
private RelativeLayout background;
private ViewTreeObserver vto;
public GameFragment() {
circleCards = new ArrayList<>();
circleButtons = new ArrayList<>();
tableStarts = new int[9];
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_game, container, false);
...
background = setBackground(rootView);
circleCards = currentCircle.getCards();
return rootView;
}
private void drawCards() {
circleButtons = new ArrayList<>();
for (Card card : circleCards) {
ImageButton myImageButton = new ImageButton(this.getContext());
myImageButton.setId(card.getCardId());
myImageButton.setBackgroundResource(R.drawable.circle_card_icon);
...
}
}
}
source
share