MaterialDialog.java 40 KB

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