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">
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"/>
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).
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);
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
You can use this form
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With