Skip to content

Commit 98ee2e1

Browse files
authored
Merge pull request #258 from dpa99c/CB-13791
CB-13791: (android) Add Android support for a footer close button
2 parents c80c338 + b73ba93 commit 98ee2e1

File tree

2 files changed

+111
-73
lines changed

2 files changed

+111
-73
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,13 @@ instance, or the system browser.
112112
- __hidden__: set to `yes` to create the browser and load the page, but not show it. The loadstop event fires when loading is complete. Omit or set to `no` (default) to have the browser open and load normally.
113113
- __clearcache__: set to `yes` to have the browser's cookie cache cleared before the new window is opened
114114
- __clearsessioncache__: set to `yes` to have the session cookie cache cleared before the new window is opened
115-
- __closebuttoncaption__: set to a string to use as the close buttons caption instead of a X. Note that you need to localize this value yourself.
115+
- __closebuttoncaption__: set to a string to use as the close button's caption instead of a X. Note that you need to localize this value yourself.
116116
- __closebuttoncolor__: set to a valid hex color string, for example: `#00ff00`, and it will change the
117117
close button color from default, regardless of being a text or default X. Only has effect if user has location set to `yes`.
118+
- __footer__: set to `yes` to show a close button in the footer similar to the iOS __Done__ button.
119+
The close button will appear the same as for the header hence use __closebuttoncaption__ and __closebuttoncolor__ to set its properties.
120+
- __footercolor__: set to a valid hex color string, for example `#00ff00` or `#CC00ff00` (`#aarrggbb`) , and it will change the footer color from default.
121+
Only has effect if user has __footer__ set to `yes`.
118122
- __hardwareback__: set to `yes` to use the hardware back button to navigate backwards through the `InAppBrowser`'s history. If there is no previous page, the `InAppBrowser` will close. The default value is `yes`, so you must set it to `no` if you want the back button to simply close the InAppBrowser.
119123
- __hidenavigationbuttons__: set to `yes` to hide the navigation buttons on the location toolbar, only has effect if user has location set to `yes`. The default value is `no`.
120124
- __hideurlbar__: set to `yes` to hide the url bar on the location toolbar, only has effect if user has location set to `yes`. The default value is `no`.

src/android/InAppBrowser.java

