0

I am trying to get color value which has declared in colors.xml and try to get from ViewModel class. In the ViewModel class, color is shown correctly but when I run the app on emulator, the color would be shown differently. I use MutableLiveData for get color value from the colors.xml. Do you have any ideas to get correct color from ViewModel?

Thank you very much

screen_1

screen_2

Here is my code

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    root = inflater.inflate(R.layout.fragment_timetable, container, false);

    TimeTableViewModel timeTableViewModel = ViewModelProviders.of(this).get(TimeTableViewModel.class);
timeTableViewModel.get_text_bg_red().observe(getViewLifecycleOwner(), new Observer<Integer>() {
        @Override
        public void onChanged(Integer integer) {
            mon_textView_10.setBackgroundColor(integer);
            mon_textView_11.setBackgroundColor(integer);
        }
    });
return root;
}

public class TimeTableViewModel extends ViewModel {

private MutableLiveData<Integer> text_bg_red;

public TimeTableViewModel() {

    text_bg_red = new MutableLiveData<>();
    text_bg_red.setValue(R.color.colorAccent);
}

LiveData<Integer> get_text_bg_red(){
    return text_bg_red;
}}

1 Answers1

0

In your activity XML or in Android Manifest (activity declaration), check the theme you are using, it could be overridden by some other theme who has colorAccent. If not try to rename colorAccent into something else e.g. colorBgRed, then rebuild and try.

Mike
  • 1,313
  • 12
  • 21