MRodrigues
MRodrigues

Reputation: 2025

Update Textclock when changing locale

I'm using a textClock widget in my app layout. I must support different languages, and add dateformat must be full date like "EEEE, dd MMMM yyyy HH:mm:ss a". The problem is that the days name and Months stays always with the default locale. When I change locale programatically it does not change.

Any suggestion? Shouldn't this be handled automatically?

Upvotes: 0

Views: 2251

Answers (3)

Karen Kirakosyan
Karen Kirakosyan

Reputation: 1

I removed format12Hour or format24Hour attributes from xml and updated my code just like this

digitalClock = view.findViewById(R.id.my_id);
textClock.setFormat12Hour("EEE, MMM dd");
textClock.setFormat24Hour("EEE, MMM dd");

after changing languages it works fine

Upvotes: 0

MRodrigues
MRodrigues

Reputation: 2025

It wont't work with the default widget. So I used my Own class and its working as expected. You just have to make sure to change your locale in the right way.

   public class MyTextClock extends android.widget.TextClock {


    public MyTextClock(Context context) {
        super(context);
        setLocaleDateFormat();
    }

    public MyTextClock(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLocaleDateFormat();
    }

    public MyTextClock(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setLocaleDateFormat();
    }

    private void setLocaleDateFormat()
    {
        Locale currentLocale = getResources().getConfiguration().locale;
        Calendar cal = GregorianCalendar.getInstance(TimeZone.getDefault(), currentLocale);

        String dayName = cal.getDisplayName(cal.DAY_OF_WEEK, Calendar.LONG, currentLocale);
        String monthName =cal.getDisplayName(cal.MONTH, Calendar.LONG, currentLocale);

        this.setFormat12Hour("'"+dayName+"' '"+monthName+"' dd, yyyy h:m:ss a");
        this.setFormat24Hour("'"+dayName+"', dd '"+monthName+"' yyyy HH:mm:ss");
    }
}

Upvotes: 1

arodriguezdonaire
arodriguezdonaire

Reputation: 5563

Try this using public void setTextLocale (Locale locale):

Resources res = context.getResources();
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale(language_code.toLowerCase());
yourTextClock.setTextLocale(conf.locale);
res.updateConfiguration(conf, dm);

Upvotes: 2

Related Questions