MaterialDialog.java 41 KB

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