Search in sources :

Example 1 with DictItem

use of com.foobnix.pdf.info.DictsHelper.DictItem in project LibreraReader by foobnix.

the class DialogTranslateFromTo method show.

public static AlertDialog show(final Activity a, final Runnable runnable) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(a);
    alertDialog.setTitle(R.string.remember_dictionary);
    View body = LayoutInflater.from(a).inflate(R.layout.dialog_translate_from_to, null, false);
    final Spinner spinnerFrom = (Spinner) body.findViewById(R.id.spinnerFrom);
    final Spinner spinnerTo = (Spinner) body.findViewById(R.id.spinnerTo);
    ImageView imageOk = (ImageView) body.findViewById(R.id.imageOK);
    final List<String> langNames = new ArrayList<String>(langs.keySet());
    final List<String> langCodes = new ArrayList<String>(langs.values());
    spinnerFrom.setAdapter(new BaseItemLayoutAdapter<String>(a, android.R.layout.simple_spinner_dropdown_item, langNames) {

        @Override
        public void populateView(View inflate, int arg1, String value) {
            Views.text(inflate, android.R.id.text1, "" + value);
        }
    });
    spinnerFrom.setSelection(langCodes.indexOf(AppState.get().fromLang));
    spinnerTo.setAdapter(new BaseItemLayoutAdapter<String>(a, android.R.layout.simple_spinner_dropdown_item, langNames) {

        @Override
        public void populateView(View inflate, int arg1, String value) {
            Views.text(inflate, android.R.id.text1, "" + value);
        }
    });
    spinnerTo.setSelection(langCodes.indexOf(AppState.get().toLang));
    final ListView dictSpinner = (ListView) body.findViewById(R.id.dictionaries);
    final List<DictItem> list = DictsHelper.getAllResolveInfoAsDictItem1(a, "");
    list.addAll(DictsHelper.getOnlineDicts(a, ""));
    dictSpinner.setAdapter(new BaseItemLayoutAdapter<DictItem>(a, R.layout.item_dict_line, list) {

        @Override
        public void populateView(View layout, int position, DictItem item) {
            ((TextView) layout.findViewById(R.id.text1)).setText(item.name);
            ((TextView) layout.findViewById(R.id.type1)).setText(item.type);
            if (item.image == null) {
                ((ImageView) layout.findViewById(R.id.image1)).setImageResource(R.drawable.web);
            } else {
                ((ImageView) layout.findViewById(R.id.image1)).setImageDrawable(item.image);
            }
        }
    });
    body.findViewById(R.id.onReverse).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            int p1 = spinnerFrom.getSelectedItemPosition();
            int p2 = spinnerTo.getSelectedItemPosition();
            spinnerTo.setSelection(p1);
            spinnerFrom.setSelection(p2);
        }
    });
    alertDialog.setView(body);
    alertDialog.setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    final AlertDialog show = alertDialog.show();
    dictSpinner.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            AppState.get().rememberDict = list.get(position).toString();
            AppState.get().fromLang = langCodes.get(spinnerFrom.getSelectedItemPosition());
            AppState.get().toLang = langCodes.get(spinnerTo.getSelectedItemPosition());
            try {
                show.dismiss();
            } catch (Exception e) {
            }
            runnable.run();
        }
    });
    imageOk.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            AppState.get().fromLang = langCodes.get(spinnerFrom.getSelectedItemPosition());
            AppState.get().toLang = langCodes.get(spinnerTo.getSelectedItemPosition());
            try {
                show.dismiss();
            } catch (Exception e) {
            }
        }
    });
    return show;
}
Also used : AlertDialog(android.app.AlertDialog) OnItemClickListener(android.widget.AdapterView.OnItemClickListener) DialogInterface(android.content.DialogInterface) Spinner(android.widget.Spinner) ArrayList(java.util.ArrayList) ImageView(android.widget.ImageView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) ListView(android.widget.ListView) OnClickListener(android.view.View.OnClickListener) DictItem(com.foobnix.pdf.info.DictsHelper.DictItem) ImageView(android.widget.ImageView)

Aggregations

AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 AdapterView (android.widget.AdapterView)1 OnItemClickListener (android.widget.AdapterView.OnItemClickListener)1 ImageView (android.widget.ImageView)1 ListView (android.widget.ListView)1 Spinner (android.widget.Spinner)1 TextView (android.widget.TextView)1 DictItem (com.foobnix.pdf.info.DictsHelper.DictItem)1 ArrayList (java.util.ArrayList)1