123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- package com.afollestad.materialdialogssample;
- import android.os.Bundle;
- import android.support.v7.app.ActionBarActivity;
- import android.text.Editable;
- import android.text.Html;
- import android.text.InputType;
- import android.text.TextWatcher;
- import android.text.method.PasswordTransformationMethod;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.afollestad.materialdialogs.Alignment;
- import com.afollestad.materialdialogs.DialogAction;
- import com.afollestad.materialdialogs.MaterialDialog;
- import com.afollestad.materialdialogs.Theme;
- /**
- * @author Aidan Follestad (afollestad)
- */
- public class MainActivity extends ActionBarActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- findViewById(R.id.basicNoTitle).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showBasicNoTitle();
- }
- });
- findViewById(R.id.basic).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showBasic();
- }
- });
- findViewById(R.id.basicLongContent).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showBasicLongContent();
- }
- });
- findViewById(R.id.basicIcon).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showBasicIcon();
- }
- });
- findViewById(R.id.stacked).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showStacked();
- }
- });
- findViewById(R.id.neutral).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showNeutral();
- }
- });
- findViewById(R.id.callbacks).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showCallbacks();
- }
- });
- findViewById(R.id.list).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showList();
- }
- });
- findViewById(R.id.longList).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showLongList();
- }
- });
- findViewById(R.id.singleChoice).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showSingleChoice();
- }
- });
- findViewById(R.id.multiChoice).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showMultiChoice();
- }
- });
- findViewById(R.id.customListItems).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showCustomList();
- }
- });
- findViewById(R.id.customView).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showCustomView();
- }
- });
- findViewById(R.id.themed).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showThemed();
- }
- });
- }
- private void showBasicNoTitle() {
- new MaterialDialog.Builder(this)
- .content(R.string.shareLocationPrompt)
- .positiveText(R.string.agree) // the default is 'Accept', this line could be left out
- .negativeText(R.string.disagree) // leaving this line out will remove the negative button
- .build()
- .show();
- }
- private void showBasic() {
- new MaterialDialog.Builder(this)
- .title(R.string.useGoogleLocationServices)
- .content(R.string.useGoogleLocationServicesPrompt)
- .positiveText(R.string.agree) // the default is 'Accept', this line could be left out
- .negativeText(R.string.disagree) // leaving this line out will remove the negative button
- .build()
- .show();
- }
- private void showBasicLongContent() {
- new MaterialDialog.Builder(this)
- .title(R.string.useGoogleLocationServices)
- .content(R.string.loremIpsum)
- .positiveText(R.string.agree) // the default is 'Accept', this line could be left out
- .negativeText(R.string.disagree) // leaving this line out will remove the negative button
- .build()
- .show();
- }
- private void showBasicIcon() {
- new MaterialDialog.Builder(this)
- .icon(R.drawable.ic_launcher)
- .title(R.string.useGoogleLocationServices)
- .content(R.string.useGoogleLocationServicesPrompt)
- .positiveText(R.string.agree) // the default is 'Accept', this line could be left out
- .negativeText(R.string.disagree) // leaving this line out will remove the negative button
- .build()
- .show();
- }
- private void showStacked() {
- new MaterialDialog.Builder(this)
- .title(R.string.useGoogleLocationServices)
- .content(R.string.useGoogleLocationServicesPrompt)
- .positiveText(R.string.speedBoost)
- .negativeText(R.string.noThanks)
- .build()
- .show();
- }
- private void showNeutral() {
- new MaterialDialog.Builder(this)
- .title(R.string.useGoogleLocationServices)
- .content(R.string.useGoogleLocationServicesPrompt)
- .positiveText(R.string.agree)
- .negativeText(R.string.disagree)
- .neutralText(R.string.more_info)
- .build()
- .show();
- }
- private void showCallbacks() {
- new MaterialDialog.Builder(this)
- .title(R.string.useGoogleLocationServices)
- .content(R.string.useGoogleLocationServicesPrompt)
- .positiveText(R.string.agree)
- .negativeText(R.string.disagree)
- .neutralText(R.string.more_info)
- .callback(new MaterialDialog.FullCallback() {
- @Override
- public void onPositive(MaterialDialog dialog) {
- Toast.makeText(getApplicationContext(), "Positive!", Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onNeutral(MaterialDialog dialog) {
- Toast.makeText(getApplicationContext(), "Neutral", Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onNegative(MaterialDialog dialog) {
- Toast.makeText(getApplicationContext(), "Negative…", Toast.LENGTH_SHORT).show();
- }
- })
- .build()
- .show();
- }
- private void showList() {
- new MaterialDialog.Builder(this)
- .title(R.string.socialNetworks)
- .items(R.array.socialNetworks)
- .itemsCallback(new MaterialDialog.ListCallback() {
- @Override
- public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
- Toast.makeText(getApplicationContext(), which + ": " + text, Toast.LENGTH_SHORT).show();
- }
- })
- .build()
- .show();
- }
- private void showLongList() {
- new MaterialDialog.Builder(this)
- .title(R.string.states)
- .items(R.array.states)
- .itemsCallback(new MaterialDialog.ListCallback() {
- @Override
- public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
- Toast.makeText(getApplicationContext(), which + ": " + text, Toast.LENGTH_SHORT).show();
- }
- })
- .build()
- .show();
- }
- private void showSingleChoice() {
- new MaterialDialog.Builder(this)
- .title(R.string.socialNetworks)
- .items(R.array.socialNetworks)
- .itemsCallbackSingleChoice(2, new MaterialDialog.ListCallback() {
- @Override
- public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
- Toast.makeText(getApplicationContext(), which + ": " + text, Toast.LENGTH_SHORT).show();
- }
- })
- .positiveText(R.string.choose)
- .build()
- .show();
- }
- private void showMultiChoice() {
- new MaterialDialog.Builder(this)
- .title(R.string.socialNetworks)
- .items(R.array.socialNetworks)
- .itemsCallbackMultiChoice(new Integer[]{1, 3}, new MaterialDialog.ListCallbackMulti() {
- @Override
- public void onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {
- StringBuilder str = new StringBuilder();
- for (int i = 0; i < which.length; i++) {
- str.append(which[i]);
- str.append(": ");
- str.append(text[i]);
- str.append('\n');
- }
- Toast.makeText(getApplicationContext(), str.toString(), Toast.LENGTH_LONG).show();
- }
- })
- .positiveText(R.string.choose)
- .build()
- .show();
- }
- private void showCustomList() {
- new MaterialDialog.Builder(this)
- .title(R.string.socialNetworks)
- .items(R.array.socialNetworks)
- .itemsCallback(new MaterialDialog.ListCallback() {
- @Override
- public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
- Toast.makeText(getApplicationContext(), which + ": " + text, Toast.LENGTH_SHORT).show();
- }
- })
- .itemProcessor(new ButtonItemProcessor(this))
- .build()
- .show();
- }
- EditText passwordInput;
- View positiveAction;
- private void showCustomView() {
- MaterialDialog dialog = new MaterialDialog.Builder(this)
- .title(R.string.googleWifi)
- .customView(R.layout.dialog_customview)
- .positiveText(R.string.connect)
- .negativeText(android.R.string.cancel)
- .callback(new MaterialDialog.Callback() {
- @Override
- public void onPositive(MaterialDialog dialog) {
- Toast.makeText(getApplicationContext(), "Password: " + passwordInput.getText().toString(), Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onNegative(MaterialDialog dialog) {
- }
- }).build();
- positiveAction = dialog.getActionButton(DialogAction.POSITIVE);
- passwordInput = (EditText) dialog.getCustomView().findViewById(R.id.password);
- passwordInput.addTextChangedListener(new TextWatcher() {
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {
- }
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- positiveAction.setEnabled(s.toString().trim().length() > 0);
- }
- @Override
- public void afterTextChanged(Editable s) {
- }
- });
- // Toggling the show password CheckBox will mask or unmask the password input EditText
- ((CheckBox) dialog.getCustomView().findViewById(R.id.showPassword)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- passwordInput.setInputType(!isChecked ? InputType.TYPE_TEXT_VARIATION_PASSWORD : InputType.TYPE_CLASS_TEXT);
- passwordInput.setTransformationMethod(!isChecked ? PasswordTransformationMethod.getInstance() : null);
- }
- });
- dialog.show();
- positiveAction.setEnabled(false); // disabled by default
- }
- private void showThemed() {
- new MaterialDialog.Builder(this)
- .title(R.string.useGoogleLocationServices)
- .content(R.string.useGoogleLocationServicesPrompt)
- .positiveText(R.string.agree)
- .negativeText(R.string.disagree)
- .positiveColorRes(R.color.material_red_400)
- .negativeColorRes(R.color.material_red_400)
- .titleAlignment(Alignment.CENTER)
- .titleColorRes(R.color.material_red_400)
- .theme(Theme.DARK)
- .build()
- .show();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.main, menu);
- return super.onCreateOptionsMenu(menu);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- if (item.getItemId() == R.id.about) {
- new MaterialDialog.Builder(this)
- .title(R.string.about)
- .positiveText(R.string.dismiss)
- .content(Html.fromHtml(getString(R.string.about_body)))
- .contentLineSpacing(1.6f)
- .callback(new MaterialDialog.SimpleCallback() {
- @Override
- public void onPositive(MaterialDialog dialog) {
- }
- })
- .build()
- .show();
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
|