You can use this code:
if(config.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
to determine wether the LayoutDirection is RTL or LTR and set the position of the Checkbox programmatically.
Here is an example:
layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#00CCCC"
>
<RelativeLayout
android:id="@+id/parentRelativeLayout"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<CheckBox
android:layout_alignParentRight="true"
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hide...">
</CheckBox>
</RelativeLayout>
</RelativeLayout>
MainActivity.java:
package com.example.androidlayout;
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
CheckBox checkbox;
RelativeLayout parent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkbox = (CheckBox) findViewById(R.id.checkbox);
parent = (RelativeLayout) findViewById(R.id.parentRelativeLayout);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) checkbox.getLayoutParams();
Configuration config = getResources().getConfiguration();
if(config.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
}else{
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
}
checkbox.setLayoutParams(layoutParams);
}
}