Skip to content

Commit 1127f83

Browse files
committed
Use CharSequence, allows for spannable strings
closes #78
1 parent 5a1d584 commit 1127f83

7 files changed

Lines changed: 43 additions & 37 deletions

File tree

simpledialogfragments/src/main/java/eltos/simpledialogfragment/CustomViewDialog.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,15 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
270270
dialog.setView(intermediate);
271271

272272

273-
String msg = getMessage();
273+
CharSequence msg = getMessage();
274274
if (msg != null) {
275275
CharSequence message;
276-
if (getArgs().getBoolean(HTML)) {
276+
if (getArgs().getBoolean(HTML) && msg instanceof String) {
277277
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
278-
message = Html.fromHtml(msg, 0);
278+
message = Html.fromHtml((String) msg, 0);
279279
} else {
280280
//noinspection deprecation
281-
message = Html.fromHtml(msg);
281+
message = Html.fromHtml((String) msg);
282282
}
283283
} else {
284284
message = msg;

simpledialogfragments/src/main/java/eltos/simpledialogfragment/SimpleCheckDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static SimpleCheckDialog build(){
5454
* @param checkBoxLabel the label as string
5555
* @return this instance
5656
*/
57-
public SimpleCheckDialog label(String checkBoxLabel){ return setArg(CHECKBOX_LABEL, checkBoxLabel); }
57+
public SimpleCheckDialog label(CharSequence checkBoxLabel){ return setArg(CHECKBOX_LABEL, checkBoxLabel); }
5858

5959
/**
6060
* Sets the checkbox's label

simpledialogfragments/src/main/java/eltos/simpledialogfragment/SimpleDialog.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ protected final This setArg(String key, boolean value){
134134
return (This) this;
135135
}
136136
@SuppressWarnings("unchecked cast")
137-
protected final This setArg(String key, String value){
138-
getArgs().putString(key, value);
137+
protected final This setArg(String key, CharSequence value){
138+
getArgs().putCharSequence(key, value);
139139
return (This) this;
140140
}
141141
@SuppressWarnings("unchecked cast")
@@ -149,10 +149,10 @@ protected final This setArg(String key, long value){
149149
return (This) this;
150150
}
151151
@Nullable
152-
protected final String getArgString(String key){
152+
protected final CharSequence getArgString(String key){
153153
Object value = getArgs().get(key);
154-
if (value instanceof String){
155-
return (String) value;
154+
if (value instanceof CharSequence){
155+
return (CharSequence) value;
156156
} else if (value instanceof Integer){
157157
return getString((Integer) value);
158158
}
@@ -179,7 +179,7 @@ protected final Bundle getArgs(){
179179
* @param title the title as string
180180
* @return this instance
181181
*/
182-
public This title(String title){ return setArg(TITLE, title); }
182+
public This title(CharSequence title){ return setArg(TITLE, title); }
183183

184184
/**
185185
* Sets this dialogs title
@@ -193,7 +193,7 @@ protected final Bundle getArgs(){
193193
* Gets the string representation of the title set
194194
* @return the dialog title
195195
*/
196-
public @Nullable String getTitle(){
196+
public @Nullable CharSequence getTitle(){
197197
return getArgString(TITLE);
198198
}
199199

@@ -203,15 +203,15 @@ protected final Bundle getArgs(){
203203
* @param message title as string
204204
* @return this instance
205205
*/
206-
public This msg(String message){ return setArg(MESSAGE, message); }
206+
public This msg(CharSequence message){ setArg(HTML, false); return setArg(MESSAGE, message); }
207207

208208
/**
209209
* Sets this dialogs message
210210
*
211211
* @param messageResourceId the message as android string resource
212212
* @return this instance
213213
*/
214-
public This msg(@StringRes int messageResourceId){ return setArg(MESSAGE, messageResourceId); }
214+
public This msg(@StringRes int messageResourceId){ setArg(HTML, false); return setArg(MESSAGE, messageResourceId); }
215215

216216
/**
217217
* Sets this dialogs message as html styled string
@@ -233,7 +233,7 @@ protected final Bundle getArgs(){
233233
* Gets the string representation of the message set
234234
* @return the dialog message
235235
*/
236-
public @Nullable String getMessage(){
236+
public @Nullable CharSequence getMessage(){
237237
return getArgString(MESSAGE);
238238
}
239239

@@ -243,7 +243,7 @@ protected final Bundle getArgs(){
243243
* @param positiveButton the text as string
244244
* @return this instance
245245
*/
246-
public This pos(String positiveButton){ return setArg(POSITIVE_BUTTON_TEXT, positiveButton); }
246+
public This pos(CharSequence positiveButton){ return setArg(POSITIVE_BUTTON_TEXT, positiveButton); }
247247

248248
/**
249249
* Sets this dialogs positive button text
@@ -259,7 +259,7 @@ protected final Bundle getArgs(){
259259
* @param negativeButton the text as string
260260
* @return this instance
261261
*/
262-
public This neg(String negativeButton){ return setArg(NEGATIVE_BUTTON_TEXT, negativeButton); }
262+
public This neg(CharSequence negativeButton){ return setArg(NEGATIVE_BUTTON_TEXT, negativeButton); }
263263

264264
/**
265265
* Sets this dialogs negative button text
@@ -282,7 +282,7 @@ protected final Bundle getArgs(){
282282
* @param neutralButton the text as string
283283
* @return this instance
284284
*/
285-
public This neut(String neutralButton){ return setArg(NEUTRAL_BUTTON_TEXT, neutralButton); }
285+
public This neut(CharSequence neutralButton){ return setArg(NEUTRAL_BUTTON_TEXT, neutralButton); }
286286

287287
/**
288288
* Sets this dialogs neutral button text
@@ -514,30 +514,30 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
514514
context = dialog.getContext();
515515

516516
dialog.setTitle(getTitle());
517-
String msg = getMessage();
517+
CharSequence msg = getMessage();
518518
if (msg != null) {
519-
if (getArgs().getBoolean(HTML)) {
519+
if (getArgs().getBoolean(HTML) && msg instanceof String) {
520520
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
521-
dialog.setMessage(Html.fromHtml(msg, 0));
521+
dialog.setMessage(Html.fromHtml((String) msg, 0));
522522
} else {
523523
//noinspection deprecation
524-
dialog.setMessage(Html.fromHtml(msg));
524+
dialog.setMessage(Html.fromHtml((String) msg));
525525
}
526526
} else {
527527
dialog.setMessage(msg);
528528
}
529529
}
530-
String positiveButtonText = getArgString(POSITIVE_BUTTON_TEXT);
530+
CharSequence positiveButtonText = getArgString(POSITIVE_BUTTON_TEXT);
531531
if (positiveButtonText != null) {
532532
dialog.setButton(DialogInterface.BUTTON_POSITIVE,
533533
positiveButtonText, forwardOnClickListener);
534534
}
535-
String negativeButtonText = getArgString(NEGATIVE_BUTTON_TEXT);
535+
CharSequence negativeButtonText = getArgString(NEGATIVE_BUTTON_TEXT);
536536
if (negativeButtonText != null) {
537537
dialog.setButton(DialogInterface.BUTTON_NEGATIVE,
538538
negativeButtonText, forwardOnClickListener);
539539
}
540-
String neutralButtonText = getArgString(NEUTRAL_BUTTON_TEXT);
540+
CharSequence neutralButtonText = getArgString(NEUTRAL_BUTTON_TEXT);
541541
if (neutralButtonText != null) {
542542
dialog.setButton(DialogInterface.BUTTON_NEUTRAL,
543543
neutralButtonText, forwardOnClickListener);

simpledialogfragments/src/main/java/eltos/simpledialogfragment/color/SimpleColorDialog.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,34 +140,34 @@ public SimpleColorDialog setupColorWheelHideHex(boolean hideHex){
140140

141141
/**
142142
* Configures {@link SimpleColorWheelDialog} for custom color picking
143-
* See {@link SimpleColorWheelDialog#title(String)}
143+
* See {@link SimpleColorWheelDialog#title(CharSequence)}
144144
*
145145
* @param text the title for the color wheel dialog
146146
* @return this instance
147147
*/
148-
public SimpleColorDialog setupColorWheelTitle(String text){
148+
public SimpleColorDialog setupColorWheelTitle(CharSequence text){
149149
return setArg(CUSTOM_TITLE, text);
150150
}
151151

152152
/**
153153
* Configures {@link SimpleColorWheelDialog} for custom color picking
154-
* See {@link SimpleColorWheelDialog#pos(String)}
154+
* See {@link SimpleColorWheelDialog#pos(CharSequence)}
155155
*
156156
* @param text the positive button text for the color wheel dialog
157157
* @return this instance
158158
*/
159-
public SimpleColorDialog setupColorWheelPosButton(String text){
159+
public SimpleColorDialog setupColorWheelPosButton(CharSequence text){
160160
return setArg(CUSTOM_POS, text);
161161
}
162162

163163
/**
164164
* Configures {@link SimpleColorWheelDialog} for custom color picking
165-
* See {@link SimpleColorWheelDialog#neut(String)}
165+
* See {@link SimpleColorWheelDialog#neut(CharSequence)}
166166
*
167167
* @param text the neutral button text for the color wheel dialog
168168
* @return this instance
169169
*/
170-
public SimpleColorDialog setupColorWheelNeutButton(String text){
170+
public SimpleColorDialog setupColorWheelNeutButton(CharSequence text){
171171
return setArg(CUSTOM_NEUT, text);
172172
}
173173

simpledialogfragments/src/main/java/eltos/simpledialogfragment/list/CustomListDialog.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public This gridColumnWidth(@DimenRes int columnWidthDimenResId){
259259
* @param title the string to be displayed
260260
* @return this instance
261261
*/
262-
public This emptyText(String title){ return setArg(EMPTY_TEXT, title); }
262+
public This emptyText(CharSequence title){ return setArg(EMPTY_TEXT, title); }
263263

264264
/**
265265
* Sets a string to be displayed if no items are currently visible
@@ -271,7 +271,7 @@ public This gridColumnWidth(@DimenRes int columnWidthDimenResId){
271271

272272

273273
@Override
274-
public This pos(String positiveButton) {
274+
public This pos(CharSequence positiveButton) {
275275
pmFlag = true;
276276
return super.pos(positiveButton);
277277
}

testApp/src/main/java/eltos/simpledialogfragments/ClipboardCopyDialog.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ public static ClipboardCopyDialog build(){
4141
protected void onNeutralButtonClick() {
4242
ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
4343
ClipData clip;
44-
if (getArgs().getBoolean(HTML)){
44+
if (getArgs().getBoolean(HTML) && getMessage() instanceof String){
4545
// HTML dialog message
46-
String plainText = Html.fromHtml(getMessage()).toString();
46+
String plainText = Html.fromHtml((String) getMessage()).toString();
4747
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
48-
clip = ClipData.newHtmlText(getTitle(), plainText, getMessage());
48+
clip = ClipData.newHtmlText(getTitle(), plainText, (String) getMessage());
4949
} else {
5050
clip = ClipData.newPlainText(getTitle(), plainText);
5151
}

testApp/src/main/java/eltos/simpledialogfragments/MainActivity.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import android.text.Spannable;
3333
import android.text.SpannableString;
3434
import android.text.style.ForegroundColorSpan;
35+
import android.text.style.StyleSpan;
3536
import android.util.Log;
3637
import android.util.Pair;
3738
import android.util.Patterns;
@@ -162,9 +163,14 @@ public void lintTest(){
162163

163164
public void showInfo(View view){
164165

166+
// example on how to use spannable strings
167+
Spannable sb = new SpannableString("Information: Hello world!");
168+
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, 11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
169+
165170
SimpleDialog.build()
166171
.title(R.string.message)
167-
.msg(R.string.hello_world)
172+
//.msg(R.string.hello_world)
173+
.msg(sb)
168174
.show(this);
169175

170176
}

0 commit comments

Comments
 (0)