MainActivity.java 15 KB

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