Android: Change time separator in standard TimePicker?

How to change time separator from standard. Q: in an Android app? I am using minSDK 15 and targetSDK 20.

time separator

+3


source to share


1 answer


This answer talks about the TimePicker class as the OP points out that as a requirement in the bounty and has additions as the element has slightly reworked the code with the Android 5 / Lollipop release.


How to do it?

The delimiter text cannot be simply changed with a simple simple call. The delimiter text is internally set to TimePicker.setDividerText()

, which is a private method, which means it cannot be overridden. To make it even harder, the variable textview divider is mDivider

set to private and also means that we don't have direct access to the textview divider .

A simple solution to the problem is to simply override the value after it has been set by the constructor. To do this, we create our own TimePicker subclass, try to find the separator and set its value. This will work fine as long as the layout is AOSP compliant. looks at OEM skins and shrugs

See the source for the android.widget.TimePicker class for reference . Assuming the android-l-preview_r2 tag is being examined, check line 91 for the variable declaration mDivider

and lines 531-553 for the method setDividerText()

.

In practice, please?

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.widget.TextView;
import android.widget.TimePicker;

public class CustomTimePicker extends TimePicker {
    public CustomTimePicker(Context context) {
        super(context);
        init();
    }

    public CustomTimePicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomTimePicker(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        TextView divider = (TextView) findViewById(Resources.getSystem()
                .getIdentifier("divider", "id", "android"));
        // the divider doesn't exist in the old-school widget style
        if (divider != null) {
            divider.setText(":");
        }
    }
}

      

Which doesn't work on Android L!



The described method is valid for KitKat, but no longer suitable for compiling with L. If you check TimePicker on android-5.0.0_r2 , you find that style handling is dumped into a separate delegate class. The TimePickerClockDelegate class seems to be like a legacy collector.

Problems with private variables and methods are still present in Android 5.0.0, and the new structuring complicates things overall. If you still think you want to break the localization (see the section “Should you do that?” Below), you are probably best off using a custom TimePicker implementation. A good starting point is choosing the new TimePicker and TimePickerClockDelegate classes, combining them and cutting the abstraction layer, and changing the code for setting the separator text, as was done in the KitKat version. No matter what goes beyond a simple and helpful answer, since taking this route would lead to many side effects such as breaking system wide topics, and should really be considered reasonable.


Should you do this?

Not really.

TimePicker is actually going to pick a character :

as the default delimiter in case something goes wrong, but it prefers a localized change over everything.

This means that if the locale you are using prefers to write the time as HH:mm

, the collector will automatically be used :

as the separator, but if your language HH.mm

does, then the collector will simply be used .

as the separator. If there was a locale that records the time in the format HH/mm

, the collector would automatically adapt and use it /

as a separator to ensure consistency with local formatting (aka localization).

If you feel like you should override this behavior, you can use the above method, but keep in mind that this is against localization and even consistency across applications.

+8


source







All Articles