Skip to content

Commit a87650a

Browse files
committed
feat(message): Custom width for Banner(LP-4382).
1 parent bf2b858 commit a87650a

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

AndroidSDK/src/com/leanplum/messagetemplates/BaseMessageDialog.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,25 @@ private RelativeLayout createContainerView(Activity context, boolean fullscreen)
243243
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
244244
} else if (isHtml) {
245245
int height = SizeUtil.dpToPx(context, htmlOptions.getHtmlHeight());
246-
layoutParams = new RelativeLayout.LayoutParams(
247-
LayoutParams.MATCH_PARENT, height);
246+
String widthType = htmlOptions.getHtmlWidthType();
247+
if (TextUtils.isEmpty(widthType)) {
248+
layoutParams = new RelativeLayout.LayoutParams(
249+
LayoutParams.MATCH_PARENT, height);
250+
} else {
251+
int width = htmlOptions.getHtmlWidth();
252+
if ("%".equals(widthType)) {
253+
Display display = context.getWindowManager().getDefaultDisplay();
254+
Point size = new Point();
255+
display.getSize(size);
256+
width = size.x * width / 100;
257+
} else {
258+
width = SizeUtil.dpToPx(context, width);
259+
}
260+
layoutParams = new RelativeLayout.LayoutParams(
261+
width, height);
262+
}
263+
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
248264
} else {
249-
250265
// Make sure the dialog fits on screen.
251266
Display display = context.getWindowManager().getDefaultDisplay();
252267
Point size = new Point();
@@ -268,8 +283,10 @@ private RelativeLayout createContainerView(Activity context, boolean fullscreen)
268283

269284
layoutParams = new RelativeLayout.LayoutParams(width, height);
270285
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
286+
271287
}
272288

289+
273290
view.setLayoutParams(layoutParams);
274291

275292
ShapeDrawable footerBackground = new ShapeDrawable();

AndroidSDK/src/com/leanplum/messagetemplates/HTMLOptions.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class HTMLOptions {
5252
private ActionContext actionContext;
5353
private String htmlAlign;
5454
private int htmlHeight;
55+
private int htmlWidth;
56+
private String htmlWidthType;
5557

5658
HTMLOptions(ActionContext context) {
5759
this.setActionContext(context);
@@ -63,6 +65,7 @@ class HTMLOptions {
6365
this.setTrackActionUrl(context.stringNamed(MessageTemplates.Args.TRACK_ACTION_URL));
6466
this.setHtmlAlign(context.stringNamed(MessageTemplates.Args.HTML_ALIGN));
6567
this.setHtmlHeight(context.numberNamed(MessageTemplates.Args.HTML_HEIGHT).intValue());
68+
this.setHtmlWidth(context.stringNamed(MessageTemplates.Args.HTML_WIDTH));
6669
}
6770

6871
/**
@@ -193,6 +196,27 @@ int getHtmlHeight() {
193196
return htmlHeight;
194197
}
195198

199+
int getHtmlWidth() {
200+
return htmlWidth;
201+
}
202+
203+
String getHtmlWidthType() {
204+
return htmlWidthType;
205+
}
206+
207+
private void setHtmlWidth(String htmlWidth) {
208+
if (TextUtils.isEmpty(htmlWidth)) {
209+
return;
210+
}
211+
if (htmlWidth.contains("px")) {
212+
this.htmlWidth = Integer.parseInt(htmlWidth.split("px")[0]);
213+
this.htmlWidthType = "px";
214+
} else if (htmlWidth.contains("%")) {
215+
this.htmlWidth = Integer.parseInt(htmlWidth.split("%")[0]);
216+
this.htmlWidthType = "%";
217+
}
218+
}
219+
196220
private void setHtmlHeight(int htmlHeight) {
197221
this.htmlHeight = htmlHeight;
198222
}

AndroidSDK/src/com/leanplum/messagetemplates/MessageTemplates.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ static class Args {
5555
static final String BACKGROUND_COLOR = "Background color";
5656
static final String LAYOUT_WIDTH = "Layout.Width";
5757
static final String LAYOUT_HEIGHT = "Layout.Height";
58+
static final String HTML_WIDTH = "HTML Width";
5859
static final String HTML_HEIGHT = "HTML Height";
5960
static final String HTML_ALIGN = "HTML Align";
6061
static final String HTML_ALIGN_TOP = "Top";

0 commit comments

Comments
 (0)