Add PM, am, or upper afternoon based on different languages on statusbar

Source: Internet
Author: User
Tags time zones

 

The android native system does not display the time in statusbar in 12 hours. Now, add this function to him. When it is in Chinese, add it to the left, and when it is in other languages, add it to the right.

System native image:

Modified style:

If the font size is the same as that of the time, you can directly

platform\frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\Clock.java

Modify the output result in this Code.

However, if you want to make the display time slightly lower in the last afternoon, you can modify the layout file and add two la s to display Chinese and English respectively.

platform\frameworks\base\packages\SystemUI\res\layout\status_bar.xml

        <com.android.systemui.statusbar.IconMerger android:id="@+id/notificationIcons"            android:layout_width="0dip"            android:layout_weight="1"            android:layout_height="match_parent"            android:layout_alignParentLeft="true"            android:paddingLeft="6dip"            android:gravity="center_vertical"            android:orientation="horizontal"/>                      <LinearLayout android:id="@+id/statusIcons"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:layout_alignParentRight="true"            android:paddingRight="6dip"            android:gravity="center_vertical"            android:orientation="horizontal"/>    <com.android.systemui.statusbar.ChineseTextClock
android:layout_width="wrap_content"android:layout_height="match_parent"android:textSize="14sp"android:gravity="bottom"/>        <com.android.systemui.statusbar.Clock            android:textAppearance="@*android:style/TextAppearance.StatusBar.Icon"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:singleLine="true"            android:paddingRight="6dip"            android:gravity="center_vertical|left"            /><com.android.systemui.statusbar.EnglishTextClockandroid:layout_width="wrap_content"android:layout_height="match_parent"android:textSize="14sp"android:gravity="bottom"/>    </LinearLayout>

In the code, we can determine whether the time is 12-hour or 24-hour, and display the time in different languages and time zones, and refresh the data in real time.

Public class chinesetextclock extends textview {private Boolean mattached; private string TZ; private time t; private calendar mcalendar; private string mclockformatstring; private simpledateformat mclockformat; Private Static final int am_pm_style_normal = 0; private Static final int am_pm_style_small = 1; Private Static final int am_pm_style_gone = 2; Private Static final int am_pm_style = am_pm_style_gon E; Public chinesetextclock (context) {This (context, null);} public chinesetextclock (context, attributeset attrs) {This (context, attrs, 0 );} public chinesetextclock (context, attributeset attrs, int defstyle) {super (context, attrs, defstyle) ;}@ overrideprotected void onattachedtowindow () {super. onattachedtowindow (); If (! Mattached) {mattached = true; intentfilter filter = new intentfilter (); filter. addaction (intent. action_time_tick); filter. addaction (intent. action_time_changed); filter. addaction (intent. action_timezone_changed); filter. addaction (intent. action_configuration_changed); filter. addaction (intent. action_locale_changed); // you can change the displayed String Based on Time Zone, time, configuration, and language.
Getcontext (). registerreceiver (mintentreceiver, filter, null, gethandler ();} mcalendar = calendar. getinstance (timezone. getdefault (); updateclock () ;}@ overrideprotected void ondetachedfromwindow () {super. ondetachedfromwindow (); If (mattached) {getcontext (). unregisterreceiver (mintentreceiver); mattached = false ;}} private final broadcastreceiver mintentreceiver = new broadcastreceiver () {@ overridepublic Vo Id onreceive (context, intent) {string action = intent. getaction (); If (action. equals (intent. action_timezone_changed) {string TZ = intent. getstringextra ("time-zone"); mcalendar = calendar. getinstance (timezone. gettimezone (Tz); If (mclockformat! = NULL) {mclockformat. settimezone (mcalendar. gettimezone ();} updateclock ();} else if (action. equals (intent. action_time_changed) {updateclock ();} else if (action. equals (intent. action_locale_changed) {updateclock ();} else if (action. equals (intent. action_time_tick) {updateclock () ;}}; final void updateclock () {// obtain the time display mode in the system, in 12-hour or 24-hour format
Context context = getContext();boolean b24 = DateFormat.is24HourFormat(context);
mCalendar.setTimeInMillis(System.currentTimeMillis());Locale locale = Locale.getDefault();String strLocale = "" + locale;if(strLocale.equals("zh_CN")){if(!b24){if (tz != null) {t = new Time(tz);} else {t = new Time();}t.setToNow();int hourTime = t.hour;if(hourTime<=11){setText(R.string.am);}else{setText(R.string.pm);}}else{setText("");}}else{setText("");}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.