MainActivity.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. package com.afollestad.materialdialogssample;
  2. import android.content.DialogInterface;
  3. import android.os.Bundle;
  4. import android.support.v7.app.ActionBarActivity;
  5. import android.text.Editable;
  6. import android.text.Html;
  7. import android.text.InputType;
  8. import android.text.TextWatcher;
  9. import android.text.method.PasswordTransformationMethod;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.view.View;
  13. import android.webkit.WebView;
  14. import android.widget.AdapterView;
  15. import android.widget.CheckBox;
  16. import android.widget.CompoundButton;
  17. import android.widget.EditText;
  18. import android.widget.ListView;
  19. import android.widget.Toast;
  20. import com.afollestad.materialdialogs.DialogAction;
  21. import com.afollestad.materialdialogs.MaterialDialog;
  22. import com.afollestad.materialdialogs.Theme;
  23. import java.io.File;
  24. /**
  25. * @author Aidan Follestad (afollestad)
  26. */
  27. public class MainActivity extends ActionBarActivity implements FolderSelectorDialog.FolderSelectCallback {
  28. @Override
  29. protected void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.activity_main);
  32. findViewById(R.id.basicNoTitle).setOnClickListener(new View.OnClickListener() {
  33. @Override
  34. public void onClick(View v) {
  35. showBasicNoTitle();
  36. }
  37. });
  38. findViewById(R.id.basic).setOnClickListener(new View.OnClickListener() {
  39. @Override
  40. public void onClick(View v) {
  41. showBasic();
  42. }
  43. });
  44. findViewById(R.id.basicLongContent).setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View v) {
  47. showBasicLongContent();
  48. }
  49. });
  50. findViewById(R.id.basicIcon).setOnClickListener(new View.OnClickListener() {
  51. @Override
  52. public void onClick(View v) {
  53. showBasicIcon();
  54. }
  55. });
  56. findViewById(R.id.stacked).setOnClickListener(new View.OnClickListener() {
  57. @Override
  58. public void onClick(View v) {
  59. showStacked();
  60. }
  61. });
  62. findViewById(R.id.neutral).setOnClickListener(new View.OnClickListener() {
  63. @Override
  64. public void onClick(View v) {
  65. showNeutral();
  66. }
  67. });
  68. findViewById(R.id.callbacks).setOnClickListener(new View.OnClickListener() {
  69. @Override
  70. public void onClick(View v) {
  71. showCallbacks();
  72. }
  73. });
  74. findViewById(R.id.list).setOnClickListener(new View.OnClickListener() {
  75. @Override
  76. public void onClick(View v) {
  77. showList();
  78. }
  79. });
  80. findViewById(R.id.listNoTitle).setOnClickListener(new View.OnClickListener() {
  81. @Override
  82. public void onClick(View v) {
  83. showListNoTitle();
  84. }
  85. });
  86. findViewById(R.id.longList).setOnClickListener(new View.OnClickListener() {
  87. @Override
  88. public void onClick(View v) {
  89. showLongList();
  90. }
  91. });
  92. findViewById(R.id.singleChoice).setOnClickListener(new View.OnClickListener() {
  93. @Override
  94. public void onClick(View v) {
  95. showSingleChoice();
  96. }
  97. });
  98. findViewById(R.id.multiChoice).setOnClickListener(new View.OnClickListener() {
  99. @Override
  100. public void onClick(View v) {
  101. showMultiChoice();
  102. }
  103. });
  104. findViewById(R.id.customListItems).setOnClickListener(new View.OnClickListener() {
  105. @Override
  106. public void onClick(View v) {
  107. showCustomList();
  108. }
  109. });
  110. findViewById(R.id.customView).setOnClickListener(new View.OnClickListener() {
  111. @Override
  112. public void onClick(View v) {
  113. showCustomView();
  114. }
  115. });
  116. findViewById(R.id.customView_noScroll).setOnClickListener(new View.OnClickListener() {
  117. @Override
  118. public void onClick(View v) {
  119. showCustomWebView();
  120. }
  121. });
  122. findViewById(R.id.themed).setOnClickListener(new View.OnClickListener() {
  123. @Override
  124. public void onClick(View v) {
  125. showThemed();
  126. }
  127. });
  128. findViewById(R.id.showCancelDismiss).setOnClickListener(new View.OnClickListener() {
  129. @Override
  130. public void onClick(View v) {
  131. showShowCancelDismissCallbacks();
  132. }
  133. });
  134. findViewById(R.id.folder_chooser).setOnClickListener(new View.OnClickListener() {
  135. @Override
  136. public void onClick(View v) {
  137. new FolderSelectorDialog().show(MainActivity.this);
  138. }
  139. });
  140. }
  141. private void showBasicNoTitle() {
  142. new MaterialDialog.Builder(this)
  143. .content(R.string.shareLocationPrompt)
  144. .positiveText(R.string.agree)
  145. .negativeText(R.string.disagree)
  146. .show();
  147. }
  148. private void showBasic() {
  149. new MaterialDialog.Builder(this)
  150. .title(R.string.useGoogleLocationServices)
  151. .content(R.string.useGoogleLocationServicesPrompt)
  152. .positiveText(R.string.agree)
  153. .negativeText(R.string.disagree)
  154. .show();
  155. }
  156. private void showBasicLongContent() {
  157. new MaterialDialog.Builder(this)
  158. .title(R.string.useGoogleLocationServices)
  159. .content(R.string.loremIpsum)
  160. .positiveText(R.string.agree)
  161. .negativeText(R.string.disagree)
  162. .show();
  163. }
  164. private void showBasicIcon() {
  165. new MaterialDialog.Builder(this)
  166. .icon(R.drawable.ic_launcher)
  167. .title(R.string.useGoogleLocationServices)
  168. .content(R.string.useGoogleLocationServicesPrompt)
  169. .positiveText(R.string.agree)
  170. .negativeText(R.string.disagree)
  171. .show();
  172. }
  173. private void showStacked() {
  174. new MaterialDialog.Builder(this)
  175. .title(R.string.useGoogleLocationServices)
  176. .content(R.string.useGoogleLocationServicesPrompt)
  177. .positiveText(R.string.speedBoost)
  178. .negativeText(R.string.noThanks)
  179. .forceStacking(true) // this generally should not be forced, but is used for demo purposes
  180. .show();
  181. }
  182. private void showNeutral() {
  183. new MaterialDialog.Builder(this)
  184. .title(R.string.useGoogleLocationServices)
  185. .content(R.string.useGoogleLocationServicesPrompt)
  186. .positiveText(R.string.agree)
  187. .negativeText(R.string.disagree)
  188. .neutralText(R.string.more_info)
  189. .show();
  190. }
  191. private void showCallbacks() {
  192. new MaterialDialog.Builder(this)
  193. .title(R.string.useGoogleLocationServices)
  194. .content(R.string.useGoogleLocationServicesPrompt)
  195. .positiveText(R.string.agree)
  196. .negativeText(R.string.disagree)
  197. .neutralText(R.string.more_info)
  198. .callback(new MaterialDialog.ButtonCallback() {
  199. @Override
  200. public void onPositive(MaterialDialog dialog) {
  201. Toast.makeText(getApplicationContext(), "Positive!", Toast.LENGTH_SHORT).show();
  202. }
  203. @Override
  204. public void onNeutral(MaterialDialog dialog) {
  205. Toast.makeText(getApplicationContext(), "Neutral", Toast.LENGTH_SHORT).show();
  206. }
  207. @Override
  208. public void onNegative(MaterialDialog dialog) {
  209. Toast.makeText(getApplicationContext(), "Negative…", Toast.LENGTH_SHORT).show();
  210. }
  211. })
  212. .show();
  213. }
  214. private void showList() {
  215. new MaterialDialog.Builder(this)
  216. .title(R.string.socialNetworks)
  217. .items(R.array.socialNetworks)
  218. .itemsCallback(new MaterialDialog.ListCallback() {
  219. @Override
  220. public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
  221. Toast.makeText(getApplicationContext(), which + ": " + text, Toast.LENGTH_SHORT).show();
  222. }
  223. })
  224. .show();
  225. }
  226. private void showListNoTitle() {
  227. new MaterialDialog.Builder(this)
  228. .items(R.array.states)
  229. .itemsCallback(new MaterialDialog.ListCallback() {
  230. @Override
  231. public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
  232. Toast.makeText(getApplicationContext(), which + ": " + text, Toast.LENGTH_SHORT).show();
  233. }
  234. })
  235. .show();
  236. }
  237. private void showLongList() {
  238. new MaterialDialog.Builder(this)
  239. .title(R.string.states)
  240. .items(R.array.states)
  241. .itemsCallback(new MaterialDialog.ListCallback() {
  242. @Override
  243. public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
  244. Toast.makeText(getApplicationContext(), which + ": " + text, Toast.LENGTH_SHORT).show();
  245. }
  246. })
  247. .positiveText(android.R.string.ok)
  248. .show();
  249. }
  250. private void showSingleChoice() {
  251. new MaterialDialog.Builder(this)
  252. .title(R.string.socialNetworks)
  253. .items(R.array.socialNetworks)
  254. .itemsCallbackSingleChoice(2, new MaterialDialog.ListCallback() {
  255. @Override
  256. public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
  257. Toast.makeText(getApplicationContext(), which + ": " + text, Toast.LENGTH_SHORT).show();
  258. }
  259. })
  260. .positiveText(R.string.choose)
  261. .show();
  262. }
  263. private void showMultiChoice() {
  264. new MaterialDialog.Builder(this)
  265. .title(R.string.socialNetworks)
  266. .items(R.array.socialNetworks)
  267. .itemsCallbackMultiChoice(new Integer[]{1, 3}, new MaterialDialog.ListCallbackMulti() {
  268. @Override
  269. public void onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {
  270. StringBuilder str = new StringBuilder();
  271. for (int i = 0; i < which.length; i++) {
  272. str.append(which[i]);
  273. str.append(": ");
  274. str.append(text[i]);
  275. str.append('\n');
  276. }
  277. Toast.makeText(getApplicationContext(), str.toString(), Toast.LENGTH_LONG).show();
  278. }
  279. })
  280. .positiveText(R.string.choose)
  281. .show();
  282. }
  283. private void showCustomList() {
  284. MaterialDialog dialog = new MaterialDialog.Builder(this)
  285. .title(R.string.socialNetworks)
  286. .adapter(new ButtonItemAdapter(this, R.array.socialNetworks))
  287. .build();
  288. ListView listView = dialog.getListView();
  289. if (listView != null) {
  290. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  291. @Override
  292. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  293. Toast.makeText(MainActivity.this, "Clicked item " + position, Toast.LENGTH_SHORT).show();
  294. }
  295. });
  296. }
  297. dialog.show();
  298. }
  299. EditText passwordInput;
  300. View positiveAction;
  301. private void showCustomView() {
  302. MaterialDialog dialog = new MaterialDialog.Builder(this)
  303. .title(R.string.googleWifi)
  304. .customView(R.layout.dialog_customview, true)
  305. .positiveText(R.string.connect)
  306. .negativeText(android.R.string.cancel)
  307. .callback(new MaterialDialog.ButtonCallback() {
  308. @Override
  309. public void onPositive(MaterialDialog dialog) {
  310. Toast.makeText(getApplicationContext(), "Password: " + passwordInput.getText().toString(), Toast.LENGTH_SHORT).show();
  311. }
  312. @Override
  313. public void onNegative(MaterialDialog dialog) {
  314. }
  315. }).build();
  316. positiveAction = dialog.getActionButton(DialogAction.POSITIVE);
  317. passwordInput = (EditText) dialog.getCustomView().findViewById(R.id.password);
  318. passwordInput.addTextChangedListener(new TextWatcher() {
  319. @Override
  320. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  321. }
  322. @Override
  323. public void onTextChanged(CharSequence s, int start, int before, int count) {
  324. positiveAction.setEnabled(s.toString().trim().length() > 0);
  325. }
  326. @Override
  327. public void afterTextChanged(Editable s) {
  328. }
  329. });
  330. // Toggling the show password CheckBox will mask or unmask the password input EditText
  331. ((CheckBox) dialog.getCustomView().findViewById(R.id.showPassword)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  332. @Override
  333. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  334. passwordInput.setInputType(!isChecked ? InputType.TYPE_TEXT_VARIATION_PASSWORD : InputType.TYPE_CLASS_TEXT);
  335. passwordInput.setTransformationMethod(!isChecked ? PasswordTransformationMethod.getInstance() : null);
  336. }
  337. });
  338. dialog.show();
  339. positiveAction.setEnabled(false); // disabled by default
  340. }
  341. private void showCustomWebView() {
  342. MaterialDialog dialog = new MaterialDialog.Builder(this)
  343. .title(R.string.changelog)
  344. .customView(R.layout.dialog_webview, false)
  345. .positiveText(android.R.string.ok)
  346. .build();
  347. WebView webView = (WebView) dialog.getCustomView().findViewById(R.id.webview);
  348. webView.loadUrl("file:///android_asset/webview.html");
  349. dialog.show();
  350. }
  351. private void showThemed() {
  352. new MaterialDialog.Builder(this)
  353. .title(R.string.useGoogleLocationServices)
  354. .content(R.string.useGoogleLocationServicesPrompt)
  355. .positiveText(R.string.agree)
  356. .negativeText(R.string.disagree)
  357. .positiveColorRes(R.color.material_red_400)
  358. .negativeColorRes(R.color.material_red_400)
  359. .titleGravity(MaterialDialog.CENTER)
  360. .titleColorRes(R.color.material_red_400)
  361. .contentColorRes(android.R.color.white)
  362. .backgroundColorRes(R.color.material_blue_grey_800)
  363. .dividerColorRes(R.color.material_pink_500)
  364. .theme(Theme.DARK)
  365. .show();
  366. }
  367. private void showShowCancelDismissCallbacks() {
  368. new MaterialDialog.Builder(this)
  369. .title(R.string.useGoogleLocationServices)
  370. .content(R.string.useGoogleLocationServicesPrompt)
  371. .positiveText(R.string.agree)
  372. .negativeText(R.string.disagree)
  373. .neutralText(R.string.more_info)
  374. .showListener(new DialogInterface.OnShowListener() {
  375. @Override
  376. public void onShow(DialogInterface dialog) {
  377. Toast.makeText(getApplicationContext(), "onShow", Toast.LENGTH_SHORT).show();
  378. }
  379. })
  380. .cancelListener(new DialogInterface.OnCancelListener() {
  381. @Override
  382. public void onCancel(DialogInterface dialog) {
  383. Toast.makeText(getApplicationContext(), "onCancel", Toast.LENGTH_SHORT).show();
  384. }
  385. })
  386. .dismissListener(new DialogInterface.OnDismissListener() {
  387. @Override
  388. public void onDismiss(DialogInterface dialog) {
  389. Toast.makeText(getApplicationContext(), "onDismiss", Toast.LENGTH_SHORT).show();
  390. }
  391. })
  392. .show();
  393. }
  394. @Override
  395. public boolean onCreateOptionsMenu(Menu menu) {
  396. getMenuInflater().inflate(R.menu.main, menu);
  397. return super.onCreateOptionsMenu(menu);
  398. }
  399. @Override
  400. public boolean onOptionsItemSelected(MenuItem item) {
  401. if (item.getItemId() == R.id.about) {
  402. new MaterialDialog.Builder(this)
  403. .title(R.string.about)
  404. .positiveText(R.string.dismiss)
  405. .content(Html.fromHtml(getString(R.string.about_body)))
  406. .contentLineSpacing(1.6f)
  407. .build()
  408. .show();
  409. return true;
  410. }
  411. return super.onOptionsItemSelected(item);
  412. }
  413. @Override
  414. public void onFolderSelection(File folder) {
  415. Toast.makeText(this, folder.getAbsolutePath(), Toast.LENGTH_SHORT).show();
  416. }
  417. }