0

I'm setting up a CustomTimePicker, and I need to change the clock Image of the time picker with any other clock image or icon.

    <TimePicker
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:timePickerMode="clock"
        /> 

"Is their any styling or something else possible to exchange the icon to the clock with my icon or image"

  • See if this answer where he showed how to create a custom style for `TimePicker` on Android with different colors and attributes helps: https://stackoverflow.com/a/51037169/11040422 – SaadAAkash Jun 18 '19 at 09:40
  • thanks both of you but I have to change the clock icon with my clock icon. Please provide information regarding to this and how to change the clock icon with my own icon – bharat singh Jun 18 '19 at 09:52
  • I have to design fully customize time picker with custom clock and custom dial to select – bharat singh Jun 18 '19 at 09:53
  • I used TimePickerDialog. It will show a dialog just like iphone. If you want that, i can share code. – Varun Jun 18 '19 at 10:40
  • Possible duplicate of [Changing TimePickerDialog Styles](https://stackoverflow.com/questions/51036919/changing-timepickerdialog-styles) – Raidri Jun 18 '19 at 11:51
  • Sure @Varun your help would be appriciated – bharat singh Jun 19 '19 at 05:42

2 Answers2

0

In timepicker, there is an important attribute:

 android:timePickerMode="spinner" or "clock"

and to backgroung color set use theme:

android:theme="@style/TimePickerTheme"
iDeveloper
  • 1,699
  • 22
  • 47
mainu
  • 625
  • 6
  • 14
0
 validTillTime.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            KeyboardUtility.hideKeyboard(CreatePassActivityTwo.this);
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                TimePickerUtility.getTime(CreatePassActivityTwo.this).show();
                validFrom = 0;
                return false;
            }
            return true;

validTillTime is an EditText here.

public class TimePickerUtility {

public static TimePickerDialog getTime(final Context context) {

    Calendar calendar = Calendar.getInstance();
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);

    TimePickerDialog.OnTimeSetListener timeSetListener =
            new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                    TimePickerListener timePickerListener = (TimePickerListener) context;
                    timePickerListener.onReceiveTime(hourOfDay, minute);
                }
            };

    return new TimePickerDialog(context,
            R.style.TimePickerTheme, timeSetListener, hour, minute, false);
}

public interface TimePickerListener {

    void onReceiveTime(int hour, int minute);
}

implement TimePickerListener in your activity.

 @Override
public void onReceiveTime(int hour, int minute) {
    if (validFrom == 1) {
        validFromTime.setText(TimeFormatterUtility.getFormattedTime(hour, minute));
    } else {
        validTillTime.setText(TimeFormatterUtility.getFormattedTime(hour, minute));
    }
}

 <style name="TimePickerTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">@color/buttonColor</item>
    <item name="android:timePickerStyle">@style/TimePickerStyle</item>
</style>

<style name="TimePickerStyle" parent="@android:style/Widget.Material.Light.TimePicker">

    <item name="android:headerBackground">@color/colorPrimary</item>
    <item name="android:timePickerMode">spinner</item>
    <item name="android:headerTimeTextAppearance">@color/colorPrimaryDark</item>
</style>
Varun
  • 214
  • 1
  • 5