Skip to content

Commit f4e2d5f

Browse files
committed
Added the possibility to use theme attributes in order to style dialogs.
1 parent 0b445e0 commit f4e2d5f

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

library/src/main/java/de/mrapp/android/dialog/MaterialDialog.java

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,127 @@ public static class Builder {
258258
*/
259259
private View customTitleView;
260260

261+
/**
262+
* Obtains all relevant attributes from the current theme.
263+
*/
264+
private void obtainStyledAttributes() {
265+
int themeId = themeResourceId != -1 ? themeResourceId : 0;
266+
obtainBackground(themeId);
267+
obtainButtonTextColor(themeId);
268+
obtainItemColor(themeId);
269+
obtainItemControlColor(themeId);
270+
obtainMessageColor(themeId);
271+
obtainTitleColor(themeId);
272+
}
273+
274+
/**
275+
* Obtains the background from a specific theme.
276+
*
277+
* @param themeId
278+
* The id of the theme, the background should be obtained from, as an {@link
279+
* Integer} value
280+
*/
281+
private void obtainBackground(@StyleRes final int themeId) {
282+
TypedArray typedArray = getContext().getTheme()
283+
.obtainStyledAttributes(themeId, new int[]{R.attr.materialDialogBackground});
284+
int color = typedArray.getColor(0, -1);
285+
286+
if (color != -1) {
287+
setBackgroundColor(color);
288+
} else {
289+
int resourceId = typedArray.getResourceId(0, 0);
290+
291+
if (resourceId != 0) {
292+
setBackground(resourceId);
293+
}
294+
}
295+
}
296+
297+
/**
298+
* Obtains the button text color from a specific theme.
299+
*
300+
* @param themeId
301+
* The id of the theme, the button text color should be obtained from, as an {@link
302+
* Integer} value
303+
*/
304+
private void obtainButtonTextColor(@StyleRes final int themeId) {
305+
TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(themeId,
306+
new int[]{R.attr.materialDialogButtonTextColor});
307+
int color = typedArray.getColor(0, -1);
308+
309+
if (color != -1) {
310+
setButtonTextColor(color);
311+
}
312+
}
313+
314+
/**
315+
* Obtains the item color from a specific theme.
316+
*
317+
* @param themeId
318+
* The id of the theme, the item color should be obtained from, as an {@link
319+
* Integer} value
320+
*/
321+
private void obtainItemColor(@StyleRes final int themeId) {
322+
TypedArray typedArray = getContext().getTheme()
323+
.obtainStyledAttributes(themeId, new int[]{R.attr.materialDialogItemColor});
324+
int color = typedArray.getColor(0, -1);
325+
326+
if (color != -1) {
327+
setItemColor(color);
328+
}
329+
}
330+
331+
/**
332+
* Obtains the item control color from a specific theme.
333+
*
334+
* @param themeId
335+
* The id of the theme, the item control color should be obtained from, as an {@link
336+
* Integer} value
337+
*/
338+
private void obtainItemControlColor(@StyleRes final int themeId) {
339+
TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(themeId,
340+
new int[]{R.attr.materialDialogItemControlColor});
341+
int color = typedArray.getColor(0, -1);
342+
343+
if (color != -1) {
344+
setItemControlColor(color);
345+
}
346+
}
347+
348+
/**
349+
* Obtains the message color from a specific theme.
350+
*
351+
* @param themeId
352+
* The id of the theme, the message color should be obtained from, as an {@link
353+
* Integer} value
354+
*/
355+
private void obtainMessageColor(@StyleRes final int themeId) {
356+
TypedArray typedArray = getContext().getTheme()
357+
.obtainStyledAttributes(themeId, new int[]{R.attr.materialDialogMessageColor});
358+
int color = typedArray.getColor(0, -1);
359+
360+
if (color != -1) {
361+
setMessageColor(color);
362+
}
363+
}
364+
365+
/**
366+
* Obtains the title color from a specific theme.
367+
*
368+
* @param themeId
369+
* The id of the theme, the title color should be obtained from, as an {@link
370+
* Integer} value
371+
*/
372+
private void obtainTitleColor(@StyleRes final int themeId) {
373+
TypedArray typedArray = getContext().getTheme()
374+
.obtainStyledAttributes(themeId, new int[]{R.attr.materialDialogTitleColor});
375+
int color = typedArray.getColor(0, -1);
376+
377+
if (color != -1) {
378+
setTitleColor(color);
379+
}
380+
}
381+
261382
/**
262383
* Inflates the dialog's layout.
263384
*
@@ -710,6 +831,7 @@ public Builder(@NonNull final Context context) {
710831
this.context = context;
711832
this.themeResourceId = -1;
712833
this.validators = new LinkedHashSet<>();
834+
obtainStyledAttributes();
713835
}
714836

715837
/**
@@ -727,6 +849,7 @@ public Builder(@NonNull final Context context, @StyleRes final int themeResource
727849
this.context = context;
728850
this.themeResourceId = themeResourceId;
729851
this.validators = new LinkedHashSet<>();
852+
obtainStyledAttributes();
730853
}
731854

732855
/**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<!--
4+
AndroidMaterialDialog Copyright 2014 - 2015 Michael Rapp
5+
6+
This program is free software: you can redistribute it and/or modify it under the terms of the GNU
7+
Lesser General Public License as published by the Free Software Foundation, either version 3 of the
8+
License, or (at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
11+
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public License along with this program. If
15+
not, see <http://www.gnu.org/licenses/>.
16+
-->
17+
<resources>
18+
19+
<attr name="materialDialogBackground" format="color|reference"/>
20+
<attr name="materialDialogButtonTextColor" format="color"/>
21+
<attr name="materialDialogItemColor" format="color"/>
22+
<attr name="materialDialogItemControlColor" format="color"/>
23+
<attr name="materialDialogMessageColor" format="color"/>
24+
<attr name="materialDialogTitleColor" format="color"/>
25+
26+
</resources>

0 commit comments

Comments
 (0)