Wednesday, December 1, 2010

Android dialog , components simple

public static void prepareForMongolianKeyboard(final Context ctx, EditText editText) {
        editText.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Toast.makeText(ctx, "' ! , ? ' ?????????? ??? ' ? , ? ' ?????? ??", Toast.LENGTH_SHORT);
            }
        });
        editText.getEditableText().setFilters(new InputFilter[]{new InputFilter() {
            public CharSequence filter(CharSequence charSequence
                    , int i, int i1, Spanned spanned, int i2, int i3) {

                if (charSequence.equals("!")) {
                    return Resources.getChars(0);
                }
                if (charSequence.equals("?")) {
                    return Resources.getChars(1);
                }
                return null;
            }
        }});
    }

    public static void prepareForSearchFromList(final CheckableListViewAdapter listViewAdapter, EditText editText) {
        editText.addTextChangedListener(new TextWatcher() {

            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                listViewAdapter.getFilter().filter(charSequence);
            }

            public void afterTextChanged(Editable editable) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
    }

    public static void prepareForWebView(WebView webView) {
        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                if (progress >= 100) {
                } else {
                }
            }
        });
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(false);
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webSettings.setDefaultTextEncodingName(StaticConfigs.DEFAULT_CHARSET);
        webSettings.setDefaultFontSize(13);
    }

    public static void showAlert(Context ctx, String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setMessage(message)
                .setNeutralButton(NewsLib.Resources.getString(R.string.caption_ok), null).show();
    }

    public static void showConfirm(Context ctx, String message, final OnDialogResultListener listener) {
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                if (listener != null) {
                    listener.onDialogResult(null, which);
                }
            }
        };
        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setMessage(message)
                .setPositiveButton(NewsLib.Resources.getString(R.string.caption_yes), dialogClickListener)
                .setNegativeButton(NewsLib.Resources.getString(R.string.caption_no), dialogClickListener)
                .show();
    }

    public static void prepareForSpinner(Spinner spinner, String[] data, AdapterView.OnItemSelectedListener listener, int selection) {

        ArrayAdapter adapter = new ArrayAdapter(spinner.getContext()
                , android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        for (String item : data) {
            NewsSiteItemData site = new NewsSiteItemData();
            site.setText(item);
            adapter.add(site);
        }
        adapter.notifyDataSetChanged();

        spinner.setAdapter(adapter);
        if (selection >= 0) {
            spinner.setSelection(selection);
        }
        spinner.setOnItemSelectedListener(listener);
    }

    public static interface OnDialogResultListener {
        void onDialogResult(Object data, int which);
    }



usage 

super.onCreate(savedInstanceState);

        setContentView(R.layout.test);

        String[] arr = MyResources.getStringArray(R.array.planets_array);
        MyLib.prepareForSpinner((Spinner)findViewById(R.id.spinner), arr, new MyOnItemSelectedListener(), 1); 


public class MyOnItemSelectedListener implements AdapterView.OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,
                                   View view, int pos, long id) {
            Toast.makeText(parent.getContext(), "The planet is " +
                    parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
        }

        public void onNothingSelected(AdapterView parent) {
            // Do nothing.
        }
    }

No comments: