Solution 1 :
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);
}
}
Problem :
I Have a Relative layout and its direction based on app language (there are two directions: LTR and RTL), and I have one view (CheckBox) inside that layout.
now I want to make the Checbox direction to the opposite direction to its layout.
Any advice?
Edit
when be the layout direction is LTR I want to make this checkbox direction is RTL, as well when be the layout direction is RTL I want to make this checkbox direction is LTR.
Comments
Comment posted by Mbuodile Obiosio
You should add the code of what you’re trying to do and possibly an image to indicate