MainActivity.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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.longList).setOnClickListener(new View.OnClickListener() {
  77. @Override
  78. public void onClick(View v) {
  79. showLongList();
  80. }
  81. });
  82. findViewById(R.id.singleChoice).setOnClickListener(new View.OnClickListener() {
  83. @Override
  84. public void onClick(View v) {
  85. showSingleChoice();
  86. }
  87. });
  88. findViewById(R.id.multiChoice).setOnClickListener(new View.OnClickListener() {
  89. @Override
  90. public void onClick(View v) {
  91. showMultiChoice();
  92. }
  93. });
  94. findViewById(R.id.customListItems).setOnClickListener(new View.OnClickListener() {
  95. @Override
  96. public void onClick(View v) {
  97. showCustomList();
  98. }
  99. });
  100. findViewById(R.id.customView).setOnClickListener(new View.OnClickListener() {
  101. @Override
  102. public void onClick(View v) {
  103. showCustomView();
  104. }
  105. });
  106. findViewById(R.id.themed).setOnClickListener(new View.OnClickListener() {
  107. @Override
  108. public void onClick(View v) {
  109. showThemed();
  110. }
  111. });
  112. }
  113. private void showBasicNoTitle() {
  114. new MaterialDialog.Builder(this)
  115. .content(R.string.shareLocationPrompt)
  116. .positiveText(R.string.agree) // the default is 'Accept', this line could be left out
  117. .negativeText(R.string.disagree) // leaving this line out will remove the negative button
  118. .build()
  119. .show();
  120. }
  121. private void showBasic() {
  122. new MaterialDialog.Builder(this)
  123. .title(R.string.useGoogleLocationServices)
  124. .content(R.string.useGoogleLocationServicesPrompt)
  125. .positiveText(R.string.agree) // the default is 'Accept', this line could be left out
  126. .negativeText(R.string.disagree) // leaving this line out will remove the negative button
  127. .build()
  128. .show();
  129. }
  130. private void showBasicLongContent() {
  131. new MaterialDialog.Builder(this)
  132. .title(R.string.useGoogleLocationServices)
  133. .content(R.string.loremIpsum)
  134. .positiveText(R.string.agree) // the default is 'Accept', this line could be left out
  135. .negativeText(R.string.disagree) // leaving this line out will remove the negative button
  136. .build()
  137. .show();
  138. }
  139. private void showBasicIcon() {
  140. new MaterialDialog.Builder(this)
  141. .icon(R.drawable.ic_launcher)
  142. .title(R.string.useGoogleLocationServices)
  143. .content(R.string.useGoogleLocationServicesPrompt)
  144. .positiveText(R.string.agree) // the default is 'Accept', this line could be left out
  145. .negativeText(R.string.disagree) // leaving this line out will remove the negative button
  146. .build()
  147. .show();
  148. }
  149. private void showStacked() {
  150. new MaterialDialog.Builder(this)
  151. .title(R.string.useGoogleLocationServices)
  152. .content(R.string.useGoogleLocationServicesPrompt)
  153. .positiveText(R.string.speedBoost)
  154. .negativeText(R.string.noThanks)
  155. .build()
  156. .show();
  157. }
  158. private void showNeutral() {
  159. new MaterialDialog.Builder(this)
  160. .title(R.string.useGoogleLocationServices)
  161. .content(R.string.useGoogleLocationServicesPrompt)
  162. .positiveText(R.string.agree)
  163. .negativeText(R.string.disagree)
  164. .neutralText(R.string.more_info)
  165. .build()
  166. .show();
  167. }
  168. private void showCallbacks() {
  169. new MaterialDialog.Builder(this)
  170. .title(R.string.useGoogleLocationServices)
  171. .content(R.string.useGoogleLocationServicesPrompt)
  172. .positiveText(R.string.agree)
  173. .negativeText(R.string.disagree)
  174. .neutralText(R.string.more_info)
  175. .callback(new MaterialDialog.FullCallback() {
  176. @Override
  177. public void onPositive(MaterialDialog dialog) {
  178. Toast.makeText(getApplicationContext(), "Positive!", Toast.LENGTH_SHORT).show();
  179. }
  180. @Override
  181. public void onNeutral(MaterialDialog dialog) {
  182. Toast.makeText(getApplicationContext(), "Neutral", Toast.LENGTH_SHORT).show();
  183. }
  184. @Override
  185. public void onNegative(MaterialDialog dialog) {
  186. Toast.makeText(getApplicationContext(), "Negative…", Toast.LENGTH_SHORT).show();
  187. }
  188. })
  189. .build()
  190. .show();
  191. }
  192. private void showList() {
  193. new MaterialDialog.Builder(this)
  194. .title(R.string.socialNetworks)
  195. .items(R.array.socialNetworks)
  196. .itemsCallback(new MaterialDialog.ListCallback() {
  197. @Override
  198. public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
  199. Toast.makeText(getApplicationContext(), which + ": " + text, Toast.LENGTH_SHORT).show();
  200. }
  201. })
  202. .build()
  203. .show();
  204. }
  205. private void showLongList() {
  206. new MaterialDialog.Builder(this)
  207. .title(R.string.states)
  208. .items(R.array.states)
  209. .itemsCallback(new MaterialDialog.ListCallback() {
  210. @Override
  211. public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
  212. Toast.makeText(getApplicationContext(), which + ": " + text, Toast.LENGTH_SHORT).show();
  213. }
  214. })
  215. .build()
  216. .show();
  217. }
  218. private void showSingleChoice() {
  219. new MaterialDialog.Builder(this)
  220. .title(R.string.socialNetworks)
  221. .items(R.array.socialNetworks)
  222. .itemsCallbackSingleChoice(2, new MaterialDialog.ListCallback() {
  223. @Override
  224. public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
  225. Toast.makeText(getApplicationContext(), which + ": " + text, Toast.LENGTH_SHORT).show();
  226. }
  227. })
  228. .positiveText(R.string.choose)
  229. .build()
  230. .show();
  231. }
  232. private void showMultiChoice() {
  233. new MaterialDialog.Builder(this)
  234. .title(R.string.socialNetworks)
  235. .items(R.array.socialNetworks)
  236. .itemsCallbackMultiChoice(new Integer[]{1, 3}, new MaterialDialog.ListCallbackMulti() {
  237. @Override
  238. public void onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {
  239. StringBuilder str = new StringBuilder();
  240. for (int i = 0; i < which.length; i++) {
  241. str.append(which[i]);
  242. str.append(": ");
  243. str.append(text[i]);
  244. str.append('\n');
  245. }
  246. Toast.makeText(getApplicationContext(), str.toString(), Toast.LENGTH_LONG).show();
  247. }
  248. })
  249. .positiveText(R.string.choose)
  250. .build()
  251. .show();
  252. }
  253. private void showCustomList() {
  254. new MaterialDialog.Builder(this)
  255. .title(R.string.socialNetworks)
  256. .items(R.array.socialNetworks)
  257. .itemsCallback(new MaterialDialog.ListCallback() {
  258. @Override
  259. public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
  260. Toast.makeText(getApplicationContext(), which + ": " + text, Toast.LENGTH_SHORT).show();
  261. }
  262. })
  263. .itemProcessor(new ButtonItemProcessor(this))
  264. .build()
  265. .show();
  266. }
  267. EditText passwordInput;
  268. View positiveAction;
  269. private void showCustomView() {
  270. MaterialDialog dialog = new MaterialDialog.Builder(this)
  271. .title(R.string.googleWifi)
  272. .customView(R.layout.dialog_customview)
  273. .positiveText(R.string.connect)
  274. .negativeText(android.R.string.cancel)
  275. .callback(new MaterialDialog.Callback() {
  276. @Override
  277. public void onPositive(MaterialDialog dialog) {
  278. Toast.makeText(getApplicationContext(), "Password: " + passwordInput.getText().toString(), Toast.LENGTH_SHORT).show();
  279. }
  280. @Override
  281. public void onNegative(MaterialDialog dialog) {
  282. }
  283. }).build();
  284. positiveAction = dialog.getActionButton(DialogAction.POSITIVE);
  285. passwordInput = (EditText) dialog.getCustomView().findViewById(R.id.password);
  286. passwordInput.addTextChangedListener(new TextWatcher() {
  287. @Override
  288. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  289. }
  290. @Override
  291. public void onTextChanged(CharSequence s, int start, int before, int count) {
  292. positiveAction.setEnabled(s.toString().trim().length() > 0);
  293. }
  294. @Override
  295. public void afterTextChanged(Editable s) {
  296. }
  297. });
  298. // Toggling the show password CheckBox will mask or unmask the password input EditText
  299. ((CheckBox) dialog.getCustomView().findViewById(R.id.showPassword)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  300. @Override
  301. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  302. passwordInput.setInputType(!isChecked ? InputType.TYPE_TEXT_VARIATION_PASSWORD : InputType.TYPE_CLASS_TEXT);
  303. passwordInput.setTransformationMethod(!isChecked ? PasswordTransformationMethod.getInstance() : null);
  304. }
  305. });
  306. dialog.show();
  307. positiveAction.setEnabled(false); // disabled by default
  308. }
  309. private void showThemed() {
  310. new MaterialDialog.Builder(this)
  311. .title(R.string.useGoogleLocationServices)
  312. .content(R.string.useGoogleLocationServicesPrompt)
  313. .positiveText(R.string.agree)
  314. .negativeText(R.string.disagree)
  315. .positiveColorRes(R.color.material_red_400)
  316. .negativeColorRes(R.color.material_red_400)
  317. .titleAlignment(Alignment.CENTER)
  318. .titleColorRes(R.color.material_red_400)
  319. .theme(Theme.DARK)
  320. .build()
  321. .show();
  322. }
  323. @Override
  324. public boolean onCreateOptionsMenu(Menu menu) {
  325. getMenuInflater().inflate(R.menu.main, menu);
  326. return super.onCreateOptionsMenu(menu);
  327. }
  328. @Override
  329. public boolean onOptionsItemSelected(MenuItem item) {
  330. if (item.getItemId() == R.id.about) {
  331. new MaterialDialog.Builder(this)
  332. .title(R.string.about)
  333. .positiveText(R.string.dismiss)
  334. .content(Html.fromHtml(getString(R.string.about_body)))
  335. .contentLineSpacing(1.6f)
  336. .callback(new MaterialDialog.SimpleCallback() {
  337. @Override
  338. public void onPositive(MaterialDialog dialog) {
  339. }
  340. })
  341. .build()
  342. .show();
  343. return true;
  344. }
  345. return super.onOptionsItemSelected(item);
  346. }
  347. }