Lines changed: 106 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ public class InAppBrowser extends CordovaPlugin {
103103
private static final String HIDE_NAVIGATION = "hidenavigationbuttons";
104104
private static final String NAVIGATION_COLOR = "navigationbuttoncolor";
105105
private static final String HIDE_URL = "hideurlbar";
106+
private static final String FOOTER = "footer";
107+
private static final String FOOTER_COLOR = "footercolor";
106108

107-
private static final List customizableOptions = Arrays.asList(CLOSE_BUTTON_CAPTION, TOOLBAR_COLOR, NAVIGATION_COLOR, CLOSE_BUTTON_COLOR);
109+
private static final List customizableOptions = Arrays.asList(CLOSE_BUTTON_CAPTION, TOOLBAR_COLOR, NAVIGATION_COLOR, CLOSE_BUTTON_COLOR, FOOTER_COLOR);
108110

109111
private InAppBrowserDialog dialog;
110112
private WebView inAppWebView;
@@ -129,6 +131,8 @@ public class InAppBrowser extends CordovaPlugin {
129131
private boolean hideNavigationButtons = false;
130132
private String navigationButtonColor = "";
131133
private boolean hideUrlBar = false;
134+
private boolean showFooter = false;
135+
private String footerColor = "";
132136

133137
/**
134138
* Executes the request and returns PluginResult.
@@ -397,11 +401,9 @@ private HashMap<String, String> parseFeature(String optString) {
397401
option = new StringTokenizer(features.nextToken(), "=");
398402
if (option.hasMoreElements()) {
399403
String key = option.nextToken();
400-
String value = null;
401-
if (customizableOptions.contains(key)) value = option.nextToken();
402-
else {
403-
String token = option.nextToken();
404-
value = token.equals("yes") || token.equals("no") ? token : "yes";
404+
String value = option.nextToken();
405+
if (!customizableOptions.contains(key)){
406+
value = value.equals("yes") || value.equals("no") ? value : "yes";
405407
}
406408
map.put(key, value);
407409
}
@@ -431,7 +433,7 @@ public String openExternal(String url) {
431433
intent.putExtra(Browser.EXTRA_APPLICATION_ID, cordova.getActivity().getPackageName());
432434
this.cordova.getActivity().startActivity(intent);
433435
return "";
434-
// not catching FileUriExposedException explicitly because buildtools<24 doesn't know about it
436+
// not catching FileUriExposedException explicitly because buildtools<24 doesn't know about it
435437
} catch (java.lang.RuntimeException e) {
436438
LOG.d(LOG_TAG, "InAppBrowser: Error loading url "+url+":"+ e.toString());
437439
return e.toString();
@@ -561,10 +563,10 @@ public String showWebPage(final String url, HashMap<String, String> features) {
561563
showLocationBar = show.equals("yes") ? true : false;
562564
}
563565
if(showLocationBar) {
564-
String hideNavigation = features.get(HIDE_NAVIGATION);
565-
String hideUrl = features.get(HIDE_URL);
566-
if(hideNavigation != null) hideNavigationButtons = hideNavigation.equals("yes") ? true : false;
567-
if(hideUrl != null) hideUrlBar = hideUrl.equals("yes") ? true : false;
566+
String hideNavigation = features.get(HIDE_NAVIGATION);
567+
String hideUrl = features.get(HIDE_URL);
568+
if(hideNavigation != null) hideNavigationButtons = hideNavigation.equals("yes") ? true : false;
569+
if(hideUrl != null) hideUrlBar = hideUrl.equals("yes") ? true : false;
568570
}
569571
String zoom = features.get(ZOOM);
570572
if (zoom != null) {
@@ -599,15 +601,15 @@ public String showWebPage(final String url, HashMap<String, String> features) {
599601
}
600602
String wideViewPort = features.get(USER_WIDE_VIEW_PORT);
601603
if (wideViewPort != null ) {
602-
useWideViewPort = wideViewPort.equals("yes") ? true : false;
604+
useWideViewPort = wideViewPort.equals("yes") ? true : false;
603605
}
604606
String closeButtonCaptionSet = features.get(CLOSE_BUTTON_CAPTION);
605607
if (closeButtonCaptionSet != null) {
606608
closeButtonCaption = closeButtonCaptionSet;
607609
}
608610
String closeButtonColorSet = features.get(CLOSE_BUTTON_COLOR);
609611
if (closeButtonColorSet != null) {
610-
closeButtonColor = closeButtonColorSet;
612+
closeButtonColor = closeButtonColorSet;
611613
}
612614
String toolbarColorSet = features.get(TOOLBAR_COLOR);
613615
if (toolbarColorSet != null) {
@@ -617,6 +619,14 @@ public String showWebPage(final String url, HashMap<String, String> features) {
617619
if (navigationButtonColorSet != null) {
618620
navigationButtonColor = navigationButtonColorSet;
619621
}
622+
String showFooterSet = features.get(FOOTER);
623+
if (showFooterSet != null) {
624+
showFooter = showFooterSet.equals("yes") ? true : false;
625+
}
626+
String footerColorSet = features.get(FOOTER_COLOR);
627+
if (footerColorSet != null) {
628+
footerColor = footerColorSet;
629+
}
620630
}
621631

622632
final CordovaWebView thatWebView = this.webView;
@@ -630,13 +640,60 @@ public String showWebPage(final String url, HashMap<String, String> features) {
630640
*/
631641
private int dpToPixels(int dipValue) {
632642
int value = (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP,
633-
(float) dipValue,
634-
cordova.getActivity().getResources().getDisplayMetrics()
643+
(float) dipValue,
644+
cordova.getActivity().getResources().getDisplayMetrics()
635645
);
636646

637647
return value;
638648
}
639649

650+
private View createCloseButton(int id){
651+
View _close;
652+
Resources activityRes = cordova.getActivity().getResources();
653+
654+
if (closeButtonCaption != "") {
655+
// Use TextView for text
656+
TextView close = new TextView(cordova.getActivity());
657+
close.setText(closeButtonCaption);
658+
close.setTextSize(20);
659+
if (closeButtonColor != "") close.setTextColor(android.graphics.Color.parseColor(closeButtonColor));
660+
close.setGravity(android.view.Gravity.CENTER_VERTICAL);
661+
close.setPadding(this.dpToPixels(10), 0, this.dpToPixels(10), 0);
662+
_close = close;
663+
}
664+
else {
665+
ImageButton close = new ImageButton(cordova.getActivity());
666+
int closeResId = activityRes.getIdentifier("ic_action_remove", "drawable", cordova.getActivity().getPackageName());
667+
Drawable closeIcon = activityRes.getDrawable(closeResId);
668+
if (closeButtonColor != "") close.setColorFilter(android.graphics.Color.parseColor(closeButtonColor));
669+
close.setImageDrawable(closeIcon);
670+
close.setScaleType(ImageView.ScaleType.FIT_CENTER);
671+
if (Build.VERSION.SDK_INT >= 16)
672+
close.getAdjustViewBounds();
673+
674+
_close = close;
675+
}
676+
677+
RelativeLayout.LayoutParams closeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
678+
closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
679+
_close.setLayoutParams(closeLayoutParams);
680+
681+
if (Build.VERSION.SDK_INT >= 16)
682+
_close.setBackground(null);
683+
else
684+
_close.setBackgroundDrawable(null);
685+
686+
_close.setContentDescription("Close Button");
687+
_close.setId(Integer.valueOf(id));
688+
_close.setOnClickListener(new View.OnClickListener() {
689+
public void onClick(View v) {
690+
closeDialog();
691+
}
692+
});
693+
694+
return _close;
695+
}
696+
640697
@SuppressLint("NewApi")
641698
public void run() {
642699

@@ -741,67 +798,37 @@ public void onClick(View v) {
741798
public boolean onKey(View v, int keyCode, KeyEvent event) {
742799
// If the event is a key-down event on the "enter" button
743800
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
744-
navigate(edittext.getText().toString());
745-
return true;
801+
navigate(edittext.getText().toString());
802+
return true;
746803
}
747804
return false;
748805
}
749806
});
750807

751-
// Close/Done button
752-
if (closeButtonCaption != "") {
753-
// Use TextView for text
754-
TextView close = new TextView(cordova.getActivity());
755-
close.setText(closeButtonCaption);
756-
close.setTextSize(20);
757-
if (closeButtonColor != "") close.setTextColor(android.graphics.Color.parseColor(closeButtonColor));
758-
close.setGravity(android.view.Gravity.CENTER_VERTICAL);
759-
RelativeLayout.LayoutParams closeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
760-
closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
761-
close.setLayoutParams(closeLayoutParams);
762-
763-
close.setContentDescription("Close Button");
764-
close.setId(Integer.valueOf(5));
765-
if (Build.VERSION.SDK_INT >= 16)
766-
close.setBackground(null);
767-
else
768-
close.setBackgroundDrawable(null);
769-
back.setPadding(0, this.dpToPixels(10), 0, this.dpToPixels(10));
770-
close.setPadding(this.dpToPixels(10), 0, this.dpToPixels(10), 0);
771-
close.setOnClickListener(new View.OnClickListener() {
772-
public void onClick(View v) {
773-
closeDialog();
774-
}
775-
});
776-
toolbar.addView(close);
777-
}
778-
else {
779-
ImageButton close = new ImageButton(cordova.getActivity());
780-
RelativeLayout.LayoutParams closeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
781-
closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
782-
close.setLayoutParams(closeLayoutParams);
783-
close.setContentDescription("Close Button");
784-
close.setId(Integer.valueOf(5));
785-
int closeResId = activityRes.getIdentifier("ic_action_remove", "drawable", cordova.getActivity().getPackageName());
786-
Drawable closeIcon = activityRes.getDrawable(closeResId);
787-
if (closeButtonColor != "") close.setColorFilter(android.graphics.Color.parseColor(closeButtonColor));
788-
if (Build.VERSION.SDK_INT >= 16)
789-
close.setBackground(null);
790-
else
791-
close.setBackgroundDrawable(null);
792-
close.setImageDrawable(closeIcon);
793-
close.setScaleType(ImageView.ScaleType.FIT_CENTER);
794-
back.setPadding(0, this.dpToPixels(10), 0, this.dpToPixels(10));
795-
if (Build.VERSION.SDK_INT >= 16)
796-
close.getAdjustViewBounds();
797-
798-
close.setOnClickListener(new View.OnClickListener() {
799-
public void onClick(View v) {
800-
closeDialog();
801-
}
802-
});
803-
toolbar.addView(close);
808+
809+
// Header Close/Done button
810+
View close = createCloseButton(5);
811+
toolbar.addView(close);
812+
813+
// Footer
814+
RelativeLayout footer = new RelativeLayout(cordova.getActivity());
815+
int _footerColor;
816+
if(footerColor != ""){
817+
_footerColor = Color.parseColor(footerColor);
818+
}else{
819+
_footerColor = android.graphics.Color.LTGRAY;
804820
}
821+
footer.setBackgroundColor(_footerColor);
822+
RelativeLayout.LayoutParams footerLayout = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, this.dpToPixels(44));
823+
footerLayout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
824+
footer.setLayoutParams(footerLayout);
825+
if (closeButtonCaption != "") footer.setPadding(this.dpToPixels(8), this.dpToPixels(8), this.dpToPixels(8), this.dpToPixels(8));
826+
footer.setHorizontalGravity(Gravity.LEFT);
827+
footer.setVerticalGravity(Gravity.BOTTOM);
828+
829+
View footerClose = createCloseButton(7);
830+
footer.addView(footerClose);
831+
805832

806833
// WebView
807834
inAppWebView = new WebView(cordova.getActivity());
@@ -915,7 +942,14 @@ public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType)
915942
}
916943

917944
// Add our webview to our main view/layout
918-
main.addView(inAppWebView);
945+
RelativeLayout webViewLayout = new RelativeLayout(cordova.getActivity());
946+
webViewLayout.addView(inAppWebView);
947+
main.addView(webViewLayout);
948+
949+
// Don't add the footer unless it's been enabled
950+
if (showFooter) {
951+
webViewLayout.addView(footer);
952+
}
919953

920954
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
921955
lp.copyFrom(dialog.getWindow().getAttributes());
@@ -1105,7 +1139,7 @@ public void onPageStarted(WebView view, String url, Bitmap favicon) {
11051139
// Update the UI if we haven't already
11061140
if (!newloc.equals(edittext.getText().toString())) {
11071141
edittext.setText(newloc);
1108-
}
1142+
}
11091143

11101144
try {
11111145
JSONObject obj = new JSONObject();

0 commit comments

Comments
 (0)