How to Change color of Calender Dialog Android

187 views Asked by At

In my app I am using DatePicker dialog and Time picker Dialog to choose Date and time accordingly. Now, in those dialog the color is set based on my Themecolor. But what to I want to do, is to change those color manually. For exmaple I have changed toolbar color , in this way toolbar.setBackgroundColor(Constant.color); But, How can I change the heading color , (where current date is showing- I have attached the picture) and datepicker color (the round picker which indicate the current date) in the same way manually.

Here is my image of DeatepickerDialog

DatePicker Dialog

Here is my code for date picker dialog

public class TodoAddActivity extends AppCompatActivity {

// Variables
private Context mContext;

....

private RealmResults<TodoModel> results;

public final static String INTENT_KEY_ID = "taskId";
public final static String DATE_FORMAT = "dd/MMM/yy";

Constant constant;
SharedPreferences app_preferences;
int appTheme;
int themeColor;
int appColor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView( R.layout.todo_add_layout);

    app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
    appColor = app_preferences.getInt("color", 0xff3F51B5);
    appTheme = app_preferences.getInt("theme", 0xff3F51B5);
    themeColor = appColor;
    constant.color = appColor;

    if (themeColor == 0){
        setTheme(Constant.theme);
    }else if (appTheme == 0){
        setTheme(Constant.theme);
    }else{
        setTheme(appTheme);
    }

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_add);
    toolbar.setBackgroundColor(Constant.color);
    setSupportActionBar(toolbar);

    mContext = this;
    realm=Realm.getDefaultInstance();
    setUpUIViews();
    showData();

    final String taskId = getIntent().getStringExtra(INTENT_KEY_ID);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (taskId == null)
                addTask();
            else
                editTask(taskId);
        }
    });
    mTaskDateEditText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            datePicker(view);
        }
    });
    mTaskDateEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if (b)
                datePicker(view);
        }
    });
    mTaskTimeEditText.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            timePicker( view );
        }
    } );
    mTaskTimeEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if (b)
                timePicker( view );
        }
    });
    if (taskId != null) {
        fillTask(realm.where(TodoModel.class).equalTo("id", taskId).findFirst());
        button.setText("EDIT");
    }



}
private void setUpUIViews() {
    ...
}

public void showData(){
    //tasks = new ArrayList<>(); //error
    //RealmResults<TodoModel> tasksResult = realm.where(TodoModel.class).findAll();
    //for (TodoModel task : tasksResult)
    //    tasks.add(task); //error*/
    results = realm.where(TodoModel.class).findAll();
}

protected TodoModel getTask(int position) {
    return results.get(position);
}

private void fillTask(TodoModel task) {
   ...
}

private void addTask() {
    ...
}

protected void addTask(TodoModel task) {

   ..
}
private void editTask(String taskId) {
    S....
}

protected void updateTask(TodoModel task) {

   ....
}


private void datePicker(final View view) {
    Calendar currentTime = Calendar.getInstance();
    int year = currentTime.get(Calendar.YEAR);
    int monthOfYear = currentTime.get(Calendar.MONTH);
    int dayOfMonth = currentTime.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog mDatePickerDialog = new DatePickerDialog(mContext,0, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(year, monthOfYear, dayOfMonth);
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
            ((EditText) view).setText(sdf.format(calendar.getTime()));
        }
    }, year, monthOfYear, dayOfMonth);

    mDatePickerDialog.setTitle("Select Time");
    mDatePickerDialog.show();
}
private void timePicker(final View view) {
    Calendar mcurrentTime = Calendar.getInstance();
    int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
    int minute = mcurrentTime.get(Calendar.MINUTE);
    TimePickerDialog mTimePicker;
    mTimePicker = new TimePickerDialog(mContext,0, new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
            mTaskTimeEditText.setText( selectedHour + ":" + selectedMinute);
        }
    }, hour, minute, true);//Yes 24 hour time
    mTimePicker.setTitle("Select Time");
    mTimePicker.show();

}

}

2

There are 2 answers

1
Harshit Trivedi On
new DatePickerDialog(MainActivity.this, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    //Your Code
}
}, year, monthOfYear, dayOfMonth).show();

EDIT - Changed theme to Theme.AppCompat.Light.Dialog as suggested

<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
      <item name="colorAccent">@color/xyz</item>
</style>
1
Martin Lund On

Try doing this:

In your styles.xml add the following:

<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">@color/yourColor</item>
</style>

Then in your datePicker do this instead.

DatePickerDialog mDatePickerDialog = new DatePickerDialog(mContext, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener()