MaterialDialog.java 55 KB

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