MaterialDialog.java 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  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.database.DataSetObserver;
  9. import android.graphics.Color;
  10. import android.graphics.Typeface;
  11. import android.graphics.drawable.Drawable;
  12. import android.os.Build;
  13. import android.support.annotation.ArrayRes;
  14. import android.support.annotation.ColorRes;
  15. import android.support.annotation.DrawableRes;
  16. import android.support.annotation.LayoutRes;
  17. import android.support.annotation.NonNull;
  18. import android.support.annotation.Nullable;
  19. import android.support.annotation.StringRes;
  20. import android.text.method.LinkMovementMethod;
  21. import android.view.ContextThemeWrapper;
  22. import android.view.Gravity;
  23. import android.view.LayoutInflater;
  24. import android.view.View;
  25. import android.view.ViewGroup;
  26. import android.widget.AdapterView;
  27. import android.widget.ArrayAdapter;
  28. import android.widget.Button;
  29. import android.widget.CheckBox;
  30. import android.widget.ImageView;
  31. import android.widget.LinearLayout;
  32. import android.widget.ListAdapter;
  33. import android.widget.ListView;
  34. import android.widget.RadioButton;
  35. import android.widget.RelativeLayout;
  36. import android.widget.ScrollView;
  37. import android.widget.TextView;
  38. import com.afollestad.materialdialogs.base.DialogBase;
  39. import com.afollestad.materialdialogs.views.MeasureCallbackListView;
  40. import com.afollestad.materialdialogs.views.MeasureCallbackScrollView;
  41. import java.util.ArrayList;
  42. import java.util.Arrays;
  43. import java.util.List;
  44. /**
  45. * @author Aidan Follestad (afollestad)
  46. */
  47. public class MaterialDialog extends DialogBase implements View.OnClickListener, MeasureCallbackScrollView.Callback, MeasureCallbackListView.Callback {
  48. private ImageView icon;
  49. private TextView title;
  50. private View titleFrame;
  51. private final int defaultItemColor;
  52. private Context mContext;
  53. private CharSequence positiveText;
  54. private TextView positiveButton;
  55. private CharSequence neutralText;
  56. private TextView neutralButton;
  57. private CharSequence negativeText;
  58. private TextView negativeButton;
  59. private View view;
  60. private ListView listView;
  61. private int positiveColor;
  62. private int negativeColor;
  63. private int neutralColor;
  64. private ButtonCallback callback;
  65. private ListCallback listCallback;
  66. private ListCallback listCallbackSingle;
  67. private ListCallbackMulti listCallbackMulti;
  68. private View customView;
  69. private CharSequence[] items;
  70. private boolean isStacked;
  71. private int selectedIndex;
  72. private Integer[] selectedIndices;
  73. private boolean mMeasuredScrollView;
  74. private Typeface mediumFont;
  75. private Typeface regularFont;
  76. private boolean autoDismiss;
  77. private ListAdapter adapter;
  78. private ListType listType;
  79. private List<Integer> selectedIndicesList;
  80. private boolean forceStacking;
  81. protected static ContextThemeWrapper getTheme(Builder builder) {
  82. TypedArray a = builder.context.getTheme().obtainStyledAttributes(new int[]{R.attr.md_dark_theme});
  83. boolean darkTheme = builder.theme == Theme.DARK;
  84. if (!darkTheme) {
  85. try {
  86. darkTheme = a.getBoolean(0, false);
  87. } finally {
  88. a.recycle();
  89. }
  90. }
  91. return new ContextThemeWrapper(builder.context, darkTheme ? R.style.MD_Dark : R.style.MD_Light);
  92. }
  93. protected MaterialDialog(Builder builder) {
  94. super(getTheme(builder));
  95. this.regularFont = builder.regularFont;
  96. if (this.regularFont == null)
  97. this.regularFont = TypefaceHelper.get(getContext(), "Roboto-Regular");
  98. this.mediumFont = builder.mediumFont;
  99. if (this.mediumFont == null)
  100. this.mediumFont = TypefaceHelper.get(getContext(), "Roboto-Medium");
  101. mContext = builder.context;
  102. this.view = LayoutInflater.from(getContext()).inflate(R.layout.md_dialog, null);
  103. this.customView = builder.customView;
  104. this.callback = builder.callback;
  105. this.listCallback = builder.listCallback;
  106. this.listCallbackSingle = builder.listCallbackSingle;
  107. this.listCallbackMulti = builder.listCallbackMulti;
  108. this.positiveText = builder.positiveText;
  109. this.neutralText = builder.neutralText;
  110. this.negativeText = builder.negativeText;
  111. this.items = builder.items;
  112. this.setCancelable(builder.cancelable);
  113. this.selectedIndex = builder.selectedIndex;
  114. this.selectedIndices = builder.selectedIndices;
  115. this.autoDismiss = builder.autoDismiss;
  116. this.adapter = builder.adapter;
  117. this.forceStacking = builder.forceStacking;
  118. this.positiveColor = builder.positiveColor;
  119. this.negativeColor = builder.negativeColor;
  120. this.neutralColor = builder.neutralColor;
  121. final int mdAccentColor = DialogUtils.resolveColor(mContext, R.attr.md_accent_color);
  122. if (mdAccentColor != 0) {
  123. this.positiveColor = mdAccentColor;
  124. this.negativeColor = mdAccentColor;
  125. this.neutralColor = mdAccentColor;
  126. }
  127. title = (TextView) view.findViewById(R.id.title);
  128. icon = (ImageView) view.findViewById(R.id.icon);
  129. titleFrame = view.findViewById(R.id.titleFrame);
  130. final TextView content = (TextView) view.findViewById(R.id.content);
  131. content.setText(builder.content);
  132. content.setMovementMethod(new LinkMovementMethod());
  133. setTypeface(content, regularFont);
  134. content.setLineSpacing(0f, builder.contentLineSpacingMultiplier);
  135. if (this.positiveColor == 0) {
  136. content.setLinkTextColor(DialogUtils.resolveColor(getContext(), android.R.attr.textColorPrimary));
  137. } else {
  138. content.setLinkTextColor(this.positiveColor);
  139. }
  140. if (builder.contentAlignment == Alignment.CENTER) {
  141. content.setGravity(Gravity.CENTER_HORIZONTAL);
  142. } else if (builder.contentAlignment == Alignment.RIGHT) {
  143. content.setGravity(Gravity.RIGHT);
  144. }
  145. if (builder.contentColor != -1) {
  146. content.setTextColor(builder.contentColor);
  147. } else {
  148. final int fallback = DialogUtils.resolveColor(getContext(), android.R.attr.textColorSecondary);
  149. final int contentColor = DialogUtils.resolveColor(getContext(), R.attr.md_content_color, fallback);
  150. content.setTextColor(contentColor);
  151. }
  152. if (builder.theme == Theme.LIGHT) {
  153. defaultItemColor = Color.BLACK;
  154. } else {
  155. defaultItemColor = Color.WHITE;
  156. }
  157. if (customView != null) {
  158. title = (TextView) view.findViewById(R.id.titleCustomView);
  159. icon = (ImageView) view.findViewById(R.id.iconCustomView);
  160. titleFrame = view.findViewById(R.id.titleFrameCustomView);
  161. invalidateCustomViewAssociations();
  162. ((LinearLayout) view.findViewById(R.id.customViewFrame)).addView(customView);
  163. } else {
  164. invalidateCustomViewAssociations();
  165. }
  166. boolean adapterProvided = adapter != null;
  167. if (items != null && items.length > 0 || adapterProvided) {
  168. title = (TextView) view.findViewById(R.id.titleCustomView);
  169. icon = (ImageView) view.findViewById(R.id.iconCustomView);
  170. titleFrame = view.findViewById(R.id.titleFrameCustomView);
  171. listView = (ListView) view.findViewById(R.id.contentListView);
  172. listView.setSelector(DialogUtils.resolveDrawable(getContext(), R.attr.md_selector));
  173. ((MeasureCallbackListView) listView).setCallback(this);
  174. if (!adapterProvided) {
  175. // Determine list type
  176. if (listCallbackSingle != null) {
  177. listType = ListType.SINGLE;
  178. } else if (listCallbackMulti != null) {
  179. listType = ListType.MULTI;
  180. if (selectedIndices != null) {
  181. selectedIndicesList = new ArrayList<>(Arrays.asList(selectedIndices));
  182. } else {
  183. selectedIndicesList = new ArrayList<>();
  184. }
  185. } else {
  186. listType = ListType.REGULAR;
  187. }
  188. adapter = new MaterialDialogAdapter(mContext, ListType.getLayoutForType(listType), R.id.title, items);
  189. }
  190. adapter.registerDataSetObserver(new DataSetObserver() {
  191. @Override
  192. public void onChanged() {
  193. super.onChanged();
  194. listView.post(new Runnable() {
  195. @Override
  196. public void run() {
  197. invalidateCustomViewAssociations();
  198. }
  199. });
  200. }
  201. });
  202. }
  203. if (builder.icon != null) {
  204. icon.setVisibility(View.VISIBLE);
  205. icon.setImageDrawable(builder.icon);
  206. } else {
  207. Drawable d = DialogUtils.resolveDrawable(mContext, R.attr.md_icon);
  208. if (d != null) {
  209. icon.setVisibility(View.VISIBLE);
  210. icon.setImageDrawable(d);
  211. } else {
  212. icon.setVisibility(View.GONE);
  213. }
  214. }
  215. // Title is set after it's determined whether to use first title or custom view title
  216. if (builder.title == null || builder.title.toString().trim().length() == 0) {
  217. titleFrame.setVisibility(View.GONE);
  218. if (customView == null)
  219. view.findViewById(R.id.titleFrameCustomView).setVisibility(View.GONE);
  220. } else {
  221. title.setText(builder.title);
  222. setTypeface(title, mediumFont);
  223. if (builder.titleColor != -1) {
  224. title.setTextColor(builder.titleColor);
  225. } else {
  226. final int fallback = DialogUtils.resolveColor(getContext(), android.R.attr.textColorPrimary);
  227. title.setTextColor(DialogUtils.resolveColor(getContext(), R.attr.md_title_color, fallback));
  228. }
  229. if (builder.titleAlignment == Alignment.CENTER) {
  230. title.setGravity(Gravity.CENTER_HORIZONTAL);
  231. } else if (builder.titleAlignment == Alignment.RIGHT) {
  232. title.setGravity(Gravity.RIGHT);
  233. }
  234. }
  235. invalidateActions();
  236. setOnShowListenerInternal();
  237. setViewInternal(view);
  238. if (builder.theme == Theme.LIGHT && Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
  239. setInverseBackgroundForced(true);
  240. title.setTextColor(Color.BLACK);
  241. content.setTextColor(Color.BLACK);
  242. }
  243. }
  244. @Override
  245. public void onShow(DialogInterface dialog) {
  246. super.onShow(dialog); // calls any external show listeners
  247. checkIfStackingNeeded();
  248. invalidateCustomViewAssociations();
  249. }
  250. /**
  251. * Invalidates visibility of views for the presence of a custom view or list content
  252. */
  253. private void invalidateCustomViewAssociations() {
  254. if (customView != null || (items != null && items.length > 0) || adapter != null) {
  255. view.findViewById(R.id.mainFrame).setVisibility(View.GONE);
  256. view.findViewById(R.id.customViewScrollParent).setVisibility(View.VISIBLE);
  257. if (!mMeasuredScrollView && listView == null) {
  258. // Wait until it's measured
  259. ((MeasureCallbackScrollView) view.findViewById(R.id.customViewScroll)).setCallback(this);
  260. return;
  261. }
  262. if (canCustomViewScroll()) {
  263. view.findViewById(R.id.customViewDivider).setVisibility(View.VISIBLE);
  264. view.findViewById(R.id.customViewDivider).setBackgroundColor(DialogUtils.resolveColor(getContext(), R.attr.md_divider));
  265. setMargin(view.findViewById(R.id.buttonStackedFrame), -1, 0, -1, -1);
  266. setMargin(view.findViewById(R.id.buttonDefaultFrame), -1, 0, -1, -1);
  267. if (items != null && items.length > 0) {
  268. View customFrame = view.findViewById(R.id.customViewFrame);
  269. Resources r = getContext().getResources();
  270. int bottomPadding = view.findViewById(R.id.titleCustomView).getVisibility() == View.VISIBLE ?
  271. (int) r.getDimension(R.dimen.md_main_frame_margin) : (int) r.getDimension(R.dimen.md_dialog_frame_margin);
  272. customFrame.setPadding(customFrame.getPaddingLeft(), customFrame.getPaddingTop(),
  273. customFrame.getPaddingRight(), bottomPadding);
  274. }
  275. } else {
  276. view.findViewById(R.id.customViewDivider).setVisibility(View.GONE);
  277. final int bottomMargin = (int) getContext().getResources().getDimension(R.dimen.md_button_padding_frame_bottom);
  278. setMargin(view.findViewById(R.id.buttonStackedFrame), -1, bottomMargin, -1, -1);
  279. setMargin(view.findViewById(R.id.buttonDefaultFrame), -1, bottomMargin, -1, -1);
  280. }
  281. } else {
  282. view.findViewById(R.id.mainFrame).setVisibility(View.VISIBLE);
  283. view.findViewById(R.id.customViewScrollParent).setVisibility(View.GONE);
  284. view.findViewById(R.id.customViewDivider).setVisibility(View.GONE);
  285. if (!mMeasuredScrollView) {
  286. // Wait until it's measured
  287. ((MeasureCallbackScrollView) view.findViewById(R.id.contentScrollView)).setCallback(this);
  288. return;
  289. }
  290. if (canContentScroll()) {
  291. view.findViewById(R.id.customViewDivider).setVisibility(View.VISIBLE);
  292. view.findViewById(R.id.customViewDivider).setBackgroundColor(DialogUtils.resolveColor(getContext(), R.attr.md_divider));
  293. setMargin(view.findViewById(R.id.mainFrame), -1, 0, -1, -1);
  294. setMargin(view.findViewById(R.id.buttonStackedFrame), -1, 0, -1, -1);
  295. setMargin(view.findViewById(R.id.buttonDefaultFrame), -1, 0, -1, -1);
  296. final int conPadding = (int) getContext().getResources().getDimension(R.dimen.md_main_frame_margin);
  297. View con = view.findViewById(R.id.content);
  298. con.setPadding(con.getPaddingLeft(), 0, con.getPaddingRight(), conPadding);
  299. } else {
  300. View con = view.findViewById(R.id.content);
  301. con.setPadding(con.getPaddingLeft(), 0, con.getPaddingRight(), 0);
  302. }
  303. }
  304. }
  305. /**
  306. * Invalidates the radio buttons in the single choice mode list so that only the radio button that
  307. * was previous selected is checked.
  308. */
  309. private void invalidateSingleChoice(int newSelection) {
  310. newSelection++;
  311. final LinearLayout list = (LinearLayout) view.findViewById(R.id.customViewFrame);
  312. for (int i = 1; i < list.getChildCount(); i++) {
  313. View v = list.getChildAt(i);
  314. @SuppressLint("WrongViewCast")
  315. RadioButton rb = (RadioButton) v.findViewById(R.id.control);
  316. if (newSelection != i) {
  317. rb.setChecked(false);
  318. rb.clearFocus();
  319. }
  320. }
  321. }
  322. /**
  323. * Constructs the dialog's list content and sets up click listeners.
  324. */
  325. private void invalidateList() {
  326. if ((items == null || items.length == 0) && adapter == null) return;
  327. // Hide content
  328. view.findViewById(R.id.contentScrollView).setVisibility(View.GONE);
  329. // Show custom frame container but hide the scrollview
  330. view.findViewById(R.id.customViewScrollParent).setVisibility(View.VISIBLE);
  331. view.findViewById(R.id.customViewScroll).setVisibility(View.GONE);
  332. // Set up list with adapter
  333. LinearLayout listViewContainer = (LinearLayout) view.findViewById(R.id.list_view_container);
  334. listViewContainer.setVisibility(View.VISIBLE);
  335. listView.setAdapter(adapter);
  336. if (listType != null) {
  337. // Only set listener for 1st-party adapter, leave custom adapter implementation to user with getListView()
  338. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  339. @Override
  340. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  341. if (listType == ListType.MULTI) {
  342. // Keep our selected items up to date
  343. boolean isChecked = !((CheckBox) view.findViewById(R.id.control)).isChecked(); // Inverted because the view's click listener is called before the check is toggled
  344. boolean previouslySelected = selectedIndicesList.contains(position);
  345. if (isChecked) {
  346. if (!previouslySelected) {
  347. selectedIndicesList.add(position);
  348. }
  349. } else if (previouslySelected) {
  350. selectedIndicesList.remove(Integer.valueOf(position));
  351. }
  352. } else if (listType == ListType.SINGLE) {
  353. // Keep our selected item up to date
  354. if (selectedIndex != position) {
  355. selectedIndex = position;
  356. ((MaterialDialogAdapter) adapter).notifyDataSetChanged();
  357. }
  358. }
  359. onClick(view);
  360. }
  361. });
  362. }
  363. final int dialogFramePadding = (int) mContext.getResources().getDimension(R.dimen.md_dialog_frame_margin);
  364. if (titleFrame.getVisibility() == View.VISIBLE || icon.getVisibility() == View.VISIBLE) {
  365. int bottomPadding = (int) getContext().getResources().getDimension(R.dimen.md_title_margin_plainlist);
  366. setMargin(titleFrame, dialogFramePadding, bottomPadding, dialogFramePadding, dialogFramePadding);
  367. ((ViewGroup) titleFrame.getParent()).removeView(titleFrame);
  368. listViewContainer.addView(titleFrame, 0);
  369. } else {
  370. listView.setPadding(listView.getPaddingLeft(), 0,
  371. listView.getPaddingRight(), listView.getPaddingBottom());
  372. }
  373. }
  374. private int calculateMaxButtonWidth() {
  375. /**
  376. * Max button width = (DialogWidth - Side margins) / [Number of buttons]
  377. * From: http://www.google.com/design/spec/components/dialogs.html#dialogs-specs
  378. */
  379. final int dialogWidth = getWindow().getDecorView().getMeasuredWidth();
  380. final int margins = (int) getContext().getResources().getDimension(R.dimen.md_button_padding_frame_side);
  381. return (dialogWidth - 2 * margins) / numberOfActionButtons();
  382. }
  383. /**
  384. * Detects whether or not the custom view or list content can be scrolled.
  385. */
  386. private boolean canCustomViewScroll() {
  387. if (listView != null) {
  388. return listView.getLastVisiblePosition() != -1 && listView.getLastVisiblePosition() < (listView.getCount() - 1);
  389. }
  390. final ScrollView scrollView = (ScrollView) view.findViewById(R.id.customViewScroll);
  391. final int childHeight = view.findViewById(R.id.customViewFrame).getMeasuredHeight();
  392. return scrollView.getMeasuredHeight() < childHeight;
  393. }
  394. /**
  395. * Detects whether or not the content TextView can be scrolled.
  396. */
  397. private boolean canContentScroll() {
  398. final ScrollView scrollView = (ScrollView) view.findViewById(R.id.contentScrollView);
  399. final int childHeight = view.findViewById(R.id.content).getMeasuredHeight();
  400. return scrollView.getMeasuredHeight() < childHeight;
  401. }
  402. /**
  403. * Measures the action button's and their text to decide whether or not the button should be stacked.
  404. */
  405. private void checkIfStackingNeeded() {
  406. if (numberOfActionButtons() <= 1) {
  407. return;
  408. } else if (forceStacking) {
  409. isStacked = true;
  410. invalidateActions();
  411. return;
  412. }
  413. final int maxWidth = calculateMaxButtonWidth();
  414. isStacked = false;
  415. if (this.positiveText != null) {
  416. final int positiveWidth = positiveButton.getWidth();
  417. isStacked = positiveWidth > maxWidth;
  418. }
  419. if (!isStacked && this.neutralText != null) {
  420. final int neutralWidth = neutralButton.getWidth();
  421. isStacked = neutralWidth > maxWidth;
  422. }
  423. if (!isStacked && this.negativeText != null) {
  424. final int negativeWidth = negativeButton.getWidth();
  425. isStacked = negativeWidth > maxWidth;
  426. }
  427. invalidateActions();
  428. }
  429. /**
  430. * Invalidates the positive/neutral/negative action buttons. Decides whether they should be visible
  431. * and sets their properties (such as height, text color, etc.).
  432. */
  433. private boolean invalidateActions() {
  434. if (!hasActionButtons()) {
  435. // If the dialog is a plain list dialog, no buttons are shown.
  436. view.findViewById(R.id.buttonDefaultFrame).setVisibility(View.GONE);
  437. view.findViewById(R.id.buttonStackedFrame).setVisibility(View.GONE);
  438. invalidateList();
  439. return false;
  440. }
  441. if (isStacked) {
  442. view.findViewById(R.id.buttonDefaultFrame).setVisibility(View.GONE);
  443. view.findViewById(R.id.buttonStackedFrame).setVisibility(View.VISIBLE);
  444. } else {
  445. view.findViewById(R.id.buttonDefaultFrame).setVisibility(View.VISIBLE);
  446. view.findViewById(R.id.buttonStackedFrame).setVisibility(View.GONE);
  447. }
  448. positiveButton = (TextView) view.findViewById(
  449. isStacked ? R.id.buttonStackedPositive : R.id.buttonDefaultPositive);
  450. if (this.positiveText != null) {
  451. setTypeface(positiveButton, mediumFont);
  452. positiveButton.setText(this.positiveText);
  453. positiveButton.setTextColor(getActionTextStateList(this.positiveColor));
  454. setBackgroundCompat(positiveButton, DialogUtils.resolveDrawable(getContext(), isStacked ? R.attr.md_selector : R.attr.md_btn_selector));
  455. positiveButton.setTag(POSITIVE);
  456. positiveButton.setOnClickListener(this);
  457. } else {
  458. positiveButton.setVisibility(View.GONE);
  459. }
  460. neutralButton = (TextView) view.findViewById(
  461. isStacked ? R.id.buttonStackedNeutral : R.id.buttonDefaultNeutral);
  462. if (this.neutralText != null) {
  463. setTypeface(neutralButton, mediumFont);
  464. neutralButton.setVisibility(View.VISIBLE);
  465. neutralButton.setTextColor(getActionTextStateList(this.neutralColor));
  466. setBackgroundCompat(neutralButton, DialogUtils.resolveDrawable(getContext(), isStacked ? R.attr.md_selector : R.attr.md_btn_selector));
  467. neutralButton.setText(this.neutralText);
  468. neutralButton.setTag(NEUTRAL);
  469. neutralButton.setOnClickListener(this);
  470. } else {
  471. neutralButton.setVisibility(View.GONE);
  472. }
  473. negativeButton = (TextView) view.findViewById(
  474. isStacked ? R.id.buttonStackedNegative : R.id.buttonDefaultNegative);
  475. if (this.negativeText != null) {
  476. setTypeface(negativeButton, mediumFont);
  477. negativeButton.setVisibility(View.VISIBLE);
  478. negativeButton.setTextColor(getActionTextStateList(this.negativeColor));
  479. setBackgroundCompat(negativeButton, DialogUtils.resolveDrawable(getContext(), isStacked ? R.attr.md_selector : R.attr.md_btn_selector));
  480. negativeButton.setText(this.negativeText);
  481. negativeButton.setTag(NEGATIVE);
  482. negativeButton.setOnClickListener(this);
  483. if (!isStacked) {
  484. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
  485. RelativeLayout.LayoutParams.WRAP_CONTENT,
  486. (int) getContext().getResources().getDimension(R.dimen.md_button_height));
  487. if (this.positiveText != null) {
  488. params.addRule(RelativeLayout.LEFT_OF, R.id.buttonDefaultPositive);
  489. } else {
  490. params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
  491. }
  492. negativeButton.setLayoutParams(params);
  493. }
  494. } else {
  495. negativeButton.setVisibility(View.GONE);
  496. }
  497. invalidateList();
  498. return true;
  499. }
  500. private void sendSingleChoiceCallback(View v) {
  501. CharSequence text = null;
  502. if (selectedIndex >= 0) {
  503. text = items[selectedIndex];
  504. }
  505. listCallbackSingle.onSelection(this, v, selectedIndex, text);
  506. }
  507. private void sendMultichoiceCallback() {
  508. List<CharSequence> selectedTitles = new ArrayList<CharSequence>();
  509. for (Integer i : selectedIndicesList) {
  510. selectedTitles.add(items[i]);
  511. }
  512. listCallbackMulti.onSelection(this,
  513. selectedIndicesList.toArray(new Integer[selectedIndicesList.size()]),
  514. selectedTitles.toArray(new CharSequence[selectedTitles.size()]));
  515. }
  516. @Override
  517. public final void onClick(View v) {
  518. String tag = (String) v.getTag();
  519. switch (tag) {
  520. case POSITIVE: {
  521. if (callback != null)
  522. callback.onPositive(this);
  523. if (listCallbackSingle != null)
  524. sendSingleChoiceCallback(v);
  525. if (listCallbackMulti != null)
  526. sendMultichoiceCallback();
  527. if (autoDismiss) dismiss();
  528. break;
  529. }
  530. case NEGATIVE: {
  531. if (callback != null)
  532. callback.onNegative(this);
  533. if (autoDismiss) dismiss();
  534. break;
  535. }
  536. case NEUTRAL: {
  537. if (callback != null)
  538. callback.onNeutral(this);
  539. if (autoDismiss) dismiss();
  540. break;
  541. }
  542. default: {
  543. String[] split = tag.split(":");
  544. int index = Integer.parseInt(split[0]);
  545. if (listCallback != null) {
  546. if (autoDismiss) {
  547. dismiss();
  548. listCallback.onSelection(this, v, index, split[1]);
  549. }
  550. } else if (listCallbackSingle != null) {
  551. RadioButton cb = (RadioButton) ((LinearLayout) v).getChildAt(0);
  552. if (!cb.isChecked())
  553. cb.setChecked(true);
  554. invalidateSingleChoice(index);
  555. if (autoDismiss && positiveText == null) {
  556. dismiss();
  557. sendSingleChoiceCallback(v);
  558. }
  559. } else if (listCallbackMulti != null) {
  560. CheckBox cb = (CheckBox) ((LinearLayout) v).getChildAt(0);
  561. cb.setChecked(!cb.isChecked());
  562. } else if (autoDismiss) dismiss();
  563. break;
  564. }
  565. }
  566. }
  567. @Override
  568. public void onMeasureScroll(ScrollView view) {
  569. if (view.getMeasuredWidth() > 0) {
  570. mMeasuredScrollView = true;
  571. invalidateCustomViewAssociations();
  572. }
  573. }
  574. @Override
  575. public void onMeasureList(ListView view) {
  576. invalidateCustomViewAssociations();
  577. }
  578. /**
  579. * The class used to construct a MaterialDialog.
  580. */
  581. public static class Builder {
  582. protected Context context;
  583. protected CharSequence title;
  584. protected Alignment titleAlignment = Alignment.LEFT;
  585. protected Alignment contentAlignment = Alignment.LEFT;
  586. protected int titleColor = -1;
  587. protected int contentColor = -1;
  588. protected CharSequence content;
  589. protected CharSequence[] items;
  590. protected CharSequence positiveText;
  591. protected CharSequence neutralText;
  592. protected CharSequence negativeText;
  593. protected View customView;
  594. protected int positiveColor;
  595. protected int negativeColor;
  596. protected int neutralColor;
  597. protected ButtonCallback callback;
  598. protected ListCallback listCallback;
  599. protected ListCallback listCallbackSingle;
  600. protected ListCallbackMulti listCallbackMulti;
  601. protected Theme theme = Theme.LIGHT;
  602. protected boolean cancelable = true;
  603. protected float contentLineSpacingMultiplier = 1.3f;
  604. protected int selectedIndex = -1;
  605. protected Integer[] selectedIndices = null;
  606. protected boolean autoDismiss = true;
  607. protected Typeface regularFont;
  608. protected Typeface mediumFont;
  609. protected Drawable icon;
  610. protected ListAdapter adapter;
  611. private OnDismissListener dismissListener;
  612. private OnCancelListener cancelListener;
  613. private OnShowListener showListener;
  614. protected boolean forceStacking;
  615. public Builder(@NonNull Context context) {
  616. this.context = context;
  617. final int materialBlue = context.getResources().getColor(R.color.md_material_blue_500);
  618. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  619. TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{android.R.attr.colorAccent});
  620. try {
  621. this.positiveColor = a.getColor(0, materialBlue);
  622. this.negativeColor = a.getColor(0, materialBlue);
  623. this.neutralColor = a.getColor(0, materialBlue);
  624. } catch (Exception e) {
  625. this.positiveColor = materialBlue;
  626. this.negativeColor = materialBlue;
  627. this.neutralColor = materialBlue;
  628. } finally {
  629. a.recycle();
  630. }
  631. } else {
  632. TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{R.attr.colorAccent});
  633. try {
  634. this.positiveColor = a.getColor(0, materialBlue);
  635. this.negativeColor = a.getColor(0, materialBlue);
  636. this.neutralColor = a.getColor(0, materialBlue);
  637. } catch (Exception e) {
  638. this.positiveColor = materialBlue;
  639. this.negativeColor = materialBlue;
  640. this.neutralColor = materialBlue;
  641. } finally {
  642. a.recycle();
  643. }
  644. }
  645. }
  646. public Builder title(@StringRes int titleRes) {
  647. title(this.context.getString(titleRes));
  648. return this;
  649. }
  650. public Builder title(CharSequence title) {
  651. this.title = title;
  652. return this;
  653. }
  654. public Builder titleAlignment(Alignment align) {
  655. this.titleAlignment = align;
  656. return this;
  657. }
  658. public Builder titleColorRes(@ColorRes int colorRes) {
  659. titleColor(this.context.getResources().getColor(colorRes));
  660. return this;
  661. }
  662. /**
  663. * Sets the fonts used in the dialog.
  664. *
  665. * @param medium The font used on titles and action buttons. Null uses the default.
  666. * @param regular The font used everywhere else, like on the content and list items. Null uses the default.
  667. * @return The Builder instance so you can chain calls to it.
  668. */
  669. public Builder typeface(Typeface medium, Typeface regular) {
  670. this.mediumFont = medium;
  671. this.regularFont = regular;
  672. return this;
  673. }
  674. public Builder titleColor(int color) {
  675. this.titleColor = color;
  676. return this;
  677. }
  678. public Builder icon(Drawable icon) {
  679. this.icon = icon;
  680. return this;
  681. }
  682. public Builder icon(@DrawableRes int icon) {
  683. this.icon = context.getResources().getDrawable(icon);
  684. return this;
  685. }
  686. public Builder iconAttr(int iconAttr) {
  687. this.icon = DialogUtils.resolveDrawable(context, iconAttr);
  688. return this;
  689. }
  690. public Builder contentColor(int color) {
  691. this.contentColor = color;
  692. return this;
  693. }
  694. public Builder contentColorRes(@ColorRes int colorRes) {
  695. contentColor(this.context.getResources().getColor(colorRes));
  696. return this;
  697. }
  698. public Builder content(@StringRes int contentRes) {
  699. content(this.context.getString(contentRes));
  700. return this;
  701. }
  702. public Builder content(CharSequence content) {
  703. this.content = content;
  704. return this;
  705. }
  706. public Builder content(@StringRes int contentRes, Object... formatArgs) {
  707. content(this.context.getString(contentRes, formatArgs));
  708. return this;
  709. }
  710. public Builder contentAlignment(Alignment align) {
  711. this.contentAlignment = align;
  712. return this;
  713. }
  714. public Builder contentLineSpacing(float multiplier) {
  715. this.contentLineSpacingMultiplier = multiplier;
  716. return this;
  717. }
  718. public Builder items(@ArrayRes int itemsRes) {
  719. items(this.context.getResources().getStringArray(itemsRes));
  720. return this;
  721. }
  722. public Builder items(CharSequence[] items) {
  723. this.items = items;
  724. return this;
  725. }
  726. public Builder itemsCallback(ListCallback callback) {
  727. this.listCallback = callback;
  728. this.listCallbackSingle = null;
  729. this.listCallbackMulti = null;
  730. return this;
  731. }
  732. /**
  733. * Pass anything below 0 (such as -1) for the selected index to leave all options unselected initially.
  734. * Otherwise pass the index of an item that will be selected initially.
  735. *
  736. * @param selectedIndex The checkbox index that will be selected initially.
  737. * @param callback The callback that will be called when the presses the positive button.
  738. * @return The Builder instance so you can chain calls to it.
  739. */
  740. public Builder itemsCallbackSingleChoice(int selectedIndex, ListCallback callback) {
  741. this.selectedIndex = selectedIndex;
  742. this.listCallback = null;
  743. this.listCallbackSingle = callback;
  744. this.listCallbackMulti = null;
  745. return this;
  746. }
  747. /**
  748. * Pass null for the selected indices to leave all options unselected initially. Otherwise pass
  749. * an array of indices that will be selected initially.
  750. *
  751. * @param selectedIndices The radio button indices that will be selected initially.
  752. * @param callback The callback that will be called when the presses the positive button.
  753. * @return The Builder instance so you can chain calls to it.
  754. */
  755. public Builder itemsCallbackMultiChoice(Integer[] selectedIndices, ListCallbackMulti callback) {
  756. this.selectedIndices = selectedIndices;
  757. this.listCallback = null;
  758. this.listCallbackSingle = null;
  759. this.listCallbackMulti = callback;
  760. return this;
  761. }
  762. public Builder positiveText(@StringRes int postiveRes) {
  763. positiveText(this.context.getString(postiveRes));
  764. return this;
  765. }
  766. public Builder positiveText(CharSequence message) {
  767. this.positiveText = message;
  768. return this;
  769. }
  770. public Builder neutralText(@StringRes int neutralRes) {
  771. neutralText(this.context.getString(neutralRes));
  772. return this;
  773. }
  774. public Builder neutralText(CharSequence message) {
  775. this.neutralText = message;
  776. return this;
  777. }
  778. public Builder negativeText(@StringRes int negativeRes) {
  779. negativeText(this.context.getString(negativeRes));
  780. return this;
  781. }
  782. public Builder negativeText(CharSequence message) {
  783. this.negativeText = message;
  784. return this;
  785. }
  786. public Builder customView(@LayoutRes int layoutRes) {
  787. LayoutInflater li = LayoutInflater.from(this.context);
  788. customView(li.inflate(layoutRes, null));
  789. return this;
  790. }
  791. public Builder customView(View view) {
  792. this.customView = view;
  793. return this;
  794. }
  795. public Builder positiveColorRes(@ColorRes int colorRes) {
  796. positiveColor(this.context.getResources().getColor(colorRes));
  797. return this;
  798. }
  799. public Builder positiveColor(int color) {
  800. this.positiveColor = color;
  801. return this;
  802. }
  803. public Builder negativeColorRes(@ColorRes int colorRes) {
  804. negativeColor(this.context.getResources().getColor(colorRes));
  805. return this;
  806. }
  807. public Builder negativeColor(int color) {
  808. this.negativeColor = color;
  809. return this;
  810. }
  811. public Builder neutralColorRes(@ColorRes int colorRes) {
  812. neutralColor(this.context.getResources().getColor(colorRes));
  813. return this;
  814. }
  815. public Builder neutralColor(int color) {
  816. this.neutralColor = color;
  817. return this;
  818. }
  819. public Builder callback(ButtonCallback callback) {
  820. this.callback = callback;
  821. return this;
  822. }
  823. public Builder theme(Theme theme) {
  824. this.theme = theme;
  825. return this;
  826. }
  827. public Builder cancelable(boolean cancelable) {
  828. this.cancelable = cancelable;
  829. return this;
  830. }
  831. /**
  832. * This defaults to true. If set to false, the dialog will not automatically be dismissed
  833. * when an action button is pressed, and not automatically dismissed when the user selects
  834. * a list item.
  835. *
  836. * @param dismiss Whether or not to dismiss the dialog automatically.
  837. * @return The Builder instance so you can chain calls to it.
  838. */
  839. public Builder autoDismiss(boolean dismiss) {
  840. this.autoDismiss = dismiss;
  841. return this;
  842. }
  843. /**
  844. * Sets a custom {@link android.widget.ListAdapter} for the dialog's list
  845. *
  846. * @return This Builder object to allow for chaining of calls to set methods
  847. */
  848. public Builder adapter(ListAdapter adapter) {
  849. this.adapter = adapter;
  850. return this;
  851. }
  852. public Builder showListener(OnShowListener listener) {
  853. this.showListener = listener;
  854. return this;
  855. }
  856. public Builder dismissListener(OnDismissListener listener) {
  857. this.dismissListener = listener;
  858. return this;
  859. }
  860. public Builder cancelListener(OnCancelListener listener) {
  861. this.cancelListener = listener;
  862. return this;
  863. }
  864. public Builder forceStacking(boolean stacked) {
  865. this.forceStacking = stacked;
  866. return this;
  867. }
  868. public MaterialDialog build() {
  869. MaterialDialog dialog = new MaterialDialog(this);
  870. if (this.showListener != null) {
  871. dialog.setOnShowListener(this.showListener);
  872. }
  873. if (this.cancelListener != null) {
  874. dialog.setOnCancelListener(this.cancelListener);
  875. }
  876. if (this.dismissListener != null) {
  877. dialog.setOnDismissListener(this.dismissListener);
  878. }
  879. return dialog;
  880. }
  881. public MaterialDialog show() {
  882. MaterialDialog dialog = build();
  883. dialog.show();
  884. return dialog;
  885. }
  886. }
  887. private ColorStateList getActionTextStateList(int newPrimaryColor) {
  888. final int fallBackButtonColor = DialogUtils.resolveColor(getContext(), android.R.attr.textColorPrimary);
  889. if (newPrimaryColor == 0) newPrimaryColor = fallBackButtonColor;
  890. int[][] states = new int[][]{
  891. new int[]{-android.R.attr.state_enabled}, // disabled
  892. new int[]{} // enabled
  893. };
  894. int[] colors = new int[]{
  895. DialogUtils.adjustAlpha(newPrimaryColor, 0.4f),
  896. newPrimaryColor
  897. };
  898. return new ColorStateList(states, colors);
  899. }
  900. /**
  901. * Retrieves the view of an action button, allowing you to modify properties such as whether or not it's enabled.
  902. *
  903. * @param which The action button of which to get the view for.
  904. * @return The view from the dialog's layout representing this action button.
  905. */
  906. public final Button getActionButton(DialogAction which) {
  907. if (view == null) return null;
  908. if (isStacked) {
  909. switch (which) {
  910. default:
  911. return (Button) view.findViewById(R.id.buttonStackedPositive);
  912. case NEUTRAL:
  913. return (Button) view.findViewById(R.id.buttonStackedNeutral);
  914. case NEGATIVE:
  915. return (Button) view.findViewById(R.id.buttonStackedNegative);
  916. }
  917. } else {
  918. switch (which) {
  919. default:
  920. return (Button) view.findViewById(R.id.buttonDefaultPositive);
  921. case NEUTRAL:
  922. return (Button) view.findViewById(R.id.buttonDefaultNeutral);
  923. case NEGATIVE:
  924. return (Button) view.findViewById(R.id.buttonDefaultNegative);
  925. }
  926. }
  927. }
  928. /**
  929. * @deprecated Use getActionButton(com.afollestad.materialdialogs.DialogAction)} instead.
  930. */
  931. @Override
  932. public Button getButton(int whichButton) {
  933. switch (whichButton) {
  934. case BUTTON_POSITIVE:
  935. return getActionButton(DialogAction.POSITIVE);
  936. case BUTTON_NEUTRAL:
  937. return getActionButton(DialogAction.NEUTRAL);
  938. case BUTTON_NEGATIVE:
  939. return getActionButton(DialogAction.NEGATIVE);
  940. default:
  941. return null;
  942. }
  943. }
  944. /**
  945. * Retrieves the frame view containing the title and icon. You can manually change visibility and retrieve children.
  946. */
  947. public final View getTitleFrame() {
  948. return titleFrame;
  949. }
  950. /**
  951. * Retrieves the custom view that was inflated or set to the MaterialDialog during building.
  952. *
  953. * @return The custom view that was passed into the Builder.
  954. */
  955. public final View getCustomView() {
  956. return customView;
  957. }
  958. /**
  959. * Updates an action button's title, causing invalidation to check if the action buttons should be stacked.
  960. *
  961. * @param which The action button to update.
  962. * @param title The new title of the action button.
  963. */
  964. public final void setActionButton(DialogAction which, CharSequence title) {
  965. switch (which) {
  966. default:
  967. this.positiveText = title;
  968. break;
  969. case NEUTRAL:
  970. this.neutralText = title;
  971. break;
  972. case NEGATIVE:
  973. this.negativeText = title;
  974. break;
  975. }
  976. invalidateActions();
  977. }
  978. /**
  979. * Updates an action button's title, causing invalidation to check if the action buttons should be stacked.
  980. *
  981. * @param which The action button to update.
  982. * @param titleRes The string resource of the new title of the action button.
  983. */
  984. public final void setActionButton(DialogAction which, @StringRes int titleRes) {
  985. setActionButton(which, getContext().getString(titleRes));
  986. }
  987. /**
  988. * Gets whether or not the positive, neutral, or negative action button is visible.
  989. *
  990. * @return Whether or not 1 or more action buttons is visible.
  991. */
  992. public final boolean hasActionButtons() {
  993. return numberOfActionButtons() > 0;
  994. }
  995. /**
  996. * Gets the number of visible action buttons.
  997. *
  998. * @return 0 through 3, depending on how many should be or are visible.
  999. */
  1000. public final int numberOfActionButtons() {
  1001. int number = 0;
  1002. if (positiveText != null) number++;
  1003. if (neutralText != null) number++;
  1004. if (negativeText != null) number++;
  1005. return number;
  1006. }
  1007. /**
  1008. * Updates the dialog's title.
  1009. */
  1010. public final void setTitle(CharSequence title) {
  1011. this.title.setText(title);
  1012. }
  1013. @Override
  1014. public void setIcon(int resId) {
  1015. icon.setImageResource(resId);
  1016. icon.setVisibility(resId != 0 ? View.VISIBLE : View.GONE);
  1017. }
  1018. @Override
  1019. public void setIcon(Drawable d) {
  1020. icon.setImageDrawable(d);
  1021. icon.setVisibility(d != null ? View.VISIBLE : View.GONE);
  1022. }
  1023. @Override
  1024. public void setIconAttribute(int attrId) {
  1025. Drawable d = DialogUtils.resolveDrawable(mContext, attrId);
  1026. icon.setImageDrawable(d);
  1027. icon.setVisibility(d != null ? View.VISIBLE : View.GONE);
  1028. }
  1029. public final void setContent(CharSequence content) {
  1030. ((TextView) view.findViewById(R.id.content)).setText(content);
  1031. }
  1032. public final void setItems(CharSequence[] items) {
  1033. if (adapter == null)
  1034. throw new IllegalStateException("This MaterialDialog instance does not yet have an adapter set to it. You cannot use setItems().");
  1035. if (adapter instanceof MaterialDialogAdapter) {
  1036. adapter = new MaterialDialogAdapter(mContext, ListType.getLayoutForType(listType), R.id.title, items);
  1037. } else {
  1038. throw new IllegalStateException("When using a custom adapter, setItems() cannot be used. Set items through the adapter instead.");
  1039. }
  1040. this.items = items;
  1041. listView.setAdapter(adapter);
  1042. invalidateCustomViewAssociations();
  1043. }
  1044. /**
  1045. * Use this to customize any list-specific logic for this dialog (OnItemClickListener, OnLongItemClickListener, etc.)
  1046. *
  1047. * @return The ListView instance used by this dialog, or null if not using a list.
  1048. */
  1049. @Nullable
  1050. public ListView getListView() {
  1051. return listView;
  1052. }
  1053. /**
  1054. * Convenience method for getting the currently selected index of a single choice list
  1055. *
  1056. * @return Currently selected index of a single choice list, or -1 if not showing a single choice list
  1057. */
  1058. public int getSelectedIndex() {
  1059. if (listCallbackSingle != null) {
  1060. return selectedIndex;
  1061. } else {
  1062. return -1;
  1063. }
  1064. }
  1065. /**
  1066. * Convenience method for getting the currently selected indices of a multi choice list
  1067. *
  1068. * @return Currently selected index of a multi choice list, or null if not showing a multi choice list
  1069. */
  1070. @Nullable
  1071. public Integer[] getSelectedIndices() {
  1072. if (listCallbackMulti != null) {
  1073. return selectedIndicesList.toArray(new Integer[selectedIndicesList.size()]);
  1074. } else {
  1075. return null;
  1076. }
  1077. }
  1078. private class MaterialDialogAdapter extends ArrayAdapter<CharSequence> {
  1079. final int itemColor;
  1080. public MaterialDialogAdapter(Context context, int resource, int textViewResourceId, CharSequence[] objects) {
  1081. super(context, resource, textViewResourceId, objects);
  1082. itemColor = DialogUtils.resolveColor(getContext(), R.attr.md_item_color, defaultItemColor);
  1083. }
  1084. @Override
  1085. public boolean hasStableIds() {
  1086. return true;
  1087. }
  1088. @Override
  1089. public long getItemId(int position) {
  1090. return position;
  1091. }
  1092. @SuppressLint("WrongViewCast")
  1093. @Override
  1094. public View getView(final int index, View convertView, ViewGroup parent) {
  1095. final View view = super.getView(index, convertView, parent);
  1096. TextView tv = (TextView) view.findViewById(R.id.title);
  1097. switch (listType) {
  1098. case SINGLE:
  1099. RadioButton radio = (RadioButton) view.findViewById(R.id.control);
  1100. radio.setChecked(selectedIndex == index);
  1101. break;
  1102. case MULTI:
  1103. if (selectedIndices != null) {
  1104. CheckBox checkbox = (CheckBox) view.findViewById(R.id.control);
  1105. checkbox.setChecked(selectedIndicesList.contains(index));
  1106. }
  1107. break;
  1108. }
  1109. tv.setText(items[index]);
  1110. tv.setTextColor(itemColor);
  1111. setTypeface(tv, regularFont);
  1112. view.setTag(index + ":" + items[index]);
  1113. return view;
  1114. }
  1115. }
  1116. private static enum ListType {
  1117. REGULAR, SINGLE, MULTI;
  1118. public static int getLayoutForType(ListType type) {
  1119. switch (type) {
  1120. case REGULAR:
  1121. return R.layout.md_listitem;
  1122. case SINGLE:
  1123. return R.layout.md_listitem_singlechoice;
  1124. case MULTI:
  1125. return R.layout.md_listitem_multichoice;
  1126. default:
  1127. // Shouldn't be possible
  1128. throw new IllegalArgumentException("Not a valid list type");
  1129. }
  1130. }
  1131. }
  1132. public static interface ListCallback {
  1133. void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text);
  1134. }
  1135. public static interface ListCallbackMulti {
  1136. void onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text);
  1137. }
  1138. /**
  1139. * @deprecated Use the new {@link com.afollestad.materialdialogs.MaterialDialog.ButtonCallback}
  1140. */
  1141. public abstract static class SimpleCallback extends ButtonCallback {
  1142. @Override
  1143. public abstract void onPositive(MaterialDialog dialog);
  1144. }
  1145. /**
  1146. * @deprecated Use the new {@link com.afollestad.materialdialogs.MaterialDialog.ButtonCallback}
  1147. */
  1148. public abstract static class Callback extends SimpleCallback {
  1149. @Override
  1150. public abstract void onNegative(MaterialDialog dialog);
  1151. }
  1152. /**
  1153. * @deprecated Use the new {@link com.afollestad.materialdialogs.MaterialDialog.ButtonCallback}
  1154. */
  1155. public abstract static class FullCallback extends Callback {
  1156. @Override
  1157. public abstract void onNeutral(MaterialDialog dialog);
  1158. }
  1159. /**
  1160. * Override these as needed, so no needing to sub empty methods from an interface
  1161. */
  1162. public static abstract class ButtonCallback {
  1163. public void onPositive(MaterialDialog dialog) {
  1164. }
  1165. public void onNegative(MaterialDialog dialog) {
  1166. }
  1167. public void onNeutral(MaterialDialog dialog) {
  1168. }
  1169. public ButtonCallback() {
  1170. super();
  1171. }
  1172. @Override
  1173. protected final Object clone() throws CloneNotSupportedException {
  1174. return super.clone();
  1175. }
  1176. @Override
  1177. public final boolean equals(Object o) {
  1178. return super.equals(o);
  1179. }
  1180. @Override
  1181. protected final void finalize() throws Throwable {
  1182. super.finalize();
  1183. }
  1184. @Override
  1185. public final int hashCode() {
  1186. return super.hashCode();
  1187. }
  1188. @Override
  1189. public final String toString() {
  1190. return super.toString();
  1191. }
  1192. }
  1193. }