MaterialDialog.java 63 KB

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