Skip to content

Commit ba3a440

Browse files
authored
Added to support android devices with right to left direction languages (issue #352)
1 parent 0fd43ae commit ba3a440

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/android/InAppBrowser.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ Licensed to the Apache Software Foundation (ASF) under one
8181
import java.util.HashMap;
8282
import java.util.StringTokenizer;
8383

84+
import android.content.res.Configuration;
85+
8486
@SuppressLint("SetJavaScriptEnabled")
8587
public class InAppBrowser extends CordovaPlugin {
8688

@@ -741,8 +743,10 @@ private View createCloseButton(int id){
741743
_close = close;
742744
}
743745

746+
Configuration config = activityRes.getConfiguration();
747+
boolean isLeftToRight = config.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR;
744748
RelativeLayout.LayoutParams closeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
745-
closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
749+
closeLayoutParams.addRule(isLeftToRight ? RelativeLayout.ALIGN_PARENT_RIGHT : RelativeLayout.ALIGN_PARENT_LEFT);
746750
_close.setLayoutParams(closeLayoutParams);
747751

748752
if (Build.VERSION.SDK_INT >= 16)
@@ -769,6 +773,9 @@ public void run() {
769773
dialog.dismiss();
770774
};
771775

776+
Configuration config = cordova.getActivity().getResources().getConfiguration();
777+
boolean isLeftToRight = config.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR;
778+
772779
// Let's create the main dialog
773780
dialog = new InAppBrowserDialog(cordova.getActivity(), android.R.style.Theme_NoTitleBar);
774781
dialog.getWindow().getAttributes().windowAnimations = android.R.style.Animation_Dialog;
@@ -786,25 +793,25 @@ public void run() {
786793
toolbar.setBackgroundColor(toolbarColor);
787794
toolbar.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, this.dpToPixels(44)));
788795
toolbar.setPadding(this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2));
789-
toolbar.setHorizontalGravity(Gravity.LEFT);
796+
toolbar.setHorizontalGravity(isLeftToRight ? Gravity.LEFT : Gravity.RIGHT);
790797
toolbar.setVerticalGravity(Gravity.TOP);
791798

792799
// Action Button Container layout
793800
RelativeLayout actionButtonContainer = new RelativeLayout(cordova.getActivity());
794801
actionButtonContainer.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
795-
actionButtonContainer.setHorizontalGravity(Gravity.LEFT);
802+
actionButtonContainer.setHorizontalGravity(isLeftToRight ? Gravity.LEFT : Gravity.RIGHT);
796803
actionButtonContainer.setVerticalGravity(Gravity.CENTER_VERTICAL);
797804
actionButtonContainer.setId(Integer.valueOf(1));
798805

799806
// Back button
800807
ImageButton back = new ImageButton(cordova.getActivity());
801808
RelativeLayout.LayoutParams backLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
802-
backLayoutParams.addRule(RelativeLayout.ALIGN_LEFT);
809+
backLayoutParams.addRule(isLeftToRight ? RelativeLayout.ALIGN_LEFT : RelativeLayout.ALIGN_RIGHT);
803810
back.setLayoutParams(backLayoutParams);
804811
back.setContentDescription("Back Button");
805812
back.setId(Integer.valueOf(2));
806813
Resources activityRes = cordova.getActivity().getResources();
807-
int backResId = activityRes.getIdentifier("ic_action_previous_item", "drawable", cordova.getActivity().getPackageName());
814+
int backResId = activityRes.getIdentifier(isLeftToRight ? "ic_action_previous_item" : "ic_action_next_item", "drawable", cordova.getActivity().getPackageName());
808815
Drawable backIcon = activityRes.getDrawable(backResId);
809816
if (navigationButtonColor != "") back.setColorFilter(android.graphics.Color.parseColor(navigationButtonColor));
810817
if (Build.VERSION.SDK_INT >= 16)
@@ -826,11 +833,11 @@ public void onClick(View v) {
826833
// Forward button
827834
ImageButton forward = new ImageButton(cordova.getActivity());
828835
RelativeLayout.LayoutParams forwardLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
829-
forwardLayoutParams.addRule(RelativeLayout.RIGHT_OF, 2);
836+
forwardLayoutParams.addRule(isLeftToRight ? RelativeLayout.RIGHT_OF : RelativeLayout.LEFT_OF, 2);
830837
forward.setLayoutParams(forwardLayoutParams);
831838
forward.setContentDescription("Forward Button");
832839
forward.setId(Integer.valueOf(3));
833-
int fwdResId = activityRes.getIdentifier("ic_action_next_item", "drawable", cordova.getActivity().getPackageName());
840+
int fwdResId = activityRes.getIdentifier(isLeftToRight ? "ic_action_next_item" : "ic_action_previous_item", "drawable", cordova.getActivity().getPackageName());
834841
Drawable fwdIcon = activityRes.getDrawable(fwdResId);
835842
if (navigationButtonColor != "") forward.setColorFilter(android.graphics.Color.parseColor(navigationButtonColor));
836843
if (Build.VERSION.SDK_INT >= 16)
@@ -852,8 +859,8 @@ public void onClick(View v) {
852859
// Edit Text Box
853860
edittext = new EditText(cordova.getActivity());
854861
RelativeLayout.LayoutParams textLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
855-
textLayoutParams.addRule(RelativeLayout.RIGHT_OF, 1);
856-
textLayoutParams.addRule(RelativeLayout.LEFT_OF, 5);
862+
textLayoutParams.addRule(isLeftToRight ? RelativeLayout.RIGHT_OF : RelativeLayout.LEFT_OF, 1);
863+
textLayoutParams.addRule(isLeftToRight ? RelativeLayout.LEFT_OF : RelativeLayout.RIGHT_OF, 5);
857864
edittext.setLayoutParams(textLayoutParams);
858865
edittext.setId(Integer.valueOf(4));
859866
edittext.setSingleLine(true);
@@ -890,7 +897,7 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
890897
footerLayout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
891898
footer.setLayoutParams(footerLayout);
892899
if (closeButtonCaption != "") footer.setPadding(this.dpToPixels(8), this.dpToPixels(8), this.dpToPixels(8), this.dpToPixels(8));
893-
footer.setHorizontalGravity(Gravity.LEFT);
900+
footer.setHorizontalGravity(isLeftToRight ? Gravity.LEFT : Gravity.RIGHT);
894901
footer.setVerticalGravity(Gravity.BOTTOM);
895902

896903
View footerClose = createCloseButton(7);

0 commit comments

Comments
 (0)