MaterialDialog.java 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. package com.afollestad.materialdialogs;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.content.res.ColorStateList;
  6. import android.content.res.Resources;
  7. import android.content.res.TypedArray;
  8. import android.graphics.Paint;
  9. import android.graphics.Typeface;
  10. import android.graphics.drawable.Drawable;
  11. import android.os.Build;
  12. import android.support.annotation.ArrayRes;
  13. import android.support.annotation.ColorRes;
  14. import android.support.annotation.DrawableRes;
  15. import android.support.annotation.LayoutRes;
  16. import android.support.annotation.NonNull;
  17. import android.support.annotation.StringRes;
  18. import android.text.method.LinkMovementMethod;
  19. import android.view.ContextThemeWrapper;
  20. import android.view.Gravity;
  21. import android.view.LayoutInflater;
  22. import android.view.View;
  23. import android.widget.CheckBox;
  24. import android.widget.ImageView;
  25. import android.widget.LinearLayout;
  26. import android.widget.RadioButton;
  27. import android.widget.ScrollView;
  28. import android.widget.TextView;
  29. import com.afollestad.materialdialogs.base.DialogBase;
  30. import com.afollestad.materialdialogs.list.ItemProcessor;
  31. import com.afollestad.materialdialogs.views.MeasureCallbackScrollView;
  32. import java.util.ArrayList;
  33. import java.util.Arrays;
  34. import java.util.List;
  35. /**
  36. * @author Aidan Follestad (afollestad)
  37. */
  38. public class MaterialDialog extends DialogBase implements View.OnClickListener, MeasureCallbackScrollView.Callback {
  39. private Context mContext;
  40. private ImageView icon;
  41. private TextView title;
  42. private View titleFrame;
  43. private CharSequence positiveText;
  44. private TextView positiveButton;
  45. private CharSequence neutralText;
  46. private TextView neutralButton;
  47. private CharSequence negativeText;
  48. private TextView negativeButton;
  49. private View view;
  50. private int positiveColor;
  51. private int negativeColor;
  52. private int neutralColor;
  53. private SimpleCallback callback;
  54. private ListCallback listCallback;
  55. private ListCallback listCallbackSingle;
  56. private ListCallbackMulti listCallbackMulti;
  57. private View customView;
  58. private CharSequence[] items;
  59. private boolean isStacked;
  60. private int selectedIndex;
  61. private Integer[] selectedIndices;
  62. private boolean mMeasuredScrollView;
  63. private Typeface mediumFont;
  64. private Typeface regularFont;
  65. private ItemProcessor mItemProcessor;
  66. private boolean autoDismiss;
  67. MaterialDialog(Builder builder) {
  68. super(new ContextThemeWrapper(builder.context, builder.theme == Theme.LIGHT ? R.style.MD_Light : R.style.MD_Dark));
  69. this.regularFont = builder.regularFont;
  70. if (this.regularFont == null)
  71. this.regularFont = Typeface.createFromAsset(getContext().getResources().getAssets(), "Roboto-Regular.ttf");
  72. this.mediumFont = builder.mediumFont;
  73. if (this.mediumFont == null)
  74. this.mediumFont = Typeface.createFromAsset(getContext().getResources().getAssets(), "Roboto-Medium.ttf");
  75. this.mContext = builder.context;
  76. this.view = LayoutInflater.from(getContext()).inflate(R.layout.md_dialog, null);
  77. this.customView = builder.customView;
  78. this.callback = builder.callback;
  79. this.listCallback = builder.listCallback;
  80. this.listCallbackSingle = builder.listCallbackSingle;
  81. this.listCallbackMulti = builder.listCallbackMulti;
  82. this.positiveText = builder.positiveText;
  83. this.neutralText = builder.neutralText;
  84. this.negativeText = builder.negativeText;
  85. this.positiveColor = builder.positiveColor;
  86. this.negativeColor = builder.negativeColor;
  87. this.neutralColor = builder.neutralColor;
  88. this.items = builder.items;
  89. this.setCancelable(builder.cancelable);
  90. this.selectedIndex = builder.selectedIndex;
  91. this.selectedIndices = builder.selectedIndicies;
  92. this.mItemProcessor = builder.itemProcessor;
  93. this.autoDismiss = builder.autoDismiss;
  94. title = (TextView) view.findViewById(R.id.title);
  95. icon = (ImageView) view.findViewById(R.id.icon);
  96. titleFrame = view.findViewById(R.id.titleFrame);
  97. final TextView content = (TextView) view.findViewById(R.id.content);
  98. content.setText(builder.content);
  99. content.setMovementMethod(new LinkMovementMethod());
  100. setTypeface(content, regularFont);
  101. content.setTextColor(DialogUtils.resolveColor(getContext(), android.R.attr.textColorSecondary));
  102. content.setLineSpacing(0f, builder.contentLineSpacingMultiplier);
  103. if (this.positiveColor == 0) {
  104. content.setLinkTextColor(DialogUtils.resolveColor(getContext(), android.R.attr.textColorPrimary));
  105. } else {
  106. content.setLinkTextColor(this.positiveColor);
  107. }
  108. if (builder.contentAlignment == Alignment.CENTER) {
  109. content.setGravity(Gravity.CENTER_HORIZONTAL);
  110. } else if (builder.contentAlignment == Alignment.RIGHT) {
  111. content.setGravity(Gravity.RIGHT);
  112. }
  113. if (customView != null) {
  114. title = (TextView) view.findViewById(R.id.titleCustomView);
  115. icon = (ImageView) view.findViewById(R.id.iconCustomView);
  116. titleFrame = view.findViewById(R.id.titleFrameCustomView);
  117. invalidateCustomViewAssociations();
  118. ((LinearLayout) view.findViewById(R.id.customViewFrame)).addView(customView);
  119. } else {
  120. invalidateCustomViewAssociations();
  121. }
  122. if (builder.icon != null) {
  123. icon.setVisibility(View.VISIBLE);
  124. icon.setImageDrawable(builder.icon);
  125. } else {
  126. icon.setVisibility(View.GONE);
  127. }
  128. if ((negativeText != null || neutralText != null) && positiveText == null)
  129. positiveText = getContext().getString(android.R.string.ok);
  130. if (items != null && items.length > 0)
  131. title = (TextView) view.findViewById(R.id.titleCustomView);
  132. else if (positiveText == null && customView == null)
  133. positiveText = getContext().getString(android.R.string.ok);
  134. // Title is set after it's determined whether to use first title or custom view title
  135. if (builder.title == null || builder.title.toString().trim().isEmpty()) {
  136. titleFrame.setVisibility(View.GONE);
  137. } else {
  138. title.setText(builder.title);
  139. setTypeface(title, mediumFont);
  140. if (builder.titleColor != -1) {
  141. title.setTextColor(builder.titleColor);
  142. } else {
  143. title.setTextColor(DialogUtils.resolveColor(getContext(), android.R.attr.textColorPrimary));
  144. }
  145. if (builder.titleAlignment == Alignment.CENTER) {
  146. title.setGravity(Gravity.CENTER_HORIZONTAL);
  147. } else if (builder.titleAlignment == Alignment.RIGHT) {
  148. title.setGravity(Gravity.RIGHT);
  149. }
  150. }
  151. invalidateActions();
  152. setOnShowListenerInternal();
  153. setViewInternal(view);
  154. }
  155. @Override
  156. public void onShow(DialogInterface dialog) {
  157. super.onShow(dialog); // calls any external show listeners
  158. checkIfStackingNeeded();
  159. }
  160. /**
  161. * Invalidates visibility of views for the presence of a custom view or list content
  162. */
  163. private void invalidateCustomViewAssociations() {
  164. if (customView != null || (items != null && items.length > 0)) {
  165. view.findViewById(R.id.mainFrame).setVisibility(View.GONE);
  166. view.findViewById(R.id.customViewScrollParent).setVisibility(View.VISIBLE);
  167. if (!mMeasuredScrollView) {
  168. // Wait until it's measured
  169. ((MeasureCallbackScrollView) view.findViewById(R.id.customViewScroll)).setCallback(this);
  170. return;
  171. }
  172. if (canCustomViewScroll()) {
  173. view.findViewById(R.id.customViewDivider).setVisibility(View.VISIBLE);
  174. view.findViewById(R.id.customViewDivider).setBackgroundColor(DialogUtils.resolveColor(getContext(), R.attr.md_divider));
  175. setMargin(view.findViewById(R.id.buttonStackedFrame), -1, 0, -1, -1);
  176. setMargin(view.findViewById(R.id.buttonDefaultFrame), -1, 0, -1, -1);
  177. if (items != null && items.length > 0) {
  178. View customFrame = view.findViewById(R.id.customViewFrame);
  179. Resources r = getContext().getResources();
  180. int bottomPadding = view.findViewById(R.id.titleCustomView).getVisibility() == View.VISIBLE ?
  181. (int) r.getDimension(R.dimen.md_main_frame_margin) : (int) r.getDimension(R.dimen.md_dialog_frame_margin);
  182. customFrame.setPadding(customFrame.getPaddingLeft(), customFrame.getPaddingTop(),
  183. customFrame.getPaddingRight(), bottomPadding);
  184. }
  185. } else {
  186. view.findViewById(R.id.customViewDivider).setVisibility(View.GONE);
  187. final int bottomMargin = (int) getContext().getResources().getDimension(R.dimen.md_button_padding_frame_bottom);
  188. setMargin(view.findViewById(R.id.buttonStackedFrame), -1, bottomMargin, -1, -1);
  189. setMargin(view.findViewById(R.id.buttonDefaultFrame), -1, bottomMargin, -1, -1);
  190. }
  191. } else {
  192. view.findViewById(R.id.mainFrame).setVisibility(View.VISIBLE);
  193. view.findViewById(R.id.customViewScrollParent).setVisibility(View.GONE);
  194. view.findViewById(R.id.customViewDivider).setVisibility(View.GONE);
  195. if (!mMeasuredScrollView) {
  196. // Wait until it's measured
  197. ((MeasureCallbackScrollView) view.findViewById(R.id.contentScrollView)).setCallback(this);
  198. return;
  199. }
  200. if (canContentScroll()) {
  201. view.findViewById(R.id.customViewDivider).setVisibility(View.VISIBLE);
  202. view.findViewById(R.id.customViewDivider).setBackgroundColor(DialogUtils.resolveColor(getContext(), R.attr.md_divider));
  203. setMargin(view.findViewById(R.id.mainFrame), -1, 0, -1, -1);
  204. setMargin(view.findViewById(R.id.buttonStackedFrame), -1, 0, -1, -1);
  205. setMargin(view.findViewById(R.id.buttonDefaultFrame), -1, 0, -1, -1);
  206. final int conPadding = (int) getContext().getResources().getDimension(R.dimen.md_main_frame_margin);
  207. View con = view.findViewById(R.id.content);
  208. con.setPadding(con.getPaddingLeft(), 0, con.getPaddingRight(), conPadding);
  209. } else {
  210. View con = view.findViewById(R.id.content);
  211. con.setPadding(con.getPaddingLeft(), 0, con.getPaddingRight(), 0);
  212. }
  213. }
  214. }
  215. /**
  216. * Invalidates the radio buttons in the single choice mode list so that only the radio button that
  217. * was previous selected is checked.
  218. */
  219. private void invalidateSingleChoice(int newSelection) {
  220. newSelection++;
  221. final LinearLayout list = (LinearLayout) view.findViewById(R.id.customViewFrame);
  222. for (int i = 1; i < list.getChildCount(); i++) {
  223. View v = list.getChildAt(i);
  224. @SuppressLint("WrongViewCast")
  225. RadioButton rb = (RadioButton) v.findViewById(R.id.control);
  226. if (newSelection != i) {
  227. rb.setChecked(false);
  228. rb.clearFocus();
  229. }
  230. }
  231. }
  232. /**
  233. * Constructs the dialog's list content and sets up click listeners.
  234. */
  235. @SuppressLint("WrongViewCast")
  236. private void invalidateList() {
  237. if (items == null || items.length == 0) return;
  238. view.findViewById(R.id.contentScrollView).setVisibility(View.GONE);
  239. view.findViewById(R.id.customViewScrollParent).setVisibility(View.VISIBLE);
  240. LinearLayout customFrame = (LinearLayout) view.findViewById(R.id.customViewFrame);
  241. ((ScrollView) view.findViewById(R.id.customViewScroll)).smoothScrollTo(0, 0);
  242. setMargin(customFrame, -1, -1, 0, 0);
  243. LayoutInflater li = LayoutInflater.from(mContext);
  244. final int customFramePadding = (int) getContext().getResources().getDimension(R.dimen.md_title_margin_plainlist);
  245. int listPaddingBottom;
  246. View title = view.findViewById(R.id.titleCustomView);
  247. if (title.getVisibility() == View.VISIBLE) {
  248. title.setPadding(customFramePadding, title.getPaddingTop(), customFramePadding, title.getPaddingBottom());
  249. listPaddingBottom = customFramePadding;
  250. } else {
  251. listPaddingBottom = (int) getContext().getResources().getDimension(R.dimen.md_main_frame_margin);
  252. }
  253. if (positiveText != null) listPaddingBottom = 0;
  254. customFrame.setPadding(customFrame.getPaddingLeft(), customFrame.getPaddingTop(),
  255. customFrame.getPaddingRight(), listPaddingBottom);
  256. View titleFrame = (View) title.getParent();
  257. customFrame.removeAllViews();
  258. customFrame.addView(titleFrame);
  259. final int itemColor = DialogUtils.resolveColor(getContext(), android.R.attr.textColorSecondary);
  260. for (int index = 0; index < items.length; index++) {
  261. View il;
  262. if (listCallbackSingle != null) {
  263. il = li.inflate(R.layout.md_listitem_singlechoice, null);
  264. if (selectedIndex > -1) {
  265. RadioButton control = (RadioButton) il.findViewById(R.id.control);
  266. if (selectedIndex == index) control.setChecked(true);
  267. }
  268. TextView tv = (TextView) il.findViewById(R.id.title);
  269. tv.setText(items[index]);
  270. tv.setTextColor(itemColor);
  271. setTypeface(tv, regularFont);
  272. } else if (listCallbackMulti != null) {
  273. il = li.inflate(R.layout.md_listitem_multichoice, null);
  274. if (selectedIndices != null) {
  275. if (Arrays.asList(selectedIndices).contains(index)) {
  276. CheckBox control = (CheckBox) il.findViewById(R.id.control);
  277. control.setChecked(true);
  278. }
  279. }
  280. TextView tv = (TextView) il.findViewById(R.id.title);
  281. tv.setText(items[index]);
  282. tv.setTextColor(itemColor);
  283. setTypeface(tv, regularFont);
  284. } else {
  285. if (mItemProcessor != null) {
  286. il = mItemProcessor.inflateItem(index, items[index]);
  287. } else {
  288. il = li.inflate(R.layout.md_listitem, null);
  289. TextView tv = (TextView) il.findViewById(R.id.title);
  290. tv.setText(items[index]);
  291. tv.setTextColor(itemColor);
  292. setTypeface(tv, regularFont);
  293. }
  294. }
  295. il.setTag(index + ":" + items[index]);
  296. il.setOnClickListener(this);
  297. setBackgroundCompat(il, DialogUtils.resolveDrawable(getContext(), R.attr.md_selector));
  298. customFrame.addView(il);
  299. }
  300. }
  301. private int calculateMaxButtonWidth() {
  302. /**
  303. * Max button width = (DialogWidth - 16dp - 16dp - 8dp) / 2
  304. * From: http://www.google.com/design/spec/components/dialogs.html#dialogs-specs
  305. */
  306. final int dialogWidth = getWindow().getDecorView().getMeasuredWidth();
  307. final int eightDp = (int) getContext().getResources().getDimension(R.dimen.md_button_padding_horizontal_external);
  308. final int sixteenDp = (int) getContext().getResources().getDimension(R.dimen.md_button_padding_frame_side);
  309. return (dialogWidth - sixteenDp - sixteenDp - eightDp) / 2;
  310. }
  311. private boolean canCustomViewScroll() {
  312. /**
  313. * Detects whether or not the custom view or list content can be scrolled.
  314. */
  315. final ScrollView scrollView = (ScrollView) view.findViewById(R.id.customViewScroll);
  316. final int childHeight = view.findViewById(R.id.customViewFrame).getMeasuredHeight();
  317. return scrollView.getMeasuredHeight() < childHeight;
  318. }
  319. private boolean canContentScroll() {
  320. /**
  321. * Detects whether or not the custom view or list content can be scrolled.
  322. */
  323. final ScrollView scrollView = (ScrollView) view.findViewById(R.id.contentScrollView);
  324. final int childHeight = view.findViewById(R.id.content).getMeasuredHeight();
  325. return scrollView.getMeasuredHeight() < childHeight;
  326. }
  327. private void checkIfStackingNeeded() {
  328. /**
  329. * Measures the action button's and their text to decide whether or not the button should be stacked.
  330. */
  331. if (((negativeButton == null || negativeButton.getVisibility() == View.GONE) &&
  332. (neutralButton == null || neutralButton.getVisibility() == View.GONE))) {
  333. // Stacking isn't necessary if you only have one button
  334. return;
  335. }
  336. final int maxWidth = calculateMaxButtonWidth();
  337. final Paint paint = positiveButton.getPaint();
  338. final int eightDp = (int) getContext().getResources().getDimension(R.dimen.md_button_padding_horizontal_external);
  339. final int positiveWidth = (int) paint.measureText(positiveButton.getText().toString()) + (eightDp * 2);
  340. isStacked = positiveWidth > maxWidth;
  341. if (!isStacked && this.neutralText != null) {
  342. final int neutralWidth = (int) paint.measureText(neutralButton.getText().toString()) + (eightDp * 2);
  343. isStacked = neutralWidth > maxWidth;
  344. }
  345. if (!isStacked && this.negativeText != null) {
  346. final int negativeWidth = (int) paint.measureText(negativeButton.getText().toString()) + (eightDp * 2);
  347. isStacked = negativeWidth > maxWidth;
  348. }
  349. invalidateActions();
  350. }
  351. private boolean invalidateActions() {
  352. /**
  353. * Invalidates the positive/neutral/negative action buttons. Decides whether they should be visible
  354. * and sets their properties (such as height, text color, etc.).
  355. */
  356. if (positiveText == null) {
  357. // If the dialog is a plain list dialog, no buttons are shown.
  358. view.findViewById(R.id.buttonDefaultFrame).setVisibility(View.GONE);
  359. view.findViewById(R.id.buttonStackedFrame).setVisibility(View.GONE);
  360. invalidateList();
  361. return false;
  362. }
  363. if (isStacked) {
  364. view.findViewById(R.id.buttonDefaultFrame).setVisibility(View.GONE);
  365. view.findViewById(R.id.buttonStackedFrame).setVisibility(View.VISIBLE);
  366. } else {
  367. view.findViewById(R.id.buttonDefaultFrame).setVisibility(View.VISIBLE);
  368. view.findViewById(R.id.buttonStackedFrame).setVisibility(View.GONE);
  369. }
  370. positiveButton = (TextView) view.findViewById(
  371. isStacked ? R.id.buttonStackedPositive : R.id.buttonDefaultPositive);
  372. if (this.positiveText != null) {
  373. setTypeface(positiveButton, mediumFont);
  374. positiveButton.setText(this.positiveText);
  375. positiveButton.setTextColor(getActionTextStateList(this.positiveColor));
  376. setBackgroundCompat(positiveButton, DialogUtils.resolveDrawable(getContext(), R.attr.md_selector));
  377. positiveButton.setTag(POSITIVE);
  378. positiveButton.setOnClickListener(this);
  379. } else {
  380. positiveButton.setVisibility(View.GONE);
  381. }
  382. neutralButton = (TextView) view.findViewById(
  383. isStacked ? R.id.buttonStackedNeutral : R.id.buttonDefaultNeutral);
  384. if (this.neutralText != null) {
  385. setTypeface(neutralButton, mediumFont);
  386. neutralButton.setVisibility(View.VISIBLE);
  387. neutralButton.setTextColor(getActionTextStateList(this.neutralColor));
  388. setBackgroundCompat(neutralButton, DialogUtils.resolveDrawable(getContext(), R.attr.md_selector));
  389. neutralButton.setText(this.neutralText);
  390. neutralButton.setTag(NEUTRAL);
  391. neutralButton.setOnClickListener(this);
  392. } else {
  393. neutralButton.setVisibility(View.GONE);
  394. }
  395. negativeButton = (TextView) view.findViewById(
  396. isStacked ? R.id.buttonStackedNegative : R.id.buttonDefaultNegative);
  397. if (this.negativeText != null) {
  398. setTypeface(negativeButton, mediumFont);
  399. negativeButton.setVisibility(View.VISIBLE);
  400. negativeButton.setTextColor(getActionTextStateList(this.negativeColor));
  401. setBackgroundCompat(negativeButton, DialogUtils.resolveDrawable(getContext(), R.attr.md_selector));
  402. negativeButton.setText(this.negativeText);
  403. negativeButton.setTag(NEGATIVE);
  404. negativeButton.setOnClickListener(this);
  405. } else {
  406. negativeButton.setVisibility(View.GONE);
  407. }
  408. invalidateList();
  409. return true;
  410. }
  411. private void sendSingleChoiceCallback(View v) {
  412. LinearLayout list = (LinearLayout) view.findViewById(R.id.customViewFrame);
  413. for (int i = 1; i < list.getChildCount(); i++) {
  414. View itemView = list.getChildAt(i);
  415. @SuppressLint("WrongViewCast")
  416. RadioButton rb = (RadioButton) itemView.findViewById(R.id.control);
  417. if (rb.isChecked()) {
  418. listCallbackSingle.onSelection(this, v, i - 1, ((TextView) itemView.findViewById(R.id.title)).getText());
  419. break;
  420. }
  421. }
  422. }
  423. private void sendMultichoiceCallback() {
  424. List<Integer> selectedIndices = new ArrayList<>();
  425. List<CharSequence> selectedTitles = new ArrayList<>();
  426. LinearLayout list = (LinearLayout) view.findViewById(R.id.customViewFrame);
  427. for (int i = 1; i < list.getChildCount(); i++) {
  428. View itemView = list.getChildAt(i);
  429. @SuppressLint("WrongViewCast")
  430. CheckBox rb = (CheckBox) itemView.findViewById(R.id.control);
  431. if (rb.isChecked()) {
  432. selectedIndices.add(i - 1);
  433. selectedTitles.add(((TextView) itemView.findViewById(R.id.title)).getText());
  434. }
  435. }
  436. listCallbackMulti.onSelection(this,
  437. selectedIndices.toArray(new Integer[selectedIndices.size()]),
  438. selectedTitles.toArray(new CharSequence[selectedTitles.size()]));
  439. }
  440. @Override
  441. public final void onClick(View v) {
  442. String tag = (String) v.getTag();
  443. if (tag.equals(POSITIVE)) {
  444. if (listCallbackSingle != null) {
  445. if (autoDismiss) dismiss();
  446. sendSingleChoiceCallback(v);
  447. } else if (listCallbackMulti != null) {
  448. if (autoDismiss) dismiss();
  449. sendMultichoiceCallback();
  450. } else if (callback != null) {
  451. if (autoDismiss) dismiss();
  452. callback.onPositive(this);
  453. } else if (autoDismiss) dismiss();
  454. } else if (tag.equals(NEGATIVE)) {
  455. if (callback != null && callback instanceof Callback) {
  456. if (autoDismiss) dismiss();
  457. ((Callback) callback).onNegative(this);
  458. } else if (autoDismiss) dismiss();
  459. } else if (tag.equals(NEUTRAL)) {
  460. if (callback != null && callback instanceof FullCallback) {
  461. if (autoDismiss) dismiss();
  462. ((FullCallback) callback).onNeutral(this);
  463. } else if (autoDismiss) dismiss();
  464. } else {
  465. String[] split = tag.split(":");
  466. int index = Integer.parseInt(split[0]);
  467. if (listCallback != null) {
  468. if (autoDismiss) dismiss();
  469. listCallback.onSelection(this, v, index, split[1]);
  470. } else if (listCallbackSingle != null) {
  471. RadioButton cb = (RadioButton) ((LinearLayout) v).getChildAt(0);
  472. if (!cb.isChecked())
  473. cb.setChecked(true);
  474. invalidateSingleChoice(index);
  475. if (positiveText == null) {
  476. // Immediately send the selection callback without dismissing if no action buttons are shown
  477. sendSingleChoiceCallback(v);
  478. }
  479. } else if (listCallbackMulti != null) {
  480. CheckBox cb = (CheckBox) ((LinearLayout) v).getChildAt(0);
  481. cb.setChecked(!cb.isChecked());
  482. if (positiveText == null) {
  483. // Immediately send the selection callback without dismissing if no action buttons are shown
  484. sendMultichoiceCallback();
  485. }
  486. } else if (autoDismiss) dismiss();
  487. }
  488. }
  489. @Override
  490. public void onMeasureScroll(ScrollView view) {
  491. if (view.getMeasuredWidth() > 0) {
  492. mMeasuredScrollView = true;
  493. invalidateCustomViewAssociations();
  494. }
  495. }
  496. /**
  497. * The class used to construct a MaterialDialog.
  498. */
  499. public static class Builder {
  500. protected Context context;
  501. protected CharSequence title;
  502. protected Alignment titleAlignment = Alignment.LEFT;
  503. protected Alignment contentAlignment = Alignment.LEFT;
  504. protected int titleColor = -1;
  505. protected CharSequence content;
  506. protected CharSequence[] items;
  507. protected CharSequence positiveText;
  508. protected CharSequence neutralText;
  509. protected CharSequence negativeText;
  510. protected View customView;
  511. protected int positiveColor;
  512. protected int negativeColor;
  513. protected int neutralColor;
  514. protected SimpleCallback callback;
  515. protected ListCallback listCallback;
  516. protected ListCallback listCallbackSingle;
  517. protected ListCallbackMulti listCallbackMulti;
  518. protected Theme theme = Theme.LIGHT;
  519. protected boolean cancelable = true;
  520. protected float contentLineSpacingMultiplier = 1.3f;
  521. protected int selectedIndex = -1;
  522. protected Integer[] selectedIndicies = null;
  523. protected ItemProcessor itemProcessor;
  524. protected boolean autoDismiss = true;
  525. protected Typeface regularFont;
  526. protected Typeface mediumFont;
  527. protected Drawable icon;
  528. public Builder(@NonNull Context context) {
  529. this.context = context;
  530. final int materialBlue = context.getResources().getColor(R.color.md_material_blue_500);
  531. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  532. TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{android.R.attr.colorAccent});
  533. try {
  534. this.positiveColor = a.getColor(0, materialBlue);
  535. this.negativeColor = a.getColor(0, materialBlue);
  536. this.neutralColor = a.getColor(0, materialBlue);
  537. } catch (Exception e) {
  538. this.positiveColor = materialBlue;
  539. this.negativeColor = materialBlue;
  540. this.neutralColor = materialBlue;
  541. } finally {
  542. a.recycle();
  543. }
  544. } else {
  545. TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{R.attr.colorAccent});
  546. try {
  547. this.positiveColor = a.getColor(0, materialBlue);
  548. this.negativeColor = a.getColor(0, materialBlue);
  549. this.neutralColor = a.getColor(0, materialBlue);
  550. } catch (Exception e) {
  551. this.positiveColor = materialBlue;
  552. this.negativeColor = materialBlue;
  553. this.neutralColor = materialBlue;
  554. } finally {
  555. a.recycle();
  556. }
  557. }
  558. }
  559. public Builder title(@StringRes int titleRes) {
  560. title(this.context.getString(titleRes));
  561. return this;
  562. }
  563. public Builder title(CharSequence title) {
  564. this.title = title;
  565. return this;
  566. }
  567. public Builder titleAlignment(Alignment align) {
  568. this.titleAlignment = align;
  569. return this;
  570. }
  571. public Builder titleColorRes(@ColorRes int colorRes) {
  572. titleColor(this.context.getResources().getColor(colorRes));
  573. return this;
  574. }
  575. /**
  576. * Sets the fonts used in the dialog.
  577. *
  578. * @param medium The font used on titles and action buttons. Null uses the default.
  579. * @param regular The font used everywhere else, like on the content and list items. Null uses the default.
  580. * @return The Builder instance so you can chain calls to it.
  581. */
  582. public Builder typeface(Typeface medium, Typeface regular) {
  583. this.mediumFont = medium;
  584. this.regularFont = regular;
  585. return this;
  586. }
  587. public Builder titleColor(int color) {
  588. this.titleColor = color;
  589. return this;
  590. }
  591. public Builder icon(Drawable icon) {
  592. this.icon = icon;
  593. return this;
  594. }
  595. public Builder icon(@DrawableRes int icon) {
  596. this.icon = context.getResources().getDrawable(icon);
  597. return this;
  598. }
  599. public Builder iconAttr(int iconAttr) {
  600. this.icon = DialogUtils.resolveDrawable(context, iconAttr);
  601. return this;
  602. }
  603. public Builder content(@StringRes int contentRes) {
  604. content(this.context.getString(contentRes));
  605. return this;
  606. }
  607. public Builder content(CharSequence content) {
  608. this.content = content;
  609. return this;
  610. }
  611. public Builder content(@StringRes int contentRes, Object... formatArgs) {
  612. content(this.context.getString(contentRes, formatArgs));
  613. return this;
  614. }
  615. public Builder contentAlignment(Alignment align) {
  616. this.contentAlignment = align;
  617. return this;
  618. }
  619. public Builder contentLineSpacing(float multiplier) {
  620. this.contentLineSpacingMultiplier = multiplier;
  621. return this;
  622. }
  623. public Builder items(@ArrayRes int itemsRes) {
  624. items(this.context.getResources().getStringArray(itemsRes));
  625. return this;
  626. }
  627. public Builder items(CharSequence[] items) {
  628. this.items = items;
  629. return this;
  630. }
  631. public Builder itemsCallback(ListCallback callback) {
  632. this.listCallback = callback;
  633. this.listCallbackSingle = null;
  634. this.listCallbackMulti = null;
  635. return this;
  636. }
  637. /**
  638. * Sets an item processor used to inflate and customize list items (NOT including single and
  639. * multi choice list items).
  640. *
  641. * @param processor The processor to apply to all non single/multi choice list items.
  642. * @return The Builder instance so you can chain calls to it.
  643. */
  644. public Builder itemProcessor(ItemProcessor processor) {
  645. this.itemProcessor = processor;
  646. return this;
  647. }
  648. /**
  649. * Pass anything below 0 (such as -1) for the selected index to leave all options unselected initially.
  650. * Otherwise pass the index of an item that will be selected initially.
  651. *
  652. * @param selectedIndex The checkbox index that will be selected initially.
  653. * @param callback The callback that will be called when the presses the positive button.
  654. * @return The Builder instance so you can chain calls to it.
  655. */
  656. public Builder itemsCallbackSingleChoice(int selectedIndex, ListCallback callback) {
  657. this.selectedIndex = selectedIndex;
  658. this.listCallback = null;
  659. this.listCallbackSingle = callback;
  660. this.listCallbackMulti = null;
  661. return this;
  662. }
  663. /**
  664. * Pass null for the selected indices to leave all options unselected initially. Otherwise pass
  665. * an array of indices that will be selected initially.
  666. *
  667. * @param selectedIndices The radio button indices that will be selected initially.
  668. * @param callback The callback that will be called when the presses the positive button.
  669. * @return The Builder instance so you can chain calls to it.
  670. */
  671. public Builder itemsCallbackMultiChoice(Integer[] selectedIndices, ListCallbackMulti callback) {
  672. this.selectedIndicies = selectedIndices;
  673. this.listCallback = null;
  674. this.listCallbackSingle = null;
  675. this.listCallbackMulti = callback;
  676. return this;
  677. }
  678. public Builder positiveText(@StringRes int postiveRes) {
  679. positiveText(this.context.getString(postiveRes));
  680. return this;
  681. }
  682. public Builder positiveText(CharSequence message) {
  683. this.positiveText = message;
  684. return this;
  685. }
  686. public Builder neutralText(@StringRes int neutralRes) {
  687. neutralText(this.context.getString(neutralRes));
  688. return this;
  689. }
  690. public Builder neutralText(CharSequence message) {
  691. this.neutralText = message;
  692. return this;
  693. }
  694. public Builder negativeText(@StringRes int negativeRes) {
  695. negativeText(this.context.getString(negativeRes));
  696. return this;
  697. }
  698. public Builder negativeText(CharSequence message) {
  699. this.negativeText = message;
  700. return this;
  701. }
  702. public Builder customView(@LayoutRes int layoutRes) {
  703. LayoutInflater li = LayoutInflater.from(this.context);
  704. customView(li.inflate(layoutRes, null));
  705. return this;
  706. }
  707. public Builder customView(View view) {
  708. this.customView = view;
  709. return this;
  710. }
  711. public Builder positiveColorRes(@ColorRes int colorRes) {
  712. positiveColor(this.context.getResources().getColor(colorRes));
  713. return this;
  714. }
  715. public Builder positiveColor(int color) {
  716. this.positiveColor = color;
  717. return this;
  718. }
  719. public Builder negativeColorRes(@ColorRes int colorRes) {
  720. negativeColor(this.context.getResources().getColor(colorRes));
  721. return this;
  722. }
  723. public Builder negativeColor(int color) {
  724. this.negativeColor = color;
  725. return this;
  726. }
  727. public Builder neutralColorRes(@ColorRes int colorRes) {
  728. neutralColor(this.context.getResources().getColor(colorRes));
  729. return this;
  730. }
  731. public Builder neutralColor(int color) {
  732. this.neutralColor = color;
  733. return this;
  734. }
  735. public Builder callback(SimpleCallback callback) {
  736. this.callback = callback;
  737. return this;
  738. }
  739. public Builder theme(Theme theme) {
  740. this.theme = theme;
  741. return this;
  742. }
  743. public Builder cancelable(boolean cancelable) {
  744. this.cancelable = cancelable;
  745. return this;
  746. }
  747. /**
  748. * This defaults to true. If set to false, the dialog will not automatically be dismissed
  749. * when an action button is pressed, and not automatically dismissed when the user selects
  750. * a list item.
  751. *
  752. * @param dismiss Whether or not to dismiss the dialog automatically.
  753. * @return The Builder instance so you can chain calls to it.
  754. */
  755. public Builder autoDismiss(boolean dismiss) {
  756. this.autoDismiss = dismiss;
  757. return this;
  758. }
  759. public MaterialDialog build() {
  760. return new MaterialDialog(this);
  761. }
  762. }
  763. private ColorStateList getActionTextStateList(int newPrimaryColor) {
  764. final int buttonColor = DialogUtils.resolveColor(getContext(), android.R.attr.textColorPrimary);
  765. if (newPrimaryColor == 0) newPrimaryColor = buttonColor;
  766. int[][] states = new int[][]{
  767. new int[]{-android.R.attr.state_enabled}, // disabled
  768. new int[]{} // enabled
  769. };
  770. int[] colors = new int[]{
  771. DialogUtils.adjustAlpha(buttonColor, 0.6f),
  772. newPrimaryColor
  773. };
  774. return new ColorStateList(states, colors);
  775. }
  776. /**
  777. * Retrieves the view of an action button, allowing you to modify properties such as whether or not it's enabled.
  778. *
  779. * @param which The action button of which to get the view for.
  780. * @return The view from the dialog's layout representing this action button.
  781. */
  782. public final View getActionButton(DialogAction which) {
  783. if (view == null) return null;
  784. if (isStacked) {
  785. switch (which) {
  786. default:
  787. return view.findViewById(R.id.buttonStackedPositive);
  788. case NEUTRAL:
  789. return view.findViewById(R.id.buttonStackedNeutral);
  790. case NEGATIVE:
  791. return view.findViewById(R.id.buttonStackedNegative);
  792. }
  793. } else {
  794. switch (which) {
  795. default:
  796. return view.findViewById(R.id.buttonDefaultPositive);
  797. case NEUTRAL:
  798. return view.findViewById(R.id.buttonDefaultNeutral);
  799. case NEGATIVE:
  800. return view.findViewById(R.id.buttonDefaultNegative);
  801. }
  802. }
  803. }
  804. /**
  805. * Retrieves the frame view containing the title and icon. You can manually change visibility and retrieve children.
  806. */
  807. public final View getTitleFrame() {
  808. return titleFrame;
  809. }
  810. /**
  811. * Retrieves the custom view that was inflated or set to the MaterialDialog during building.
  812. *
  813. * @return The custom view that was passed into the Builder.
  814. */
  815. public final View getCustomView() {
  816. return customView;
  817. }
  818. /**
  819. * Updates an action button's title, causing invalidation to check if the action buttons should be stacked.
  820. *
  821. * @param which The action button to update.
  822. * @param title The new title of the action button.
  823. */
  824. public final void setActionButton(DialogAction which, CharSequence title) {
  825. switch (which) {
  826. default:
  827. this.positiveText = title;
  828. break;
  829. case NEUTRAL:
  830. this.neutralText = title;
  831. break;
  832. case NEGATIVE:
  833. this.negativeText = title;
  834. break;
  835. }
  836. invalidateActions();
  837. }
  838. /**
  839. * Updates an action button's title, causing invalidation to check if the action buttons should be stacked.
  840. *
  841. * @param which The action button to update.
  842. * @param titleRes The string resource of the new title of the action button.
  843. */
  844. public final void setActionButton(DialogAction which, @StringRes int titleRes) {
  845. setActionButton(which, getContext().getString(titleRes));
  846. }
  847. /**
  848. * Updates the dialog's title.
  849. */
  850. public final void setTitle(CharSequence title) {
  851. this.title.setText(title);
  852. }
  853. @Override
  854. public void setIcon(int resId) {
  855. icon.setImageResource(resId);
  856. icon.setVisibility(resId != 0 ? View.VISIBLE : View.GONE);
  857. }
  858. @Override
  859. public void setIcon(Drawable d) {
  860. icon.setImageDrawable(d);
  861. icon.setVisibility(d != null ? View.VISIBLE : View.GONE);
  862. }
  863. @Override
  864. public void setIconAttribute(int attrId) {
  865. Drawable d = DialogUtils.resolveDrawable(getContext(), attrId);
  866. icon.setImageDrawable(d);
  867. icon.setVisibility(d != null ? View.VISIBLE : View.GONE);
  868. }
  869. public final void setContent(CharSequence content) {
  870. ((TextView) view.findViewById(R.id.content)).setText(content);
  871. }
  872. public final void setItems(CharSequence[] items) {
  873. this.items = items;
  874. invalidateList();
  875. }
  876. public static interface ListCallback {
  877. void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text);
  878. }
  879. public static interface ListCallbackMulti {
  880. void onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text);
  881. }
  882. public static interface SimpleCallback {
  883. void onPositive(MaterialDialog dialog);
  884. }
  885. public static interface Callback extends SimpleCallback {
  886. void onPositive(MaterialDialog dialog);
  887. void onNegative(MaterialDialog dialog);
  888. }
  889. public static interface FullCallback extends Callback {
  890. void onNeutral(MaterialDialog dialog);
  891. }
  892. }