MaterialDialog.java 54 KB

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