1
0

MaterialDialog.java 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708
  1. package com.afollestad.materialdialogs;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.content.res.ColorStateList;
  6. import android.graphics.Paint;
  7. import android.graphics.Typeface;
  8. import android.graphics.drawable.Drawable;
  9. import android.os.Build;
  10. import android.os.Handler;
  11. import android.support.annotation.ArrayRes;
  12. import android.support.annotation.AttrRes;
  13. import android.support.annotation.ColorInt;
  14. import android.support.annotation.ColorRes;
  15. import android.support.annotation.DimenRes;
  16. import android.support.annotation.DrawableRes;
  17. import android.support.annotation.LayoutRes;
  18. import android.support.annotation.NonNull;
  19. import android.support.annotation.Nullable;
  20. import android.support.annotation.StringRes;
  21. import android.support.annotation.UiThread;
  22. import android.support.v4.content.res.ResourcesCompat;
  23. import android.text.Editable;
  24. import android.text.TextUtils;
  25. import android.text.TextWatcher;
  26. import android.view.LayoutInflater;
  27. import android.view.View;
  28. import android.view.ViewGroup;
  29. import android.view.ViewTreeObserver;
  30. import android.view.WindowManager;
  31. import android.widget.AdapterView;
  32. import android.widget.CheckBox;
  33. import android.widget.EditText;
  34. import android.widget.FrameLayout;
  35. import android.widget.ImageView;
  36. import android.widget.ListAdapter;
  37. import android.widget.ListView;
  38. import android.widget.ProgressBar;
  39. import android.widget.RadioButton;
  40. import android.widget.TextView;
  41. import com.afollestad.materialdialogs.internal.MDButton;
  42. import com.afollestad.materialdialogs.internal.MDRootLayout;
  43. import com.afollestad.materialdialogs.internal.MDTintHelper;
  44. import com.afollestad.materialdialogs.util.DialogUtils;
  45. import com.afollestad.materialdialogs.util.TypefaceHelper;
  46. import java.text.NumberFormat;
  47. import java.util.ArrayList;
  48. import java.util.Arrays;
  49. import java.util.Collections;
  50. import java.util.List;
  51. /**
  52. * @author Aidan Follestad (afollestad)
  53. */
  54. public class MaterialDialog extends DialogBase implements
  55. View.OnClickListener, AdapterView.OnItemClickListener {
  56. protected final Builder mBuilder;
  57. protected ListView listView;
  58. protected ImageView icon;
  59. protected TextView title;
  60. protected View titleFrame;
  61. protected FrameLayout customViewFrame;
  62. protected ProgressBar mProgress;
  63. protected TextView mProgressLabel;
  64. protected TextView mProgressMinMax;
  65. protected TextView content;
  66. protected EditText input;
  67. protected TextView inputMinMax;
  68. protected MDButton positiveButton;
  69. protected MDButton neutralButton;
  70. protected MDButton negativeButton;
  71. protected ListType listType;
  72. protected List<Integer> selectedIndicesList;
  73. public final Builder getBuilder() {
  74. return mBuilder;
  75. }
  76. @SuppressLint("InflateParams")
  77. protected MaterialDialog(Builder builder) {
  78. super(builder.context, DialogInit.getTheme(builder));
  79. mHandler = new Handler();
  80. mBuilder = builder;
  81. final LayoutInflater inflater = LayoutInflater.from(builder.context);
  82. view = (MDRootLayout) inflater.inflate(DialogInit.getInflateLayout(builder), null);
  83. DialogInit.init(this);
  84. // Set up width if on tablet
  85. if (builder.context.getResources().getBoolean(R.bool.md_is_tablet)) {
  86. WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
  87. lp.copyFrom(getWindow().getAttributes());
  88. lp.width = builder.context.getResources().getDimensionPixelSize(R.dimen.md_default_dialog_width);
  89. getWindow().setAttributes(lp);
  90. }
  91. }
  92. public final void setTypeface(TextView target, Typeface t) {
  93. if (t == null) return;
  94. int flags = target.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG;
  95. target.setPaintFlags(flags);
  96. target.setTypeface(t);
  97. }
  98. protected final void checkIfListInitScroll() {
  99. if (listView == null)
  100. return;
  101. listView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  102. @Override
  103. public void onGlobalLayout() {
  104. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
  105. //noinspection deprecation
  106. listView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
  107. } else {
  108. listView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
  109. }
  110. if (listType == ListType.SINGLE || listType == ListType.MULTI) {
  111. int selectedIndex;
  112. if (listType == ListType.SINGLE) {
  113. if (mBuilder.selectedIndex < 0)
  114. return;
  115. selectedIndex = mBuilder.selectedIndex;
  116. } else {
  117. if (mBuilder.selectedIndices == null || mBuilder.selectedIndices.length == 0)
  118. return;
  119. List<Integer> indicesList = Arrays.asList(mBuilder.selectedIndices);
  120. Collections.sort(indicesList);
  121. selectedIndex = indicesList.get(0);
  122. }
  123. if (listView.getLastVisiblePosition() < selectedIndex) {
  124. final int totalVisible = listView.getLastVisiblePosition() - listView.getFirstVisiblePosition();
  125. // Scroll so that the selected index appears in the middle (vertically) of the ListView
  126. int scrollIndex = selectedIndex - (totalVisible / 2);
  127. if (scrollIndex < 0) scrollIndex = 0;
  128. final int fScrollIndex = scrollIndex;
  129. listView.post(new Runnable() {
  130. @Override
  131. public void run() {
  132. listView.requestFocus();
  133. listView.setSelection(fScrollIndex);
  134. }
  135. });
  136. }
  137. }
  138. }
  139. });
  140. }
  141. /**
  142. * Sets the dialog ListView's adapter and it's item click listener.
  143. */
  144. protected final void invalidateList() {
  145. if (listView == null)
  146. return;
  147. else if ((mBuilder.items == null || mBuilder.items.length == 0) && mBuilder.adapter == null)
  148. return;
  149. // Set up list with adapter
  150. listView.setAdapter(mBuilder.adapter);
  151. if (listType != null || mBuilder.listCallbackCustom != null)
  152. listView.setOnItemClickListener(this);
  153. }
  154. @Override
  155. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  156. if (mBuilder.listCallbackCustom != null) {
  157. // Custom adapter
  158. CharSequence text = null;
  159. if (view instanceof TextView)
  160. text = ((TextView) view).getText();
  161. mBuilder.listCallbackCustom.onSelection(this, view, position, text);
  162. } else if (listType == null || listType == ListType.REGULAR) {
  163. // Default adapter, non choice mode
  164. if (mBuilder.autoDismiss) {
  165. // If auto dismiss is enabled, dismiss the dialog when a list item is selected
  166. dismiss();
  167. }
  168. mBuilder.listCallback.onSelection(this, view, position, mBuilder.items[position]);
  169. } else {
  170. // Default adapter, choice mode
  171. if (listType == ListType.MULTI) {
  172. final boolean shouldBeChecked = !selectedIndicesList.contains(Integer.valueOf(position));
  173. final CheckBox cb = (CheckBox) view.findViewById(R.id.control);
  174. if (shouldBeChecked) {
  175. // Add the selection to the states first so the callback includes it (when alwaysCallMultiChoiceCallback)
  176. selectedIndicesList.add(position);
  177. if (mBuilder.alwaysCallMultiChoiceCallback) {
  178. // If the checkbox wasn't previously selected, and the callback returns true, add it to the states and check it
  179. if (sendMultichoiceCallback()) {
  180. cb.setChecked(true);
  181. } else {
  182. // The callback cancelled selection, remove it from the states
  183. selectedIndicesList.remove(Integer.valueOf(position));
  184. }
  185. } else {
  186. // The callback was not used to check if selection is allowed, just select it
  187. cb.setChecked(true);
  188. }
  189. } else {
  190. // The checkbox was unchecked
  191. selectedIndicesList.remove(Integer.valueOf(position));
  192. cb.setChecked(false);
  193. if (mBuilder.alwaysCallMultiChoiceCallback)
  194. sendMultichoiceCallback();
  195. }
  196. } else if (listType == ListType.SINGLE) {
  197. boolean allowSelection = true;
  198. final MaterialDialogAdapter adapter = (MaterialDialogAdapter) mBuilder.adapter;
  199. final RadioButton radio = (RadioButton) view.findViewById(R.id.control);
  200. if (mBuilder.autoDismiss && mBuilder.positiveText == null) {
  201. // If auto dismiss is enabled, and no action button is visible to approve the selection, dismiss the dialog
  202. dismiss();
  203. // Don't allow the selection to be updated since the dialog is being dismissed anyways
  204. allowSelection = false;
  205. // Update selected index and send callback
  206. mBuilder.selectedIndex = position;
  207. sendSingleChoiceCallback(view);
  208. } else if (mBuilder.alwaysCallSingleChoiceCallback) {
  209. int oldSelected = mBuilder.selectedIndex;
  210. // Temporarily set the new index so the callback uses the right one
  211. mBuilder.selectedIndex = position;
  212. // Only allow the radio button to be checked if the callback returns true
  213. allowSelection = sendSingleChoiceCallback(view);
  214. // Restore the old selected index, so the state is updated below
  215. mBuilder.selectedIndex = oldSelected;
  216. }
  217. // Update the checked states
  218. if (allowSelection && mBuilder.selectedIndex != position) {
  219. mBuilder.selectedIndex = position;
  220. // Uncheck the previously selected radio button
  221. if (adapter.mRadioButton == null) {
  222. adapter.mInitRadio = true;
  223. adapter.notifyDataSetChanged();
  224. }
  225. if (adapter.mRadioButton != null)
  226. adapter.mRadioButton.setChecked(false);
  227. // Check the newly selected radio button
  228. radio.setChecked(true);
  229. adapter.mRadioButton = radio;
  230. }
  231. }
  232. }
  233. }
  234. public static class NotImplementedException extends Error {
  235. public NotImplementedException(@SuppressWarnings("SameParameterValue") String message) {
  236. super(message);
  237. }
  238. }
  239. public static class DialogException extends WindowManager.BadTokenException {
  240. public DialogException(@SuppressWarnings("SameParameterValue") String message) {
  241. super(message);
  242. }
  243. }
  244. protected final Drawable getListSelector() {
  245. if (mBuilder.listSelector != 0)
  246. return ResourcesCompat.getDrawable(mBuilder.context.getResources(), mBuilder.listSelector, null);
  247. final Drawable d = DialogUtils.resolveDrawable(mBuilder.context, R.attr.md_list_selector);
  248. if (d != null) return d;
  249. return DialogUtils.resolveDrawable(getContext(), R.attr.md_list_selector);
  250. }
  251. /* package */ Drawable getButtonSelector(DialogAction which, boolean isStacked) {
  252. if (isStacked) {
  253. if (mBuilder.btnSelectorStacked != 0)
  254. return ResourcesCompat.getDrawable(mBuilder.context.getResources(), mBuilder.btnSelectorStacked, null);
  255. final Drawable d = DialogUtils.resolveDrawable(mBuilder.context, R.attr.md_btn_stacked_selector);
  256. if (d != null) return d;
  257. return DialogUtils.resolveDrawable(getContext(), R.attr.md_btn_stacked_selector);
  258. } else {
  259. switch (which) {
  260. default: {
  261. if (mBuilder.btnSelectorPositive != 0)
  262. return ResourcesCompat.getDrawable(mBuilder.context.getResources(), mBuilder.btnSelectorPositive, null);
  263. final Drawable d = DialogUtils.resolveDrawable(mBuilder.context, R.attr.md_btn_positive_selector);
  264. if (d != null) return d;
  265. return DialogUtils.resolveDrawable(getContext(), R.attr.md_btn_positive_selector);
  266. }
  267. case NEUTRAL: {
  268. if (mBuilder.btnSelectorNeutral != 0)
  269. return ResourcesCompat.getDrawable(mBuilder.context.getResources(), mBuilder.btnSelectorNeutral, null);
  270. final Drawable d = DialogUtils.resolveDrawable(mBuilder.context, R.attr.md_btn_neutral_selector);
  271. if (d != null) return d;
  272. return DialogUtils.resolveDrawable(getContext(), R.attr.md_btn_neutral_selector);
  273. }
  274. case NEGATIVE: {
  275. if (mBuilder.btnSelectorNegative != 0)
  276. return ResourcesCompat.getDrawable(mBuilder.context.getResources(), mBuilder.btnSelectorNegative, null);
  277. final Drawable d = DialogUtils.resolveDrawable(mBuilder.context, R.attr.md_btn_negative_selector);
  278. if (d != null) return d;
  279. return DialogUtils.resolveDrawable(getContext(), R.attr.md_btn_negative_selector);
  280. }
  281. }
  282. }
  283. }
  284. private boolean sendSingleChoiceCallback(View v) {
  285. CharSequence text = null;
  286. if (mBuilder.selectedIndex >= 0) {
  287. text = mBuilder.items[mBuilder.selectedIndex];
  288. }
  289. return mBuilder.listCallbackSingleChoice.onSelection(this, v, mBuilder.selectedIndex, text);
  290. }
  291. private boolean sendMultichoiceCallback() {
  292. Collections.sort(selectedIndicesList); // make sure the indicies are in order
  293. List<CharSequence> selectedTitles = new ArrayList<>();
  294. for (Integer i : selectedIndicesList) {
  295. selectedTitles.add(mBuilder.items[i]);
  296. }
  297. return mBuilder.listCallbackMultiChoice.onSelection(this,
  298. selectedIndicesList.toArray(new Integer[selectedIndicesList.size()]),
  299. selectedTitles.toArray(new CharSequence[selectedTitles.size()]));
  300. }
  301. @Override
  302. public final void onClick(View v) {
  303. DialogAction tag = (DialogAction) v.getTag();
  304. switch (tag) {
  305. case POSITIVE: {
  306. if (mBuilder.callback != null) {
  307. mBuilder.callback.onAny(this);
  308. mBuilder.callback.onPositive(this);
  309. }
  310. if (mBuilder.listCallbackSingleChoice != null)
  311. sendSingleChoiceCallback(v);
  312. if (mBuilder.listCallbackMultiChoice != null)
  313. sendMultichoiceCallback();
  314. if (mBuilder.inputCallback != null && input != null && !mBuilder.alwaysCallInputCallback)
  315. mBuilder.inputCallback.onInput(this, input.getText());
  316. if (mBuilder.autoDismiss) dismiss();
  317. break;
  318. }
  319. case NEGATIVE: {
  320. if (mBuilder.callback != null) {
  321. mBuilder.callback.onAny(this);
  322. mBuilder.callback.onNegative(this);
  323. }
  324. if (mBuilder.autoDismiss) dismiss();
  325. break;
  326. }
  327. case NEUTRAL: {
  328. if (mBuilder.callback != null) {
  329. mBuilder.callback.onAny(this);
  330. mBuilder.callback.onNeutral(this);
  331. }
  332. if (mBuilder.autoDismiss) dismiss();
  333. break;
  334. }
  335. }
  336. }
  337. /**
  338. * The class used to construct a MaterialDialog.
  339. */
  340. public static class Builder {
  341. protected final Context context;
  342. protected CharSequence title;
  343. protected GravityEnum titleGravity = GravityEnum.START;
  344. protected GravityEnum contentGravity = GravityEnum.START;
  345. protected GravityEnum btnStackedGravity = GravityEnum.END;
  346. protected GravityEnum itemsGravity = GravityEnum.START;
  347. protected GravityEnum buttonsGravity = GravityEnum.START;
  348. protected int titleColor = -1;
  349. protected int contentColor = -1;
  350. protected CharSequence content;
  351. protected CharSequence[] items;
  352. protected CharSequence positiveText;
  353. protected CharSequence neutralText;
  354. protected CharSequence negativeText;
  355. protected View customView;
  356. protected int widgetColor;
  357. protected ColorStateList positiveColor;
  358. protected ColorStateList negativeColor;
  359. protected ColorStateList neutralColor;
  360. protected ButtonCallback callback;
  361. protected ListCallback listCallback;
  362. protected ListCallbackSingleChoice listCallbackSingleChoice;
  363. protected ListCallbackMultiChoice listCallbackMultiChoice;
  364. protected ListCallback listCallbackCustom;
  365. protected boolean alwaysCallMultiChoiceCallback = false;
  366. protected boolean alwaysCallSingleChoiceCallback = false;
  367. protected Theme theme = Theme.LIGHT;
  368. protected boolean cancelable = true;
  369. protected float contentLineSpacingMultiplier = 1.2f;
  370. protected int selectedIndex = -1;
  371. protected Integer[] selectedIndices = null;
  372. protected boolean autoDismiss = true;
  373. protected Typeface regularFont;
  374. protected Typeface mediumFont;
  375. protected Drawable icon;
  376. protected boolean limitIconToDefaultSize;
  377. protected int maxIconSize = -1;
  378. protected ListAdapter adapter;
  379. protected OnDismissListener dismissListener;
  380. protected OnCancelListener cancelListener;
  381. protected OnKeyListener keyListener;
  382. protected OnShowListener showListener;
  383. protected boolean forceStacking;
  384. protected boolean wrapCustomViewInScroll;
  385. protected int dividerColor;
  386. protected int backgroundColor;
  387. protected int itemColor;
  388. protected boolean indeterminateProgress;
  389. protected boolean showMinMax;
  390. protected int progress = -2;
  391. protected int progressMax = 0;
  392. protected CharSequence inputPrefill;
  393. protected CharSequence inputHint;
  394. protected InputCallback inputCallback;
  395. protected boolean inputAllowEmpty;
  396. protected int inputType = -1;
  397. protected boolean alwaysCallInputCallback;
  398. protected int inputMaxLength = -1;
  399. protected int inputMaxLengthErrorColor = 0;
  400. protected String progressNumberFormat;
  401. protected NumberFormat progressPercentFormat;
  402. protected boolean indeterminateIsHorizontalProgress;
  403. protected boolean titleColorSet = false;
  404. protected boolean contentColorSet = false;
  405. protected boolean itemColorSet = false;
  406. protected boolean positiveColorSet = false;
  407. protected boolean neutralColorSet = false;
  408. protected boolean negativeColorSet = false;
  409. protected boolean widgetColorSet = false;
  410. protected boolean dividerColorSet = false;
  411. @DrawableRes
  412. protected int listSelector;
  413. @DrawableRes
  414. protected int btnSelectorStacked;
  415. @DrawableRes
  416. protected int btnSelectorPositive;
  417. @DrawableRes
  418. protected int btnSelectorNeutral;
  419. @DrawableRes
  420. protected int btnSelectorNegative;
  421. public final Context getContext() {
  422. return context;
  423. }
  424. public final GravityEnum getItemsGravity() {
  425. return itemsGravity;
  426. }
  427. public final int getItemColor() {
  428. return itemColor;
  429. }
  430. public final Typeface getRegularFont() {
  431. return regularFont;
  432. }
  433. public Builder(@NonNull Context context) {
  434. this.context = context;
  435. final int materialBlue = context.getResources().getColor(R.color.md_material_blue_600);
  436. // Retrieve default accent colors, which are used on the action buttons and progress bars
  437. this.widgetColor = DialogUtils.resolveColor(context, R.attr.colorAccent, materialBlue);
  438. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  439. this.widgetColor = DialogUtils.resolveColor(context, android.R.attr.colorAccent, this.widgetColor);
  440. }
  441. this.positiveColor = DialogUtils.getActionTextStateList(context, this.widgetColor);
  442. this.negativeColor = DialogUtils.getActionTextStateList(context, this.widgetColor);
  443. this.neutralColor = DialogUtils.getActionTextStateList(context, this.widgetColor);
  444. this.progressPercentFormat = NumberFormat.getPercentInstance();
  445. this.progressNumberFormat = "%1d/%2d";
  446. // Set the default theme based on the Activity theme's primary color darkness (more white or more black)
  447. final int primaryTextColor = DialogUtils.resolveColor(context, android.R.attr.textColorPrimary);
  448. this.theme = DialogUtils.isColorDark(primaryTextColor) ? Theme.LIGHT : Theme.DARK;
  449. // Load theme values from the ThemeSingleton if needed
  450. checkSingleton();
  451. // Retrieve gravity settings from global theme attributes if needed
  452. this.titleGravity = DialogUtils.resolveGravityEnum(context, R.attr.md_title_gravity, this.titleGravity);
  453. this.contentGravity = DialogUtils.resolveGravityEnum(context, R.attr.md_content_gravity, this.contentGravity);
  454. this.btnStackedGravity = DialogUtils.resolveGravityEnum(context, R.attr.md_btnstacked_gravity, this.btnStackedGravity);
  455. this.itemsGravity = DialogUtils.resolveGravityEnum(context, R.attr.md_items_gravity, this.itemsGravity);
  456. this.buttonsGravity = DialogUtils.resolveGravityEnum(context, R.attr.md_buttons_gravity, this.buttonsGravity);
  457. final String mediumFont = DialogUtils.resolveString(context, R.attr.md_medium_font);
  458. final String regularFont = DialogUtils.resolveString(context, R.attr.md_regular_font);
  459. typeface(mediumFont, regularFont);
  460. if (this.mediumFont == null) {
  461. try {
  462. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
  463. this.mediumFont = Typeface.create("sans-serif-medium", Typeface.NORMAL);
  464. else
  465. this.mediumFont = Typeface.create("sans-serif", Typeface.BOLD);
  466. } catch (Exception ignored) {
  467. }
  468. }
  469. if (this.regularFont == null) {
  470. try {
  471. this.regularFont = Typeface.create("sans-serif", Typeface.NORMAL);
  472. } catch (Exception ignored) {
  473. }
  474. }
  475. }
  476. private void checkSingleton() {
  477. if (ThemeSingleton.get(false) == null) return;
  478. ThemeSingleton s = ThemeSingleton.get();
  479. if (s.darkTheme)
  480. this.theme = Theme.DARK;
  481. if (s.titleColor != 0)
  482. this.titleColor = s.titleColor;
  483. if (s.contentColor != 0)
  484. this.contentColor = s.contentColor;
  485. if (s.positiveColor != null)
  486. this.positiveColor = s.positiveColor;
  487. if (s.neutralColor != null)
  488. this.neutralColor = s.neutralColor;
  489. if (s.negativeColor != null)
  490. this.negativeColor = s.negativeColor;
  491. if (s.itemColor != 0)
  492. this.itemColor = s.itemColor;
  493. if (s.icon != null)
  494. this.icon = s.icon;
  495. if (s.backgroundColor != 0)
  496. this.backgroundColor = s.backgroundColor;
  497. if (s.dividerColor != 0)
  498. this.dividerColor = s.dividerColor;
  499. if (s.btnSelectorStacked != 0)
  500. this.btnSelectorStacked = s.btnSelectorStacked;
  501. if (s.listSelector != 0)
  502. this.listSelector = s.listSelector;
  503. if (s.btnSelectorPositive != 0)
  504. this.btnSelectorPositive = s.btnSelectorPositive;
  505. if (s.btnSelectorNeutral != 0)
  506. this.btnSelectorNeutral = s.btnSelectorNeutral;
  507. if (s.btnSelectorNegative != 0)
  508. this.btnSelectorNegative = s.btnSelectorNegative;
  509. if (s.widgetColor != 0)
  510. this.widgetColor = s.widgetColor;
  511. this.titleGravity = s.titleGravity;
  512. this.contentGravity = s.contentGravity;
  513. this.btnStackedGravity = s.btnStackedGravity;
  514. this.itemsGravity = s.itemsGravity;
  515. this.buttonsGravity = s.buttonsGravity;
  516. }
  517. public Builder title(@StringRes int titleRes) {
  518. title(this.context.getText(titleRes));
  519. return this;
  520. }
  521. public Builder title(@NonNull CharSequence title) {
  522. this.title = title;
  523. return this;
  524. }
  525. public Builder titleGravity(@NonNull GravityEnum gravity) {
  526. this.titleGravity = gravity;
  527. return this;
  528. }
  529. public Builder titleColor(@ColorInt int color) {
  530. this.titleColor = color;
  531. this.titleColorSet = true;
  532. return this;
  533. }
  534. public Builder titleColorRes(@ColorRes int colorRes) {
  535. titleColor(this.context.getResources().getColor(colorRes));
  536. return this;
  537. }
  538. public Builder titleColorAttr(@AttrRes int colorAttr) {
  539. titleColor(DialogUtils.resolveColor(this.context, colorAttr));
  540. return this;
  541. }
  542. /**
  543. * Sets the fonts used in the dialog. It's recommended that you use {@link #typeface(String, String)} instead,
  544. * to avoid duplicate Typeface allocations and high memory usage.
  545. *
  546. * @param medium The font used on titles and action buttons. Null uses device default.
  547. * @param regular The font used everywhere else, like on the content and list items. Null uses device default.
  548. * @return The Builder instance so you can chain calls to it.
  549. */
  550. public Builder typeface(@Nullable Typeface medium, @Nullable Typeface regular) {
  551. this.mediumFont = medium;
  552. this.regularFont = regular;
  553. return this;
  554. }
  555. /**
  556. * Sets the fonts used in the dialog, by file names. This also uses TypefaceHelper in order
  557. * to avoid any un-needed allocations (it recycles typefaces for you).
  558. *
  559. * @param medium The name of font in assets/fonts used on titles and action buttons (null uses device default). E.g. [your-project]/app/main/assets/fonts/[medium]
  560. * @param regular The name of font in assets/fonts used everywhere else, like content and list items (null uses device default). E.g. [your-project]/app/main/assets/fonts/[regular]
  561. * @return The Builder instance so you can chain calls to it.
  562. */
  563. public Builder typeface(@Nullable String medium, @Nullable String regular) {
  564. if (medium != null) {
  565. this.mediumFont = TypefaceHelper.get(this.context, medium);
  566. if (this.mediumFont == null)
  567. throw new IllegalArgumentException("No font asset found for " + medium);
  568. }
  569. if (regular != null) {
  570. this.regularFont = TypefaceHelper.get(this.context, regular);
  571. if (this.regularFont == null)
  572. throw new IllegalArgumentException("No font asset found for " + regular);
  573. }
  574. return this;
  575. }
  576. public Builder icon(@NonNull Drawable icon) {
  577. this.icon = icon;
  578. return this;
  579. }
  580. public Builder iconRes(@DrawableRes int icon) {
  581. this.icon = ResourcesCompat.getDrawable(context.getResources(), icon, null);
  582. return this;
  583. }
  584. public Builder iconAttr(@AttrRes int iconAttr) {
  585. this.icon = DialogUtils.resolveDrawable(context, iconAttr);
  586. return this;
  587. }
  588. public Builder content(@StringRes int contentRes) {
  589. content(this.context.getText(contentRes));
  590. return this;
  591. }
  592. public Builder content(@NonNull CharSequence content) {
  593. if (this.customView != null)
  594. throw new IllegalStateException("You cannot set content() when you're using a custom view.");
  595. this.content = content;
  596. return this;
  597. }
  598. public Builder content(@StringRes int contentRes, Object... formatArgs) {
  599. content(this.context.getString(contentRes, formatArgs));
  600. return this;
  601. }
  602. public Builder contentColor(@ColorInt int color) {
  603. this.contentColor = color;
  604. this.contentColorSet = true;
  605. return this;
  606. }
  607. public Builder contentColorRes(@ColorRes int colorRes) {
  608. contentColor(this.context.getResources().getColor(colorRes));
  609. return this;
  610. }
  611. public Builder contentColorAttr(@AttrRes int colorAttr) {
  612. contentColor(DialogUtils.resolveColor(this.context, colorAttr));
  613. return this;
  614. }
  615. public Builder contentGravity(@NonNull GravityEnum gravity) {
  616. this.contentGravity = gravity;
  617. return this;
  618. }
  619. public Builder contentLineSpacing(float multiplier) {
  620. this.contentLineSpacingMultiplier = multiplier;
  621. return this;
  622. }
  623. public Builder items(@ArrayRes int itemsRes) {
  624. items(this.context.getResources().getTextArray(itemsRes));
  625. return this;
  626. }
  627. public Builder items(@NonNull CharSequence[] items) {
  628. if (this.customView != null)
  629. throw new IllegalStateException("You cannot set items() when you're using a custom view.");
  630. this.items = items;
  631. return this;
  632. }
  633. public Builder itemsCallback(@NonNull ListCallback callback) {
  634. this.listCallback = callback;
  635. this.listCallbackSingleChoice = null;
  636. this.listCallbackMultiChoice = null;
  637. return this;
  638. }
  639. public Builder itemColor(@ColorInt int color) {
  640. this.itemColor = color;
  641. this.itemColorSet = true;
  642. return this;
  643. }
  644. public Builder itemColorRes(@ColorRes int colorRes) {
  645. return itemColor(this.context.getResources().getColor(colorRes));
  646. }
  647. public Builder itemColorAttr(@AttrRes int colorAttr) {
  648. return itemColor(DialogUtils.resolveColor(this.context, colorAttr));
  649. }
  650. public Builder itemsGravity(@NonNull GravityEnum gravity) {
  651. this.itemsGravity = gravity;
  652. return this;
  653. }
  654. public Builder buttonsGravity(@NonNull GravityEnum gravity) {
  655. this.buttonsGravity = gravity;
  656. return this;
  657. }
  658. /**
  659. * Pass anything below 0 (such as -1) for the selected index to leave all options unselected initially.
  660. * Otherwise pass the index of an item that will be selected initially.
  661. *
  662. * @param selectedIndex The checkbox index that will be selected initially.
  663. * @param callback The callback that will be called when the presses the positive button.
  664. * @return The Builder instance so you can chain calls to it.
  665. */
  666. public Builder itemsCallbackSingleChoice(int selectedIndex, @NonNull ListCallbackSingleChoice callback) {
  667. this.selectedIndex = selectedIndex;
  668. this.listCallback = null;
  669. this.listCallbackSingleChoice = callback;
  670. this.listCallbackMultiChoice = null;
  671. return this;
  672. }
  673. /**
  674. * By default, the single choice callback is only called when the user clicks the positive button
  675. * or if there are no buttons. Call this to force it to always call on item clicks even if the
  676. * positive button exists.
  677. *
  678. * @return The Builder instance so you can chain calls to it.
  679. */
  680. public Builder alwaysCallSingleChoiceCallback() {
  681. this.alwaysCallSingleChoiceCallback = true;
  682. return this;
  683. }
  684. /**
  685. * Pass null for the selected indices to leave all options unselected initially. Otherwise pass
  686. * an array of indices that will be selected initially.
  687. *
  688. * @param selectedIndices The radio button indices that will be selected initially.
  689. * @param callback The callback that will be called when the presses the positive button.
  690. * @return The Builder instance so you can chain calls to it.
  691. */
  692. public Builder itemsCallbackMultiChoice(@Nullable Integer[] selectedIndices, @NonNull ListCallbackMultiChoice callback) {
  693. this.selectedIndices = selectedIndices;
  694. this.listCallback = null;
  695. this.listCallbackSingleChoice = null;
  696. this.listCallbackMultiChoice = callback;
  697. return this;
  698. }
  699. /**
  700. * By default, the multi choice callback is only called when the user clicks the positive button
  701. * or if there are no buttons. Call this to force it to always call on item clicks even if the
  702. * positive button exists.
  703. *
  704. * @return The Builder instance so you can chain calls to it.
  705. */
  706. public Builder alwaysCallMultiChoiceCallback() {
  707. this.alwaysCallMultiChoiceCallback = true;
  708. return this;
  709. }
  710. public Builder positiveText(@StringRes int postiveRes) {
  711. positiveText(this.context.getText(postiveRes));
  712. return this;
  713. }
  714. public Builder positiveText(@NonNull CharSequence message) {
  715. this.positiveText = message;
  716. return this;
  717. }
  718. public Builder positiveColor(@ColorInt int color) {
  719. return positiveColor(DialogUtils.getActionTextStateList(context, color));
  720. }
  721. public Builder positiveColorRes(@ColorRes int colorRes) {
  722. return positiveColor(DialogUtils.getActionTextColorStateList(this.context, colorRes));
  723. }
  724. public Builder positiveColorAttr(@AttrRes int colorAttr) {
  725. return positiveColor(DialogUtils.resolveActionTextColorStateList(this.context, colorAttr, null));
  726. }
  727. public Builder positiveColor(ColorStateList colorStateList) {
  728. this.positiveColor = colorStateList;
  729. this.positiveColorSet = true;
  730. return this;
  731. }
  732. public Builder neutralText(@StringRes int neutralRes) {
  733. return neutralText(this.context.getText(neutralRes));
  734. }
  735. public Builder neutralText(@NonNull CharSequence message) {
  736. this.neutralText = message;
  737. return this;
  738. }
  739. public Builder negativeColor(@ColorInt int color) {
  740. return negativeColor(DialogUtils.getActionTextStateList(context, color));
  741. }
  742. public Builder negativeColorRes(@ColorRes int colorRes) {
  743. return negativeColor(DialogUtils.getActionTextColorStateList(this.context, colorRes));
  744. }
  745. public Builder negativeColorAttr(@AttrRes int colorAttr) {
  746. return negativeColor(DialogUtils.resolveActionTextColorStateList(this.context, colorAttr, null));
  747. }
  748. public Builder negativeColor(ColorStateList colorStateList) {
  749. this.negativeColor = colorStateList;
  750. this.negativeColorSet = true;
  751. return this;
  752. }
  753. public Builder negativeText(@StringRes int negativeRes) {
  754. return negativeText(this.context.getText(negativeRes));
  755. }
  756. public Builder negativeText(@NonNull CharSequence message) {
  757. this.negativeText = message;
  758. return this;
  759. }
  760. public Builder neutralColor(@ColorInt int color) {
  761. return neutralColor(DialogUtils.getActionTextStateList(context, color));
  762. }
  763. public Builder neutralColorRes(@ColorRes int colorRes) {
  764. return neutralColor(DialogUtils.getActionTextColorStateList(this.context, colorRes));
  765. }
  766. public Builder neutralColorAttr(@AttrRes int colorAttr) {
  767. return neutralColor(DialogUtils.resolveActionTextColorStateList(this.context, colorAttr, null));
  768. }
  769. public Builder neutralColor(ColorStateList colorStateList) {
  770. this.neutralColor = colorStateList;
  771. this.neutralColorSet = true;
  772. return this;
  773. }
  774. public Builder listSelector(@DrawableRes int selectorRes) {
  775. this.listSelector = selectorRes;
  776. return this;
  777. }
  778. public Builder btnSelectorStacked(@DrawableRes int selectorRes) {
  779. this.btnSelectorStacked = selectorRes;
  780. return this;
  781. }
  782. public Builder btnSelector(@DrawableRes int selectorRes) {
  783. this.btnSelectorPositive = selectorRes;
  784. this.btnSelectorNeutral = selectorRes;
  785. this.btnSelectorNegative = selectorRes;
  786. return this;
  787. }
  788. public Builder btnSelector(@DrawableRes int selectorRes, @NonNull DialogAction which) {
  789. switch (which) {
  790. default:
  791. this.btnSelectorPositive = selectorRes;
  792. break;
  793. case NEUTRAL:
  794. this.btnSelectorNeutral = selectorRes;
  795. break;
  796. case NEGATIVE:
  797. this.btnSelectorNegative = selectorRes;
  798. break;
  799. }
  800. return this;
  801. }
  802. /**
  803. * Sets the gravity used for the text in stacked action buttons. By default, it's #{@link GravityEnum#END}.
  804. *
  805. * @param gravity The gravity to use.
  806. * @return The Builder instance so calls can be chained.
  807. */
  808. public Builder btnStackedGravity(@NonNull GravityEnum gravity) {
  809. this.btnStackedGravity = gravity;
  810. return this;
  811. }
  812. public Builder customView(@LayoutRes int layoutRes, boolean wrapInScrollView) {
  813. LayoutInflater li = LayoutInflater.from(this.context);
  814. return customView(li.inflate(layoutRes, null), wrapInScrollView);
  815. }
  816. public Builder customView(@NonNull View view, boolean wrapInScrollView) {
  817. if (this.content != null)
  818. throw new IllegalStateException("You cannot use customView() when you have content set.");
  819. else if (this.items != null)
  820. throw new IllegalStateException("You cannot use customView() when you have items set.");
  821. else if (this.inputCallback != null)
  822. throw new IllegalStateException("You cannot use customView() with an input dialog");
  823. else if (this.progress > -2 || this.indeterminateProgress)
  824. throw new IllegalStateException("You cannot use customView() with a progress dialog");
  825. if (view.getParent() != null && view.getParent() instanceof ViewGroup)
  826. ((ViewGroup) view.getParent()).removeView(view);
  827. this.customView = view;
  828. this.wrapCustomViewInScroll = wrapInScrollView;
  829. return this;
  830. }
  831. /**
  832. * Makes this dialog a progress dialog.
  833. *
  834. * @param indeterminate If true, an infinite circular spinner is shown. If false, a horizontal progress bar is shown that is incremented or set via the built MaterialDialog instance.
  835. * @param max When indeterminate is false, the max value the horizontal progress bar can get to.
  836. * @return An instance of the Builder so calls can be chained.
  837. */
  838. public Builder progress(boolean indeterminate, int max) {
  839. if (this.customView != null)
  840. throw new IllegalStateException("You cannot set progress() when you're using a custom view.");
  841. if (indeterminate) {
  842. this.indeterminateProgress = true;
  843. this.progress = -2;
  844. } else {
  845. this.indeterminateProgress = false;
  846. this.progress = -1;
  847. this.progressMax = max;
  848. }
  849. return this;
  850. }
  851. /**
  852. * Makes this dialog a progress dialog.
  853. *
  854. * @param indeterminate If true, an infinite circular spinner is shown. If false, a horizontal progress bar is shown that is incremented or set via the built MaterialDialog instance.
  855. * @param max When indeterminate is false, the max value the horizontal progress bar can get to.
  856. * @param showMinMax For determinate dialogs, the min and max will be displayed to the left (start) of the progress bar, e.g. 50/100.
  857. * @return An instance of the Builder so calls can be chained.
  858. */
  859. public Builder progress(boolean indeterminate, int max, boolean showMinMax) {
  860. this.showMinMax = showMinMax;
  861. return progress(indeterminate, max);
  862. }
  863. /**
  864. * hange the format of the small text showing current and maximum units of progress.
  865. * The default is "%1d/%2d".
  866. */
  867. public Builder progressNumberFormat(@NonNull String format) {
  868. this.progressNumberFormat = format;
  869. return this;
  870. }
  871. /**
  872. * Change the format of the small text showing the percentage of progress.
  873. * The default is NumberFormat.getPercentageInstance().
  874. */
  875. public Builder progressPercentFormat(@NonNull NumberFormat format) {
  876. this.progressPercentFormat = format;
  877. return this;
  878. }
  879. /**
  880. * By default, indeterminate progress dialogs will use a circular indicator. You
  881. * can change it to use a horizontal progress indicator.
  882. */
  883. public Builder progressIndeterminateStyle(boolean horizontal) {
  884. this.indeterminateIsHorizontalProgress = horizontal;
  885. return this;
  886. }
  887. public Builder widgetColor(@ColorInt int color) {
  888. this.widgetColor = color;
  889. this.widgetColorSet = true;
  890. return this;
  891. }
  892. public Builder widgetColorRes(@ColorRes int colorRes) {
  893. return widgetColor(this.context.getResources().getColor(colorRes));
  894. }
  895. public Builder widgetColorAttr(@AttrRes int colorAttr) {
  896. return widgetColorRes(DialogUtils.resolveColor(this.context, colorAttr));
  897. }
  898. public Builder dividerColor(@ColorInt int color) {
  899. this.dividerColor = color;
  900. this.dividerColorSet = true;
  901. return this;
  902. }
  903. public Builder dividerColorRes(@ColorRes int colorRes) {
  904. return dividerColor(this.context.getResources().getColor(colorRes));
  905. }
  906. public Builder dividerColorAttr(@AttrRes int colorAttr) {
  907. return dividerColor(DialogUtils.resolveColor(this.context, colorAttr));
  908. }
  909. public Builder backgroundColor(@ColorInt int color) {
  910. this.backgroundColor = color;
  911. return this;
  912. }
  913. public Builder backgroundColorRes(@ColorRes int colorRes) {
  914. return backgroundColor(this.context.getResources().getColor(colorRes));
  915. }
  916. public Builder backgroundColorAttr(@AttrRes int colorAttr) {
  917. return backgroundColor(DialogUtils.resolveColor(this.context, colorAttr));
  918. }
  919. public Builder callback(@NonNull ButtonCallback callback) {
  920. this.callback = callback;
  921. return this;
  922. }
  923. public Builder theme(@NonNull Theme theme) {
  924. this.theme = theme;
  925. return this;
  926. }
  927. public Builder cancelable(boolean cancelable) {
  928. this.cancelable = cancelable;
  929. return this;
  930. }
  931. /**
  932. * This defaults to true. If set to false, the dialog will not automatically be dismissed
  933. * when an action button is pressed, and not automatically dismissed when the user selects
  934. * a list item.
  935. *
  936. * @param dismiss Whether or not to dismiss the dialog automatically.
  937. * @return The Builder instance so you can chain calls to it.
  938. */
  939. public Builder autoDismiss(boolean dismiss) {
  940. this.autoDismiss = dismiss;
  941. return this;
  942. }
  943. /**
  944. * Sets a custom {@link android.widget.ListAdapter} for the dialog's list
  945. *
  946. * @param adapter The adapter to set to the list.
  947. * @param callback The callback invoked when an item in the list is selected.
  948. * @return This Builder object to allow for chaining of calls to set methods
  949. */
  950. public Builder adapter(@NonNull ListAdapter adapter, @Nullable ListCallback callback) {
  951. if (this.customView != null)
  952. throw new IllegalStateException("You cannot set adapter() when you're using a custom view.");
  953. this.adapter = adapter;
  954. this.listCallbackCustom = callback;
  955. return this;
  956. }
  957. /**
  958. * Limits the display size of a set icon to 48dp.
  959. */
  960. public Builder limitIconToDefaultSize() {
  961. this.limitIconToDefaultSize = true;
  962. return this;
  963. }
  964. public Builder maxIconSize(int maxIconSize) {
  965. this.maxIconSize = maxIconSize;
  966. return this;
  967. }
  968. public Builder maxIconSizeRes(@DimenRes int maxIconSizeRes) {
  969. return maxIconSize((int) this.context.getResources().getDimension(maxIconSizeRes));
  970. }
  971. public Builder showListener(@NonNull OnShowListener listener) {
  972. this.showListener = listener;
  973. return this;
  974. }
  975. public Builder dismissListener(@NonNull OnDismissListener listener) {
  976. this.dismissListener = listener;
  977. return this;
  978. }
  979. public Builder cancelListener(@NonNull OnCancelListener listener) {
  980. this.cancelListener = listener;
  981. return this;
  982. }
  983. public Builder keyListener(@NonNull OnKeyListener listener) {
  984. this.keyListener = listener;
  985. return this;
  986. }
  987. public Builder forceStacking(boolean stacked) {
  988. this.forceStacking = stacked;
  989. return this;
  990. }
  991. public Builder input(@Nullable CharSequence hint, @Nullable CharSequence prefill, boolean allowEmptyInput, @NonNull InputCallback callback) {
  992. if (this.customView != null)
  993. throw new IllegalStateException("You cannot set content() when you're using a custom view.");
  994. this.inputCallback = callback;
  995. this.inputHint = hint;
  996. this.inputPrefill = prefill;
  997. this.inputAllowEmpty = allowEmptyInput;
  998. return this;
  999. }
  1000. public Builder input(@Nullable CharSequence hint, @Nullable CharSequence prefill, @NonNull InputCallback callback) {
  1001. return input(hint, prefill, true, callback);
  1002. }
  1003. public Builder input(@StringRes int hint, @StringRes int prefill, boolean allowEmptyInput, @NonNull InputCallback callback) {
  1004. return input(hint == 0 ? null : context.getText(hint), prefill == 0 ? null : context.getText(prefill), allowEmptyInput, callback);
  1005. }
  1006. public Builder input(@StringRes int hint, @StringRes int prefill, @NonNull InputCallback callback) {
  1007. return input(hint, prefill, true, callback);
  1008. }
  1009. public Builder inputType(int type) {
  1010. this.inputType = type;
  1011. return this;
  1012. }
  1013. public Builder inputMaxLength(int maxLength) {
  1014. return inputMaxLength(maxLength, 0);
  1015. }
  1016. /**
  1017. * @param errorColor Pass in 0 for the default red error color (as specified in guidelines).
  1018. */
  1019. public Builder inputMaxLength(int maxLength, int errorColor) {
  1020. if (maxLength < 1)
  1021. throw new IllegalArgumentException("Max length for input dialogs cannot be less than 1.");
  1022. this.inputMaxLength = maxLength;
  1023. if (errorColor == 0) {
  1024. inputMaxLengthErrorColor = context.getResources().getColor(R.color.md_edittext_error);
  1025. } else {
  1026. this.inputMaxLengthErrorColor = errorColor;
  1027. }
  1028. return this;
  1029. }
  1030. /**
  1031. * Same as #{@link #inputMaxLength(int, int)}, but it takes a color resource ID for the error color.
  1032. */
  1033. public Builder inputMaxLengthRes(int maxLength, @ColorRes int errorColor) {
  1034. return inputMaxLength(maxLength, context.getResources().getColor(errorColor));
  1035. }
  1036. public Builder alwaysCallInputCallback() {
  1037. this.alwaysCallInputCallback = true;
  1038. return this;
  1039. }
  1040. @UiThread
  1041. public MaterialDialog build() {
  1042. return new MaterialDialog(this);
  1043. }
  1044. @UiThread
  1045. public MaterialDialog show() {
  1046. MaterialDialog dialog = build();
  1047. dialog.show();
  1048. return dialog;
  1049. }
  1050. }
  1051. @Override
  1052. @UiThread
  1053. public void show() {
  1054. try {
  1055. super.show();
  1056. } catch (WindowManager.BadTokenException e) {
  1057. throw new DialogException("Bad window token, you cannot show a dialog before an Activity is created or after it's hidden.");
  1058. }
  1059. }
  1060. /**
  1061. * Retrieves the view of an action button, allowing you to modify properties such as whether or not it's enabled.
  1062. * Use {@link #setActionButton(DialogAction, int)} to change text, since the view returned here is not
  1063. * the view that displays text.
  1064. *
  1065. * @param which The action button of which to get the view for.
  1066. * @return The view from the dialog's layout representing this action button.
  1067. */
  1068. public final View getActionButton(@NonNull DialogAction which) {
  1069. switch (which) {
  1070. default:
  1071. return view.findViewById(R.id.buttonDefaultPositive);
  1072. case NEUTRAL:
  1073. return view.findViewById(R.id.buttonDefaultNeutral);
  1074. case NEGATIVE:
  1075. return view.findViewById(R.id.buttonDefaultNegative);
  1076. }
  1077. }
  1078. /**
  1079. * Retrieves the view representing the dialog as a whole. Be careful with this.
  1080. */
  1081. public final View getView() {
  1082. return view;
  1083. }
  1084. @Nullable
  1085. public final ListView getListView() {
  1086. return listView;
  1087. }
  1088. @Nullable
  1089. public final EditText getInputEditText() {
  1090. return input;
  1091. }
  1092. /**
  1093. * Retrieves the TextView that contains the dialog title. If you want to update the
  1094. * title, use #{@link #setTitle(CharSequence)} instead.
  1095. */
  1096. public final TextView getTitleView() {
  1097. return title;
  1098. }
  1099. /**
  1100. * Retrieves the TextView that contains the dialog content. If you want to update the
  1101. * content (message), use #{@link #setContent(CharSequence)} instead.
  1102. */
  1103. @Nullable
  1104. public final TextView getContentView() {
  1105. return content;
  1106. }
  1107. /**
  1108. * Retrieves the custom view that was inflated or set to the MaterialDialog during building.
  1109. *
  1110. * @return The custom view that was passed into the Builder.
  1111. */
  1112. @Nullable
  1113. public final View getCustomView() {
  1114. return mBuilder.customView;
  1115. }
  1116. /**
  1117. * Updates an action button's title, causing invalidation to check if the action buttons should be stacked.
  1118. * Setting an action button's text to null is a shortcut for hiding it, too.
  1119. *
  1120. * @param which The action button to update.
  1121. * @param title The new title of the action button.
  1122. */
  1123. @UiThread
  1124. public final void setActionButton(@NonNull final DialogAction which, final CharSequence title) {
  1125. switch (which) {
  1126. default:
  1127. mBuilder.positiveText = title;
  1128. positiveButton.setText(title);
  1129. positiveButton.setVisibility(title == null ? View.GONE : View.VISIBLE);
  1130. break;
  1131. case NEUTRAL:
  1132. mBuilder.neutralText = title;
  1133. neutralButton.setText(title);
  1134. neutralButton.setVisibility(title == null ? View.GONE : View.VISIBLE);
  1135. break;
  1136. case NEGATIVE:
  1137. mBuilder.negativeText = title;
  1138. negativeButton.setText(title);
  1139. negativeButton.setVisibility(title == null ? View.GONE : View.VISIBLE);
  1140. break;
  1141. }
  1142. }
  1143. /**
  1144. * Updates an action button's title, causing invalidation to check if the action buttons should be stacked.
  1145. *
  1146. * @param which The action button to update.
  1147. * @param titleRes The string resource of the new title of the action button.
  1148. */
  1149. public final void setActionButton(DialogAction which, @StringRes int titleRes) {
  1150. setActionButton(which, getContext().getText(titleRes));
  1151. }
  1152. /**
  1153. * Gets whether or not the positive, neutral, or negative action button is visible.
  1154. *
  1155. * @return Whether or not 1 or more action buttons is visible.
  1156. */
  1157. public final boolean hasActionButtons() {
  1158. return numberOfActionButtons() > 0;
  1159. }
  1160. /**
  1161. * Gets the number of visible action buttons.
  1162. *
  1163. * @return 0 through 3, depending on how many should be or are visible.
  1164. */
  1165. public final int numberOfActionButtons() {
  1166. int number = 0;
  1167. if (mBuilder.positiveText != null && positiveButton.getVisibility() == View.VISIBLE)
  1168. number++;
  1169. if (mBuilder.neutralText != null && neutralButton.getVisibility() == View.VISIBLE)
  1170. number++;
  1171. if (mBuilder.negativeText != null && negativeButton.getVisibility() == View.VISIBLE)
  1172. number++;
  1173. return number;
  1174. }
  1175. @UiThread
  1176. @Override
  1177. public final void setTitle(@NonNull CharSequence newTitle) {
  1178. title.setText(newTitle);
  1179. }
  1180. @UiThread
  1181. @Override
  1182. public final void setTitle(@StringRes int newTitleRes) {
  1183. setTitle(mBuilder.context.getString(newTitleRes));
  1184. }
  1185. @UiThread
  1186. public final void setTitle(@StringRes int newTitleRes, @Nullable Object... formatArgs) {
  1187. setTitle(mBuilder.context.getString(newTitleRes, formatArgs));
  1188. }
  1189. @UiThread
  1190. public void setIcon(@DrawableRes final int resId) {
  1191. icon.setImageResource(resId);
  1192. icon.setVisibility(resId != 0 ? View.VISIBLE : View.GONE);
  1193. }
  1194. @UiThread
  1195. public void setIcon(final Drawable d) {
  1196. icon.setImageDrawable(d);
  1197. icon.setVisibility(d != null ? View.VISIBLE : View.GONE);
  1198. }
  1199. @UiThread
  1200. public void setIconAttribute(@AttrRes int attrId) {
  1201. Drawable d = DialogUtils.resolveDrawable(mBuilder.context, attrId);
  1202. setIcon(d);
  1203. }
  1204. @UiThread
  1205. public final void setContent(CharSequence newContent) {
  1206. content.setText(newContent);
  1207. content.setVisibility(TextUtils.isEmpty(newContent) ? View.GONE : View.VISIBLE);
  1208. }
  1209. @UiThread
  1210. public final void setContent(@StringRes int newContentRes) {
  1211. setContent(mBuilder.context.getString(newContentRes));
  1212. }
  1213. @UiThread
  1214. public final void setContent(@StringRes int newContentRes, @Nullable Object... formatArgs) {
  1215. setContent(mBuilder.context.getString(newContentRes, formatArgs));
  1216. }
  1217. /**
  1218. * @deprecated Use setContent() instead.
  1219. */
  1220. @Deprecated
  1221. public void setMessage(CharSequence message) {
  1222. setContent(message);
  1223. }
  1224. @UiThread
  1225. public final void setItems(CharSequence[] items) {
  1226. if (mBuilder.adapter == null)
  1227. throw new IllegalStateException("This MaterialDialog instance does not yet have an adapter set to it. You cannot use setItems().");
  1228. mBuilder.items = items;
  1229. if (mBuilder.adapter instanceof MaterialDialogAdapter) {
  1230. mBuilder.adapter = new MaterialDialogAdapter(this, ListType.getLayoutForType(listType));
  1231. } else {
  1232. throw new IllegalStateException("When using a custom adapter, setItems() cannot be used. Set items through the adapter instead.");
  1233. }
  1234. listView.setAdapter(mBuilder.adapter);
  1235. }
  1236. public final int getCurrentProgress() {
  1237. if (mProgress == null) return -1;
  1238. return mProgress.getProgress();
  1239. }
  1240. public ProgressBar getProgressBar() {
  1241. return mProgress;
  1242. }
  1243. public final void incrementProgress(final int by) {
  1244. setProgress(getCurrentProgress() + by);
  1245. }
  1246. private Handler mHandler;
  1247. public final void setProgress(final int progress) {
  1248. if (mBuilder.progress <= -2)
  1249. throw new IllegalStateException("Cannot use setProgress() on this dialog.");
  1250. mProgress.setProgress(progress);
  1251. mHandler.post(new Runnable() {
  1252. @Override
  1253. public void run() {
  1254. if (mProgressLabel != null) {
  1255. // final int percentage = (int) (((float) getCurrentProgress() / (float) getMaxProgress()) * 100f);
  1256. mProgressLabel.setText(mBuilder.progressPercentFormat.format(
  1257. (float) getCurrentProgress() / (float) getMaxProgress()));
  1258. }
  1259. if (mProgressMinMax != null) {
  1260. mProgressMinMax.setText(String.format(mBuilder.progressNumberFormat,
  1261. getCurrentProgress(), getMaxProgress()));
  1262. }
  1263. }
  1264. });
  1265. }
  1266. public final void setMaxProgress(final int max) {
  1267. if (mBuilder.progress <= -2)
  1268. throw new IllegalStateException("Cannot use setMaxProgress() on this dialog.");
  1269. mProgress.setMax(max);
  1270. }
  1271. public final boolean isIndeterminateProgress() {
  1272. return mBuilder.indeterminateProgress;
  1273. }
  1274. public final int getMaxProgress() {
  1275. if (mProgress == null) return -1;
  1276. return mProgress.getMax();
  1277. }
  1278. /**
  1279. * Change the format of the small text showing the percentage of progress.
  1280. * The default is NumberFormat.getPercentageInstance().
  1281. */
  1282. public final void setProgressPercentFormat(NumberFormat format) {
  1283. mBuilder.progressPercentFormat = format;
  1284. setProgress(getCurrentProgress()); // invalidates display
  1285. }
  1286. /**
  1287. * Change the format of the small text showing current and maximum units of progress.
  1288. * The default is "%1d/%2d".
  1289. */
  1290. public final void setProgressNumberFormat(String format) {
  1291. mBuilder.progressNumberFormat = format;
  1292. setProgress(getCurrentProgress()); // invalidates display
  1293. }
  1294. public final boolean isCancelled() {
  1295. return !isShowing();
  1296. }
  1297. /**
  1298. * Convenience method for getting the currently selected index of a single choice list.
  1299. *
  1300. * @return Currently selected index of a single choice list, or -1 if not showing a single choice list
  1301. */
  1302. public int getSelectedIndex() {
  1303. if (mBuilder.listCallbackSingleChoice != null) {
  1304. return mBuilder.selectedIndex;
  1305. } else {
  1306. return -1;
  1307. }
  1308. }
  1309. /**
  1310. * Convenience method for getting the currently selected indices of a multi choice list
  1311. *
  1312. * @return Currently selected index of a multi choice list, or null if not showing a multi choice list
  1313. */
  1314. @Nullable
  1315. public Integer[] getSelectedIndices() {
  1316. if (mBuilder.listCallbackMultiChoice != null) {
  1317. return selectedIndicesList.toArray(new Integer[selectedIndicesList.size()]);
  1318. } else {
  1319. return null;
  1320. }
  1321. }
  1322. /**
  1323. * Convenience method for setting the currently selected index of a single choice list.
  1324. * This only works if you are not using a custom adapter; if you're using a custom adapter,
  1325. * an IllegalStateException is thrown. Note that this does not call the respective single choice callback.
  1326. *
  1327. * @param index The index of the list item to check.
  1328. */
  1329. @UiThread
  1330. public void setSelectedIndex(int index) {
  1331. mBuilder.selectedIndex = index;
  1332. if (mBuilder.adapter != null && mBuilder.adapter instanceof MaterialDialogAdapter) {
  1333. ((MaterialDialogAdapter) mBuilder.adapter).notifyDataSetChanged();
  1334. } else {
  1335. throw new IllegalStateException("You can only use setSelectedIndex() with the default adapter implementation.");
  1336. }
  1337. }
  1338. /**
  1339. * Convenience method for setting the currently selected indices of a multi choice list.
  1340. * This only works if you are not using a custom adapter; if you're using a custom adapter,
  1341. * an IllegalStateException is thrown. Note that this does not call the respective multi choice callback.
  1342. *
  1343. * @param indices The indices of the list items to check.
  1344. */
  1345. @UiThread
  1346. public void setSelectedIndices(@NonNull Integer[] indices) {
  1347. mBuilder.selectedIndices = indices;
  1348. selectedIndicesList = new ArrayList<>(Arrays.asList(indices));
  1349. if (mBuilder.adapter != null && mBuilder.adapter instanceof MaterialDialogAdapter) {
  1350. ((MaterialDialogAdapter) mBuilder.adapter).notifyDataSetChanged();
  1351. } else {
  1352. throw new IllegalStateException("You can only use setSelectedIndices() with the default adapter implementation.");
  1353. }
  1354. }
  1355. /**
  1356. * Clears all selected checkboxes from multi choice list dialogs.
  1357. */
  1358. public void clearSelectedIndices() {
  1359. if (selectedIndicesList == null)
  1360. throw new IllegalStateException("You can only use clearSelectedIndicies() with multi choice list dialogs.");
  1361. mBuilder.selectedIndices = null;
  1362. selectedIndicesList.clear();
  1363. if (mBuilder.adapter != null && mBuilder.adapter instanceof MaterialDialogAdapter) {
  1364. ((MaterialDialogAdapter) mBuilder.adapter).notifyDataSetChanged();
  1365. } else {
  1366. throw new IllegalStateException("You can only use clearSelectedIndicies() with the default adapter implementation.");
  1367. }
  1368. }
  1369. @Override
  1370. public final void onShow(DialogInterface dialog) {
  1371. if (input != null) {
  1372. DialogUtils.showKeyboard(this, mBuilder);
  1373. if (input.getText().length() > 0)
  1374. input.setSelection(input.getText().length());
  1375. }
  1376. super.onShow(dialog);
  1377. }
  1378. protected void setInternalInputCallback() {
  1379. if (input == null) return;
  1380. input.addTextChangedListener(new TextWatcher() {
  1381. @Override
  1382. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  1383. }
  1384. @Override
  1385. public void onTextChanged(CharSequence s, int start, int before, int count) {
  1386. final int length = s.toString().length();
  1387. boolean emptyDisabled = false;
  1388. if (!mBuilder.inputAllowEmpty) {
  1389. emptyDisabled = length == 0;
  1390. final View positiveAb = getActionButton(DialogAction.POSITIVE);
  1391. positiveAb.setEnabled(!emptyDisabled);
  1392. }
  1393. invalidateInputMinMaxIndicator(length, emptyDisabled);
  1394. if (mBuilder.alwaysCallInputCallback)
  1395. mBuilder.inputCallback.onInput(MaterialDialog.this, s);
  1396. }
  1397. @Override
  1398. public void afterTextChanged(Editable s) {
  1399. }
  1400. });
  1401. }
  1402. protected void invalidateInputMinMaxIndicator(int currentLength, boolean emptyDisabled) {
  1403. if (inputMinMax != null) {
  1404. inputMinMax.setText(currentLength + "/" + mBuilder.inputMaxLength);
  1405. final boolean isDisabled = (emptyDisabled && currentLength == 0) || currentLength > mBuilder.inputMaxLength;
  1406. final int colorText = isDisabled ? mBuilder.inputMaxLengthErrorColor : mBuilder.contentColor;
  1407. final int colorWidget = isDisabled ? mBuilder.inputMaxLengthErrorColor : mBuilder.widgetColor;
  1408. inputMinMax.setTextColor(colorText);
  1409. MDTintHelper.setTint(input, colorWidget);
  1410. final View positiveAb = getActionButton(DialogAction.POSITIVE);
  1411. positiveAb.setEnabled(!isDisabled);
  1412. }
  1413. }
  1414. @Override
  1415. protected void onStop() {
  1416. super.onStop();
  1417. if (input != null)
  1418. DialogUtils.hideKeyboard(this, mBuilder);
  1419. }
  1420. protected enum ListType {
  1421. REGULAR, SINGLE, MULTI;
  1422. public static int getLayoutForType(ListType type) {
  1423. switch (type) {
  1424. case REGULAR:
  1425. return R.layout.md_listitem;
  1426. case SINGLE:
  1427. return R.layout.md_listitem_singlechoice;
  1428. case MULTI:
  1429. return R.layout.md_listitem_multichoice;
  1430. default:
  1431. throw new IllegalArgumentException("Not a valid list type");
  1432. }
  1433. }
  1434. }
  1435. /**
  1436. * A callback used for regular list dialogs.
  1437. */
  1438. public interface ListCallback {
  1439. void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text);
  1440. }
  1441. /**
  1442. * A callback used for multi choice (check box) list dialogs.
  1443. */
  1444. public interface ListCallbackSingleChoice {
  1445. /**
  1446. * Return true to allow the radio button to be checked, if the alwaysCallSingleChoice() option is used.
  1447. *
  1448. * @param dialog The dialog of which a list item was selected.
  1449. * @param which The index of the item that was selected.
  1450. * @param text The text of the item that was selected.
  1451. * @return True to allow the radio button to be selected.
  1452. */
  1453. boolean onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text);
  1454. }
  1455. /**
  1456. * A callback used for multi choice (check box) list dialogs.
  1457. */
  1458. public interface ListCallbackMultiChoice {
  1459. /**
  1460. * Return true to allow the check box to be checked, if the alwaysCallSingleChoice() option is used.
  1461. *
  1462. * @param dialog The dialog of which a list item was selected.
  1463. * @param which The indices of the items that were selected.
  1464. * @param text The text of the items that were selected.
  1465. * @return True to allow the checkbox to be selected.
  1466. */
  1467. boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text);
  1468. }
  1469. /**
  1470. * Override these as needed, so no needing to sub empty methods from an interface
  1471. */
  1472. public static abstract class ButtonCallback {
  1473. public void onAny(MaterialDialog dialog) {
  1474. }
  1475. public void onPositive(MaterialDialog dialog) {
  1476. }
  1477. public void onNegative(MaterialDialog dialog) {
  1478. }
  1479. public void onNeutral(MaterialDialog dialog) {
  1480. }
  1481. public ButtonCallback() {
  1482. super();
  1483. }
  1484. @Override
  1485. protected final Object clone() throws CloneNotSupportedException {
  1486. return super.clone();
  1487. }
  1488. @Override
  1489. public final boolean equals(Object o) {
  1490. return super.equals(o);
  1491. }
  1492. @Override
  1493. protected final void finalize() throws Throwable {
  1494. super.finalize();
  1495. }
  1496. @Override
  1497. public final int hashCode() {
  1498. return super.hashCode();
  1499. }
  1500. @Override
  1501. public final String toString() {
  1502. return super.toString();
  1503. }
  1504. }
  1505. public interface InputCallback {
  1506. void onInput(MaterialDialog dialog, CharSequence input);
  1507. }
  1508. }