Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date format changing on RTL devices?

I have a date field that is presented great on LTR devices (android 7.1.1)
The issue is that on RTL devices the date field content is mirrored

So on US devices the date will appear normally : 03/14/2019
On RTL android devices the date will appear like so: 2019/14/03

Code for the TextView holding the value:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/inspect_date"
            android:textSize="18sp" />

        <com.xerox.printerinspection.controls.DateEditText
            android:id="@+id/edit_inspect_date"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@color/lightGray"
            android:paddingStart="4dp"
            android:paddingEnd="4dp"
            android:drawableEnd="@android:drawable/ic_menu_my_calendar" />
    </LinearLayout>

I am setting the date like so:

   Date currentDate = Calendar.getInstance().getTime();
   inspectionDateEdit.setDate(currentDate);

What will be the correct way to fix this issue?

UPDATE

Parent fragment_detail.xml tag looks like this:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/lightGray"
    android:textDirection="locale"
    tools:context="com.xerox.MainActivity">
like image 201
JavaSheriff Avatar asked Mar 23 '19 01:03

JavaSheriff


4 Answers

Due to the parent's android:textDirection="locale" attribute (which might even be the default value, when having set android:supportsRtl="true" in the Manifest.xml - and could therefore be omitted), one has to define the text-direction for the nested child node - so it won't inherit the parent's value; this at least works for an EditText, when forcing RTL layout on a LTR device:

<androidx.appcompat.widget.AppCompatEditText
    android:id="@+id/edit_inspect_date"
    android:text="@string/edit_inspect_date"
    android:textDirection="ltr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

screenshot

Possibly the android:drawableEnd should be android:drawableRight, so that it would not be affected (this depends upon, if it is intended to have it flipped to the other side, whether or or not).

like image 122
Martin Zeitler Avatar answered Oct 19 '22 06:10

Martin Zeitler


You simply can format the Date to the form what you want and don't worry about it, like this:

SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
String dateFormat = format.format(YourDate);
like image 25
Raúl Sánchez Avatar answered Oct 19 '22 08:10

Raúl Sánchez


Firstly, identify if the layout direction is Right to Left or not.

boolean isRightToLeft =
  TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) ==
  ViewCompat.LAYOUT_DIRECTION_RTL;

if it is, then just reverse the date format yourself,

SimpleDateFormat format;
Date currentDate = Calendar.getInstance().getTime();
if (isRightToLeft) {
  // yup, use the mirrored date format
  format = new SimpleDateFormat("yyyy/dd/MM");
} else {
  // use the regular date format
  format = new SimpleDateFormat("MM/dd/yyyy");
}
currentDate.setTime(simpleDateFormat.parse(simpleDateFormat.format(currentDate)));
inspectionDateEdit.setDate(currentDate);

At run time, os will then mirror your yyyy/dd/MM to MM/dd/yyyy

like image 2
Darshan Miskin Avatar answered Oct 19 '22 08:10

Darshan Miskin


You can use this form

SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
like image 1
Amirhf Avatar answered Oct 19 '22 08:10

Amirhf