MaterialDialog.java 56 KB

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