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
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;
}}