MainActivity.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. package com.afollestad.materialdialogssample;
  2. import android.content.DialogInterface;
  3. import android.content.Intent;
  4. import android.graphics.Color;
  5. import android.graphics.drawable.ColorDrawable;
  6. import android.os.Build;
  7. import android.os.Bundle;
  8. import android.support.annotation.ColorInt;
  9. import android.support.annotation.NonNull;
  10. import android.support.annotation.StringRes;
  11. import android.support.v4.content.ContextCompat;
  12. import android.support.v7.app.AppCompatActivity;
  13. import android.text.Editable;
  14. import android.text.Html;
  15. import android.text.InputType;
  16. import android.text.TextWatcher;
  17. import android.text.method.PasswordTransformationMethod;
  18. import android.view.Menu;
  19. import android.view.MenuItem;
  20. import android.view.View;
  21. import android.widget.CheckBox;
  22. import android.widget.CompoundButton;
  23. import android.widget.EditText;
  24. import android.widget.Toast;
  25. import com.afollestad.materialdialogs.DialogAction;
  26. import com.afollestad.materialdialogs.GravityEnum;
  27. import com.afollestad.materialdialogs.MaterialDialog;
  28. import com.afollestad.materialdialogs.Theme;
  29. import com.afollestad.materialdialogs.internal.ThemeSingleton;
  30. import com.afollestad.materialdialogs.color.CircleView;
  31. import com.afollestad.materialdialogs.color.ColorChooserDialog;
  32. import com.afollestad.materialdialogs.simplelist.MaterialSimpleListAdapter;
  33. import com.afollestad.materialdialogs.simplelist.MaterialSimpleListItem;
  34. import com.afollestad.materialdialogs.internal.MDTintHelper;
  35. import com.afollestad.materialdialogs.util.DialogUtils;
  36. import java.io.File;
  37. /**
  38. * @author Aidan Follestad (afollestad)
  39. */
  40. public class MainActivity extends AppCompatActivity implements
  41. FolderSelectorDialog.FolderSelectCallback, ColorChooserDialog.ColorCallback {
  42. private Toast mToast;
  43. private Thread mThread;
  44. private void showToast(String message) {
  45. if (mToast != null) {
  46. mToast.cancel();
  47. mToast = null;
  48. }
  49. mToast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
  50. mToast.show();
  51. }
  52. private void startThread(Runnable run) {
  53. if (mThread != null)
  54. mThread.interrupt();
  55. mThread = new Thread(run);
  56. mThread.start();
  57. }
  58. private void showToast(@StringRes int message) {
  59. showToast(getString(message));
  60. }
  61. @Override
  62. protected void onCreate(Bundle savedInstanceState) {
  63. super.onCreate(savedInstanceState);
  64. setContentView(R.layout.activity_main);
  65. findViewById(R.id.basicNoTitle).setOnClickListener(new View.OnClickListener() {
  66. @Override
  67. public void onClick(View v) {
  68. showBasicNoTitle();
  69. }
  70. });
  71. findViewById(R.id.basic).setOnClickListener(new View.OnClickListener() {
  72. @Override
  73. public void onClick(View v) {
  74. showBasic();
  75. }
  76. });
  77. findViewById(R.id.basicLongContent).setOnClickListener(new View.OnClickListener() {
  78. @Override
  79. public void onClick(View v) {
  80. showBasicLongContent();
  81. }
  82. });
  83. findViewById(R.id.basicIcon).setOnClickListener(new View.OnClickListener() {
  84. @Override
  85. public void onClick(View v) {
  86. showBasicIcon();
  87. }
  88. });
  89. findViewById(R.id.stacked).setOnClickListener(new View.OnClickListener() {
  90. @Override
  91. public void onClick(View v) {
  92. showStacked();
  93. }
  94. });
  95. findViewById(R.id.neutral).setOnClickListener(new View.OnClickListener() {
  96. @Override
  97. public void onClick(View v) {
  98. showNeutral();
  99. }
  100. });
  101. findViewById(R.id.callbacks).setOnClickListener(new View.OnClickListener() {
  102. @Override
  103. public void onClick(View v) {
  104. showCallbacks();
  105. }
  106. });
  107. findViewById(R.id.list).setOnClickListener(new View.OnClickListener() {
  108. @Override
  109. public void onClick(View v) {
  110. showList();
  111. }
  112. });
  113. findViewById(R.id.listNoTitle).setOnClickListener(new View.OnClickListener() {
  114. @Override
  115. public void onClick(View v) {
  116. showListNoTitle();
  117. }
  118. });
  119. findViewById(R.id.longList).setOnClickListener(new View.OnClickListener() {
  120. @Override
  121. public void onClick(View v) {
  122. showLongList();
  123. }
  124. });
  125. findViewById(R.id.singleChoice).setOnClickListener(new View.OnClickListener() {
  126. @Override
  127. public void onClick(View v) {
  128. showSingleChoice();
  129. }
  130. });
  131. findViewById(R.id.multiChoice).setOnClickListener(new View.OnClickListener() {
  132. @Override
  133. public void onClick(View v) {
  134. showMultiChoice();
  135. }
  136. });
  137. findViewById(R.id.multiChoiceLimited).setOnClickListener(new View.OnClickListener() {
  138. @Override
  139. public void onClick(View v) {
  140. showMultiChoiceLimited();
  141. }
  142. });
  143. findViewById(R.id.simpleList).setOnClickListener(new View.OnClickListener() {
  144. @Override
  145. public void onClick(View v) {
  146. showSimpleList();
  147. }
  148. });
  149. findViewById(R.id.customListItems).setOnClickListener(new View.OnClickListener() {
  150. @Override
  151. public void onClick(View v) {
  152. showCustomList();
  153. }
  154. });
  155. findViewById(R.id.customView).setOnClickListener(new View.OnClickListener() {
  156. @Override
  157. public void onClick(View v) {
  158. showCustomView();
  159. }
  160. });
  161. findViewById(R.id.customView_webView).setOnClickListener(new View.OnClickListener() {
  162. @Override
  163. public void onClick(View v) {
  164. showCustomWebView();
  165. }
  166. });
  167. findViewById(R.id.colorChooser_primary).setOnClickListener(new View.OnClickListener() {
  168. @Override
  169. public void onClick(View v) {
  170. showColorChooser(false);
  171. }
  172. });
  173. findViewById(R.id.colorChooser_accent).setOnClickListener(new View.OnClickListener() {
  174. @Override
  175. public void onClick(View v) {
  176. showColorChooser(true);
  177. }
  178. });
  179. findViewById(R.id.themed).setOnClickListener(new View.OnClickListener() {
  180. @Override
  181. public void onClick(View v) {
  182. showThemed();
  183. }
  184. });
  185. findViewById(R.id.showCancelDismiss).setOnClickListener(new View.OnClickListener() {
  186. @Override
  187. public void onClick(View v) {
  188. showShowCancelDismissCallbacks();
  189. }
  190. });
  191. findViewById(R.id.folder_chooser).setOnClickListener(new View.OnClickListener() {
  192. @Override
  193. public void onClick(View v) {
  194. new FolderSelectorDialog().show(MainActivity.this);
  195. }
  196. });
  197. findViewById(R.id.input).setOnClickListener(new View.OnClickListener() {
  198. @Override
  199. public void onClick(View v) {
  200. showInputDialog();
  201. }
  202. });
  203. findViewById(R.id.input_custominvalidation).setOnClickListener(new View.OnClickListener() {
  204. @Override
  205. public void onClick(View v) {
  206. showInputDialogCustomInvalidation();
  207. }
  208. });
  209. findViewById(R.id.progress1).setOnClickListener(new View.OnClickListener() {
  210. @Override
  211. public void onClick(View v) {
  212. showDeterminateProgressDialog();
  213. }
  214. });
  215. findViewById(R.id.progress2).setOnClickListener(new View.OnClickListener() {
  216. @Override
  217. public void onClick(View v) {
  218. showIndeterminateProgressDialog(false);
  219. }
  220. });
  221. findViewById(R.id.progress3).setOnClickListener(new View.OnClickListener() {
  222. @Override
  223. public void onClick(View v) {
  224. showIndeterminateProgressDialog(true);
  225. }
  226. });
  227. findViewById(R.id.preference_dialogs).setOnClickListener(new View.OnClickListener() {
  228. @Override
  229. public void onClick(View v) {
  230. if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1)
  231. startActivity(new Intent(getApplicationContext(), PreferenceActivity.class));
  232. else
  233. startActivity(new Intent(getApplicationContext(), PreferenceActivityCompat.class));
  234. }
  235. });
  236. }
  237. @Override
  238. protected void onPause() {
  239. super.onPause();
  240. if (mThread != null && !mThread.isInterrupted() && mThread.isAlive())
  241. mThread.interrupt();
  242. }
  243. private void showBasicNoTitle() {
  244. new MaterialDialog.Builder(this)
  245. .content(R.string.shareLocationPrompt)
  246. .positiveText(R.string.agree)
  247. .negativeText(R.string.disagree)
  248. .show();
  249. }
  250. private void showBasic() {
  251. new MaterialDialog.Builder(this)
  252. .title(R.string.useGoogleLocationServices)
  253. .content(R.string.useGoogleLocationServicesPrompt)
  254. .positiveText(R.string.agree)
  255. .negativeText(R.string.disagree)
  256. .show();
  257. }
  258. private void showBasicLongContent() {
  259. new MaterialDialog.Builder(this)
  260. .title(R.string.useGoogleLocationServices)
  261. .content(R.string.loremIpsum)
  262. .positiveText(R.string.agree)
  263. .negativeText(R.string.disagree)
  264. .show();
  265. }
  266. private void showBasicIcon() {
  267. new MaterialDialog.Builder(this)
  268. .iconRes(R.drawable.ic_launcher)
  269. .limitIconToDefaultSize() // limits the displayed icon size to 48dp
  270. .title(R.string.useGoogleLocationServices)
  271. .content(R.string.useGoogleLocationServicesPrompt)
  272. .positiveText(R.string.agree)
  273. .negativeText(R.string.disagree)
  274. .show();
  275. }
  276. private void showStacked() {
  277. new MaterialDialog.Builder(this)
  278. .title(R.string.useGoogleLocationServices)
  279. .content(R.string.useGoogleLocationServicesPrompt)
  280. .positiveText(R.string.speedBoost)
  281. .negativeText(R.string.noThanks)
  282. .btnStackedGravity(GravityEnum.END)
  283. .forceStacking(true) // this generally should not be forced, but is used for demo purposes
  284. .show();
  285. }
  286. private void showNeutral() {
  287. new MaterialDialog.Builder(this)
  288. .title(R.string.useGoogleLocationServices)
  289. .content(R.string.useGoogleLocationServicesPrompt)
  290. .positiveText(R.string.agree)
  291. .negativeText(R.string.disagree)
  292. .neutralText(R.string.more_info)
  293. .show();
  294. }
  295. private void showCallbacks() {
  296. new MaterialDialog.Builder(this)
  297. .title(R.string.useGoogleLocationServices)
  298. .content(R.string.useGoogleLocationServicesPrompt)
  299. .positiveText(R.string.agree)
  300. .negativeText(R.string.disagree)
  301. .neutralText(R.string.more_info)
  302. .callback(new MaterialDialog.ButtonCallback() {
  303. @Override
  304. public void onPositive(MaterialDialog dialog) {
  305. showToast("Positive!");
  306. }
  307. @Override
  308. public void onNeutral(MaterialDialog dialog) {
  309. showToast("Neutral");
  310. }
  311. @Override
  312. public void onNegative(MaterialDialog dialog) {
  313. showToast("Negative…");
  314. }
  315. })
  316. .show();
  317. }
  318. private void showList() {
  319. new MaterialDialog.Builder(this)
  320. .title(R.string.socialNetworks)
  321. .items(R.array.socialNetworks)
  322. .itemsCallback(new MaterialDialog.ListCallback() {
  323. @Override
  324. public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
  325. showToast(which + ": " + text);
  326. }
  327. })
  328. .show();
  329. }
  330. private void showListNoTitle() {
  331. new MaterialDialog.Builder(this)
  332. .items(R.array.socialNetworks)
  333. .itemsCallback(new MaterialDialog.ListCallback() {
  334. @Override
  335. public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
  336. showToast(which + ": " + text);
  337. }
  338. })
  339. .show();
  340. }
  341. private void showLongList() {
  342. new MaterialDialog.Builder(this)
  343. .title(R.string.states)
  344. .items(R.array.states)
  345. .itemsCallback(new MaterialDialog.ListCallback() {
  346. @Override
  347. public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
  348. showToast(which + ": " + text);
  349. }
  350. })
  351. .positiveText(android.R.string.cancel)
  352. .show();
  353. }
  354. private void showSingleChoice() {
  355. new MaterialDialog.Builder(this)
  356. .title(R.string.socialNetworks)
  357. .items(R.array.socialNetworks)
  358. .itemsCallbackSingleChoice(2, new MaterialDialog.ListCallbackSingleChoice() {
  359. @Override
  360. public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
  361. showToast(which + ": " + text);
  362. return true; // allow selection
  363. }
  364. })
  365. .positiveText(R.string.choose)
  366. .show();
  367. }
  368. private void showMultiChoice() {
  369. new MaterialDialog.Builder(this)
  370. .title(R.string.socialNetworks)
  371. .items(R.array.socialNetworks)
  372. .itemsCallbackMultiChoice(new Integer[]{1, 3}, new MaterialDialog.ListCallbackMultiChoice() {
  373. @Override
  374. public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {
  375. StringBuilder str = new StringBuilder();
  376. for (int i = 0; i < which.length; i++) {
  377. if (i > 0) str.append('\n');
  378. str.append(which[i]);
  379. str.append(": ");
  380. str.append(text[i]);
  381. }
  382. showToast(str.toString());
  383. return true; // allow selection
  384. }
  385. })
  386. .callback(new MaterialDialog.ButtonCallback() {
  387. @Override
  388. public void onNeutral(MaterialDialog dialog) {
  389. dialog.clearSelectedIndices();
  390. }
  391. })
  392. .alwaysCallMultiChoiceCallback()
  393. .positiveText(R.string.choose)
  394. .autoDismiss(false)
  395. .neutralText(R.string.clear_selection)
  396. .show();
  397. }
  398. private void showMultiChoiceLimited() {
  399. new MaterialDialog.Builder(this)
  400. .title(R.string.socialNetworks)
  401. .items(R.array.socialNetworks)
  402. .itemsCallbackMultiChoice(new Integer[]{1}, new MaterialDialog.ListCallbackMultiChoice() {
  403. @Override
  404. public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {
  405. boolean allowSelection = which.length <= 2; // limit selection to 2, the new selection is included in the which array
  406. if (!allowSelection) {
  407. showToast(R.string.selection_limit_reached);
  408. }
  409. return allowSelection;
  410. }
  411. })
  412. .positiveText(R.string.dismiss)
  413. .alwaysCallMultiChoiceCallback() // the callback will always be called, to check if selection is still allowed
  414. .show();
  415. }
  416. private void showSimpleList() {
  417. final MaterialSimpleListAdapter adapter = new MaterialSimpleListAdapter(this);
  418. adapter.add(new MaterialSimpleListItem.Builder(this)
  419. .content("username@gmail.com")
  420. .icon(R.drawable.ic_circle_darker)
  421. .build());
  422. adapter.add(new MaterialSimpleListItem.Builder(this)
  423. .content("user02@gmail.com")
  424. .icon(R.drawable.ic_circle_darker)
  425. .build());
  426. adapter.add(new MaterialSimpleListItem.Builder(this)
  427. .content(R.string.add_account)
  428. .icon(R.drawable.ic_circle_lighter)
  429. .build());
  430. new MaterialDialog.Builder(this)
  431. .title(R.string.set_backup)
  432. .adapter(adapter, new MaterialDialog.ListCallback() {
  433. @Override
  434. public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
  435. MaterialSimpleListItem item = adapter.getItem(which);
  436. showToast(item.getContent().toString());
  437. }
  438. })
  439. .show();
  440. }
  441. private void showCustomList() {
  442. new MaterialDialog.Builder(this)
  443. .title(R.string.socialNetworks)
  444. .adapter(new ButtonItemAdapter(this, R.array.socialNetworks),
  445. new MaterialDialog.ListCallback() {
  446. @Override
  447. public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
  448. showToast("Clicked item " + which);
  449. }
  450. })
  451. .show();
  452. }
  453. private EditText passwordInput;
  454. private View positiveAction;
  455. private void showCustomView() {
  456. MaterialDialog dialog = new MaterialDialog.Builder(this)
  457. .title(R.string.googleWifi)
  458. .customView(R.layout.dialog_customview, true)
  459. .positiveText(R.string.connect)
  460. .negativeText(android.R.string.cancel)
  461. .callback(new MaterialDialog.ButtonCallback() {
  462. @Override
  463. public void onPositive(MaterialDialog dialog) {
  464. showToast("Password: " + passwordInput.getText().toString());
  465. }
  466. @Override
  467. public void onNegative(MaterialDialog dialog) {
  468. }
  469. }).build();
  470. positiveAction = dialog.getActionButton(DialogAction.POSITIVE);
  471. //noinspection ConstantConditions
  472. passwordInput = (EditText) dialog.getCustomView().findViewById(R.id.password);
  473. passwordInput.addTextChangedListener(new TextWatcher() {
  474. @Override
  475. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  476. }
  477. @Override
  478. public void onTextChanged(CharSequence s, int start, int before, int count) {
  479. positiveAction.setEnabled(s.toString().trim().length() > 0);
  480. }
  481. @Override
  482. public void afterTextChanged(Editable s) {
  483. }
  484. });
  485. // Toggling the show password CheckBox will mask or unmask the password input EditText
  486. CheckBox checkbox = (CheckBox) dialog.getCustomView().findViewById(R.id.showPassword);
  487. checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  488. @Override
  489. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  490. passwordInput.setInputType(!isChecked ? InputType.TYPE_TEXT_VARIATION_PASSWORD : InputType.TYPE_CLASS_TEXT);
  491. passwordInput.setTransformationMethod(!isChecked ? PasswordTransformationMethod.getInstance() : null);
  492. }
  493. });
  494. int widgetColor = ThemeSingleton.get().widgetColor;
  495. MDTintHelper.setTint(checkbox,
  496. widgetColor == 0 ? ContextCompat.getColor(this, R.color.material_teal_500) : widgetColor);
  497. MDTintHelper.setTint(passwordInput,
  498. widgetColor == 0 ? ContextCompat.getColor(this, R.color.material_teal_500) : widgetColor);
  499. dialog.show();
  500. positiveAction.setEnabled(false); // disabled by default
  501. }
  502. private void showCustomWebView() {
  503. int accentColor = ThemeSingleton.get().widgetColor;
  504. if (accentColor == 0)
  505. accentColor = ContextCompat.getColor(this, R.color.material_teal_500);
  506. ChangelogDialog.create(false, accentColor)
  507. .show(getSupportFragmentManager(), "changelog");
  508. }
  509. private int primaryPreselect;
  510. private int accentPreselect;
  511. private void showColorChooser(boolean accent) {
  512. new ColorChooserDialog.Builder(this, R.string.color_palette)
  513. .titleSub(R.string.colors)
  514. .accentMode(accent)
  515. .doneButton(R.string.md_done_label)
  516. .cancelButton(R.string.md_cancel_label)
  517. .backButton(R.string.md_back_label)
  518. .preselect(accent ? accentPreselect : primaryPreselect)
  519. .show();
  520. }
  521. // Receives callback from color chooser dialog
  522. @Override
  523. public void onColorSelection(@NonNull ColorChooserDialog dialog, @ColorInt int color) {
  524. if (dialog.isAccentMode()) {
  525. accentPreselect = color;
  526. ThemeSingleton.get().positiveColor = DialogUtils.getActionTextStateList(this, color);
  527. ThemeSingleton.get().neutralColor = DialogUtils.getActionTextStateList(this, color);
  528. ThemeSingleton.get().negativeColor = DialogUtils.getActionTextStateList(this, color);
  529. ThemeSingleton.get().widgetColor = color;
  530. } else {
  531. primaryPreselect = color;
  532. if (getSupportActionBar() != null)
  533. getSupportActionBar().setBackgroundDrawable(new ColorDrawable(color));
  534. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  535. getWindow().setStatusBarColor(CircleView.shiftColorDown(color));
  536. getWindow().setNavigationBarColor(color);
  537. }
  538. }
  539. }
  540. private void showThemed() {
  541. new MaterialDialog.Builder(this)
  542. .title(R.string.useGoogleLocationServices)
  543. .content(R.string.useGoogleLocationServicesPrompt)
  544. .positiveText(R.string.agree)
  545. .negativeText(R.string.disagree)
  546. .positiveColorRes(R.color.material_red_400)
  547. .negativeColorRes(R.color.material_red_400)
  548. .titleGravity(GravityEnum.CENTER)
  549. .titleColorRes(R.color.material_red_400)
  550. .contentColorRes(android.R.color.white)
  551. .backgroundColorRes(R.color.material_blue_grey_800)
  552. .dividerColorRes(R.color.material_teal_500)
  553. .btnSelector(R.drawable.md_btn_selector_custom, DialogAction.POSITIVE)
  554. .positiveColor(Color.WHITE)
  555. .negativeColorAttr(android.R.attr.textColorSecondaryInverse)
  556. .theme(Theme.DARK)
  557. .show();
  558. }
  559. private void showShowCancelDismissCallbacks() {
  560. new MaterialDialog.Builder(this)
  561. .title(R.string.useGoogleLocationServices)
  562. .content(R.string.useGoogleLocationServicesPrompt)
  563. .positiveText(R.string.agree)
  564. .negativeText(R.string.disagree)
  565. .neutralText(R.string.more_info)
  566. .showListener(new DialogInterface.OnShowListener() {
  567. @Override
  568. public void onShow(DialogInterface dialog) {
  569. showToast("onShow");
  570. }
  571. })
  572. .cancelListener(new DialogInterface.OnCancelListener() {
  573. @Override
  574. public void onCancel(DialogInterface dialog) {
  575. showToast("onCancel");
  576. }
  577. })
  578. .dismissListener(new DialogInterface.OnDismissListener() {
  579. @Override
  580. public void onDismiss(DialogInterface dialog) {
  581. showToast("onDismiss");
  582. }
  583. })
  584. .show();
  585. }
  586. private void showInputDialog() {
  587. new MaterialDialog.Builder(this)
  588. .title(R.string.input)
  589. .content(R.string.input_content)
  590. .inputType(InputType.TYPE_CLASS_TEXT |
  591. InputType.TYPE_TEXT_VARIATION_PERSON_NAME |
  592. InputType.TYPE_TEXT_FLAG_CAP_WORDS)
  593. .inputMaxLength(16)
  594. .positiveText(R.string.submit)
  595. .input(R.string.input_hint, R.string.input_hint, false, new MaterialDialog.InputCallback() {
  596. @Override
  597. public void onInput(MaterialDialog dialog, CharSequence input) {
  598. showToast("Hello, " + input.toString() + "!");
  599. }
  600. }).show();
  601. }
  602. private void showInputDialogCustomInvalidation() {
  603. new MaterialDialog.Builder(this)
  604. .title(R.string.input)
  605. .content(R.string.input_content_custominvalidation)
  606. .inputType(InputType.TYPE_CLASS_TEXT |
  607. InputType.TYPE_TEXT_VARIATION_PERSON_NAME |
  608. InputType.TYPE_TEXT_FLAG_CAP_WORDS)
  609. .positiveText(R.string.submit)
  610. .alwaysCallInputCallback() // this forces the callback to be invoked with every input change
  611. .input(R.string.input_hint, 0, false, new MaterialDialog.InputCallback() {
  612. @Override
  613. public void onInput(MaterialDialog dialog, CharSequence input) {
  614. if (input.toString().equalsIgnoreCase("hello")) {
  615. dialog.setContent("I told you not to type that!");
  616. dialog.getActionButton(DialogAction.POSITIVE).setEnabled(false);
  617. } else {
  618. dialog.setContent(R.string.input_content_custominvalidation);
  619. dialog.getActionButton(DialogAction.POSITIVE).setEnabled(true);
  620. }
  621. }
  622. }).show();
  623. }
  624. private void showIndeterminateProgressDialog(boolean horizontal) {
  625. new MaterialDialog.Builder(this)
  626. .title(R.string.progress_dialog)
  627. .content(R.string.please_wait)
  628. .progress(true, 0)
  629. .progressIndeterminateStyle(horizontal)
  630. .show();
  631. }
  632. private void showDeterminateProgressDialog() {
  633. new MaterialDialog.Builder(this)
  634. .title(R.string.progress_dialog)
  635. .content(R.string.please_wait)
  636. .contentGravity(GravityEnum.CENTER)
  637. .progress(false, 150, true)
  638. .cancelListener(new DialogInterface.OnCancelListener() {
  639. @Override
  640. public void onCancel(DialogInterface dialog) {
  641. if (mThread != null)
  642. mThread.interrupt();
  643. }
  644. })
  645. .showListener(new DialogInterface.OnShowListener() {
  646. @Override
  647. public void onShow(DialogInterface dialogInterface) {
  648. final MaterialDialog dialog = (MaterialDialog) dialogInterface;
  649. startThread(new Runnable() {
  650. @Override
  651. public void run() {
  652. while (dialog.getCurrentProgress() != dialog.getMaxProgress() &&
  653. !Thread.currentThread().isInterrupted()) {
  654. if (dialog.isCancelled())
  655. break;
  656. try {
  657. Thread.sleep(50);
  658. } catch (InterruptedException e) {
  659. break;
  660. }
  661. dialog.incrementProgress(1);
  662. }
  663. runOnUiThread(new Runnable() {
  664. @Override
  665. public void run() {
  666. mThread = null;
  667. dialog.setContent(getString(R.string.md_done_label));
  668. }
  669. });
  670. }
  671. });
  672. }
  673. }).show();
  674. }
  675. @Override
  676. public boolean onCreateOptionsMenu(Menu menu) {
  677. getMenuInflater().inflate(R.menu.main, menu);
  678. return super.onCreateOptionsMenu(menu);
  679. }
  680. @Override
  681. public boolean onOptionsItemSelected(MenuItem item) {
  682. if (item.getItemId() == R.id.about) {
  683. new MaterialDialog.Builder(this)
  684. .title(R.string.about)
  685. .positiveText(R.string.dismiss)
  686. .content(Html.fromHtml(getString(R.string.about_body)))
  687. .contentLineSpacing(1.6f)
  688. .show();
  689. return true;
  690. }
  691. return super.onOptionsItemSelected(item);
  692. }
  693. @Override
  694. public void onFolderSelection(File folder) {
  695. showToast(folder.getAbsolutePath());
  696. }
  697. }