Example usage for android.view View.OnCreateContextMenuListener View.OnCreateContextMenuListener

List of usage examples for android.view View.OnCreateContextMenuListener View.OnCreateContextMenuListener

Introduction

In this page you can find the example usage for android.view View.OnCreateContextMenuListener View.OnCreateContextMenuListener.

Prototype

View.OnCreateContextMenuListener

Source Link

Usage

From source file:com.dattasmoon.pebble.plugin.ConvertPreference.java

@Override
protected void onBindDialogView(View view) {
    Button btnAdd = (Button) view.findViewById(R.id.btnAdd);
    etFrom = (EditText) view.findViewById(R.id.etFrom);
    etTo = (EditText) view.findViewById(R.id.etTo);
    lvConvert = (ListView) view.findViewById(R.id.lvConvert);
    lvConvert.setEmptyView(view.findViewById(android.R.id.empty));
    lvConvert.setAdapter(arrayAdapter);/*from  www  . j  a va  2  s  .  c om*/
    lvConvert.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
        @Override
        public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
            AdapterView.AdapterContextMenuInfo contextInfo = (AdapterView.AdapterContextMenuInfo) menuInfo;
            int position = contextInfo.position;
            long id = contextInfo.id;
            // the child view who's info we're viewing (should be equal to v)
            View v = contextInfo.targetView;
            AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());

            final int arrayPosition = (Integer) v.getTag();
            final String text = ((TextView) v.findViewById(R.id.tvItem)).getText().toString();
            builder.setMessage(
                    getContext().getResources().getString(R.string.confirm_delete) + " '" + text + "' ?")
                    .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            JSONArray temp = new JSONArray();
                            for (int i = 0; i < arrayAdapter.getJSONArray().length(); i++) {
                                if (i == arrayPosition) {
                                    continue;
                                }
                                try {
                                    temp.put(arrayAdapter.getJSONArray().getJSONObject(i));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                            arrayAdapter.setJSONArray(temp);

                            arrayAdapter.notifyDataSetChanged();
                        }
                    }).setNegativeButton(R.string.decline, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // User cancelled the dialog
                        }
                    });
            builder.create().show();
        }
    });

    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            JSONObject item = new JSONObject();
            try {
                item.put("from", etFrom.getText().toString());
                item.put("to", etTo.getText().toString());
                arrayAdapter.getJSONArray().put(item);
                etFrom.setText("");
                etTo.setText("");
                arrayAdapter.notifyDataSetChanged();
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    });
    super.onBindDialogView(view);
}

From source file:com.dattasmoon.pebble.plugin.IgnorePreference.java

@Override
protected void onBindDialogView(View view) {

    btnAdd = (Button) view.findViewById(R.id.btnAdd);
    etMatch = (EditText) view.findViewById(R.id.etMatch);
    chkRawRegex = (CheckBox) view.findViewById(R.id.chkRawRegex);
    chkCaseInsensitive = (CheckBox) view.findViewById(R.id.chkCaseInsensitive);
    actvApplications = (AutoCompleteTextView) view.findViewById(R.id.actvApplications);

    spnApplications = (Spinner) view.findViewById(R.id.spnApplications);
    spnMode = (Spinner) view.findViewById(R.id.spnMode);
    lvIgnore = (ListView) view.findViewById(R.id.lvIgnore);
    lvIgnore.setAdapter(arrayAdapter);//from  www  .  j a  v a 2  s .  c om
    lvIgnore.setEmptyView(view.findViewById(android.R.id.empty));
    new LoadAppsTask().execute();

    lvIgnore.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
        @Override
        public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
            AdapterView.AdapterContextMenuInfo contextInfo = (AdapterView.AdapterContextMenuInfo) menuInfo;
            int position = contextInfo.position;
            long id = contextInfo.id;
            // the child view who's info we're viewing (should be equal to v)
            final View v = contextInfo.targetView;
            MenuInflater inflater = new MenuInflater(getContext());
            inflater.inflate(R.menu.preference_ignore_context, menu);

            //we have to do this mess because DialogPreference doesn't allow for onMenuItemSelected or onOptionsItemSelected. Bleh
            menu.findItem(R.id.btnEdit).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    final int arrayPosition = (Integer) v.getTag();
                    final String text = ((TextView) v.findViewById(R.id.tvItem)).getText().toString();
                    JSONArray temp = new JSONArray();
                    for (int i = 0; i < arrayAdapter.getJSONArray().length(); i++) {
                        try {
                            JSONObject ignore = arrayAdapter.getJSONArray().getJSONObject(i);
                            if (i == arrayPosition) {
                                etMatch.setText(ignore.getString("match"));
                                chkRawRegex.setChecked(ignore.getBoolean("raw"));
                                chkCaseInsensitive.setChecked(ignore.optBoolean("insensitive", true));
                                String app = ignore.getString("app");
                                if (app == "-1") {
                                    actvApplications.setText(getContext().getString(R.string.ignore_any));
                                } else {
                                    actvApplications.setText(app);
                                }
                                boolean exclude = ignore.optBoolean("exclude", true);
                                if (exclude) {
                                    spnMode.setSelection(Constants.IgnoreMode.EXCLUDE.ordinal());
                                } else {
                                    spnMode.setSelection(Constants.IgnoreMode.INCLUDE.ordinal());
                                }
                                continue;
                            }

                            temp.put(ignore);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    arrayAdapter.setJSONArray(temp);

                    arrayAdapter.notifyDataSetChanged();
                    return true;
                }
            });
            menu.findItem(R.id.btnDelete).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());

                    final int arrayPosition = (Integer) v.getTag();
                    final String text = ((TextView) v.findViewById(R.id.tvItem)).getText().toString();
                    builder.setMessage(getContext().getResources().getString(R.string.confirm_delete) + " '"
                            + text + "' ?")
                            .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    JSONArray temp = new JSONArray();
                                    for (int i = 0; i < arrayAdapter.getJSONArray().length(); i++) {
                                        if (i == arrayPosition) {
                                            continue;
                                        }
                                        try {
                                            temp.put(arrayAdapter.getJSONArray().getJSONObject(i));
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                    arrayAdapter.setJSONArray(temp);

                                    arrayAdapter.notifyDataSetChanged();
                                }
                            }).setNegativeButton(R.string.decline, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    // User cancelled the dialog
                                }
                            });
                    builder.create().show();
                    return true;
                }
            });
        }
    });
    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            JSONObject item = new JSONObject();
            try {
                item.put("match", etMatch.getText().toString());
                item.put("raw", chkRawRegex.isChecked());
                item.put("insensitive", chkCaseInsensitive.isChecked());
                if (actvApplications.getText().toString()
                        .equalsIgnoreCase(getContext().getString(R.string.ignore_any))) {
                    item.put("app", "-1");
                } else {
                    item.put("app", actvApplications.getText().toString());
                }
                if (spnMode.getSelectedItemPosition() == Constants.IgnoreMode.INCLUDE.ordinal()) {
                    item.put("exclude", false);
                } else {
                    item.put("exclude", true);
                }
                if (Constants.IS_LOGGABLE) {
                    Log.i(Constants.LOG_TAG, "Item is: " + item.toString());
                }
                arrayAdapter.getJSONArray().put(item);
                etMatch.setText("");
                arrayAdapter.notifyDataSetChanged();
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    });
    actvApplications.setText(getContext().getString(R.string.ignore_any));
    actvApplications.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            actvApplications.showDropDown();
        }
    });
    actvApplications.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ApplicationInfo pkg = (ApplicationInfo) parent.getItemAtPosition(position);
            if (pkg == null) {
                actvApplications.setText(getContext().getString(R.string.ignore_any));
            } else {
                actvApplications.setText(pkg.packageName);
            }
        }
    });
    actvApplications.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                actvApplications.showDropDown();
            } else {
                if (actvApplications.getText().length() == 0) {
                    actvApplications.setText(getContext().getString(R.string.ignore_any));
                }
            }
        }
    });
    super.onBindDialogView(view);

}

From source file:com.juick.android.UserCenterActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    JuickAdvancedApplication.setupTheme(this);
    handler = new Handler();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.user_center);
    final ListView list = (ListView) findViewById(R.id.list);
    final View listWait = findViewById(R.id.list_wait);
    final TextView userRealName = (TextView) findViewById(R.id.user_realname);
    final ImageView userPic = (ImageView) findViewById(R.id.userpic);
    final TextView userName = (TextView) findViewById(R.id.username);
    search = findViewById(R.id.search);//from  www  .  j  ava 2s .  c  o m
    final View stats = findViewById(R.id.stats);
    userRealName.setText("...");
    Bundle extras = getIntent().getExtras();
    if (extras == null) {
        finish();
        return;
    }
    uname = extras.getString("uname");
    final int uid = extras.getInt("uid");

    final MessageID mid = (MessageID) extras.getSerializable("mid");
    final MessagesSource messagesSource = (MessagesSource) extras.getSerializable("messagesSource");
    if (uname == null || mid == null) {
        finish();
        return;
    }
    int height = getWindow().getWindowManager().getDefaultDisplay().getHeight();
    final int userpicSize = height <= 320 ? 32 : 96;
    float scaledDensity = getResources().getDisplayMetrics().scaledDensity;
    userPic.setMinimumHeight((int) (scaledDensity * userpicSize));
    userPic.setMinimumWidth((int) (scaledDensity * userpicSize));
    stats.setEnabled(false);
    userName.setText("@" + uname);
    final boolean russian = Locale.getDefault().getLanguage().equals("ru");
    new Thread() {
        @Override
        public void run() {
            final Utils.RESTResponse json = Utils.getJSON(UserCenterActivity.this,
                    "http://" + Utils.JA_ADDRESS + "/api/userinfo?uname=" + Uri.encode(uname), null);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    stats.setEnabled(true);
                    if (json.getErrorText() != null) {
                        Toast.makeText(UserCenterActivity.this, "JA server: " + json.getErrorText(),
                                Toast.LENGTH_LONG).show();
                        listWait.setVisibility(View.GONE);
                    } else {
                        final UserInfo userInfo = new Gson().fromJson(json.getResult(), UserInfo.class);
                        if (userInfo == null) {
                            Toast.makeText(UserCenterActivity.this, "Unable to parse JSON", Toast.LENGTH_LONG)
                                    .show();
                            listWait.setVisibility(View.GONE);
                        } else {
                            userRealName.setText(userInfo.fullName);
                            listWait.setVisibility(View.GONE);
                            list.setVisibility(View.VISIBLE);
                            list.setAdapter(new BaseAdapter() {
                                @Override
                                public int getCount() {
                                    return userInfo.getExtraInfo().size();
                                }

                                @Override
                                public Object getItem(int position) {
                                    return userInfo.getExtraInfo().get(position);
                                }

                                @Override
                                public long getItemId(int position) {
                                    return position;
                                }

                                @Override
                                public View getView(int position, View convertView, ViewGroup parent) {
                                    if (convertView == null) {
                                        convertView = getLayoutInflater().inflate(R.layout.listitem_userinfo,
                                                null);
                                    }
                                    TextView text = (TextView) convertView.findViewById(R.id.text);
                                    TextView text2 = (TextView) convertView.findViewById(R.id.text2);
                                    String info = userInfo.getExtraInfo().get(position);
                                    int ix = info.indexOf("|");
                                    if (ix == -1) {
                                        text.setText(info);
                                        if (russian && UserInfo.translations.containsKey(info)) {
                                            info = UserInfo.translations.get(info);
                                        }
                                        text2.setText("");
                                    } else {
                                        String theInfo = info.substring(0, ix);
                                        if (russian && UserInfo.translations.containsKey(theInfo)) {
                                            theInfo = UserInfo.translations.get(theInfo);
                                        }
                                        text.setText(theInfo);
                                        String value = info.substring(ix + 1);
                                        if (value.startsWith("Date:")) {
                                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                            value = value.substring(5);
                                            value = sdf.format(new Date(Long.parseLong(value)));
                                        }
                                        text2.setText(value);
                                    }
                                    return convertView;
                                }
                            });
                        }
                    }
                }
            });
        }

    }.start();

    View subscribe_user = findViewById(R.id.subscribe_user);
    View unsubscribe_user = findViewById(R.id.unsubscribe_user);
    View subscribe_comments = findViewById(R.id.subscribe_comments);
    View unsubscribe_comments = findViewById(R.id.unsubscribe_comments);
    View filter_user = findViewById(R.id.filter_user);
    View blacklist_user = findViewById(R.id.blacklist_user);
    View show_blog = findViewById(R.id.show_blog);
    MicroBlog microBlog = MainActivity.getMicroBlog(mid.getMicroBlogCode());
    final MessageMenu mm = microBlog.getMessageMenu(this, messagesSource, null, null);
    JuickMessage message = microBlog.createMessage();
    mm.listSelectedItem = message;
    message.User = new JuickUser();
    message.User.UName = uname;
    message.User.UID = uid;
    message.setMID(mid);
    final UserpicStorage.AvatarID avatarID = microBlog.getAvatarID(message);

    final UserpicStorage.Listener userpicListener = new UserpicStorage.Listener() {
        @Override
        public void onUserpicReady(UserpicStorage.AvatarID id, int size) {
            final UserpicStorage.Listener thiz = this;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    UserpicStorage.instance.removeListener(avatarID, userpicSize, thiz);
                    final Bitmap userpic = UserpicStorage.instance.getUserpic(UserCenterActivity.this, avatarID,
                            userpicSize, thiz);
                    userPic.setImageBitmap(userpic); // can be null
                }
            });
        }
    };
    Bitmap userpic = UserpicStorage.instance.getUserpic(this, avatarID, userpicSize, userpicListener);
    userPic.setImageBitmap(userpic); // can be null

    subscribe_user.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mm.actionSubscribeUser();
        }
    });
    show_blog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mm.listSelectedItem.User.UID == 0) {
                JuickMicroBlog.obtainProperUserIdByName(UserCenterActivity.this, mm.listSelectedItem.User.UName,
                        "Getting Juick User Id", new Utils.Function<Void, Pair<String, String>>() {
                            @Override
                            public Void apply(Pair<String, String> cred) {
                                mm.listSelectedItem.User.UID = Integer.parseInt(cred.first);
                                mm.actionUserBlog();
                                return null;
                            }
                        });
            } else {
                mm.actionUserBlog();
            }
        }
    });
    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            search.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
                @Override
                public void onCreateContextMenu(ContextMenu menu, View v,
                        ContextMenu.ContextMenuInfo menuInfo) {
                    menu.add(0, SEARCH_PAST_CONVERSATIONS, 0, "My dialogs with user");
                    menu.add(0, SEARCH_MORE, 1, "More");
                }
            });
            search.showContextMenu();
        }
    });
    stats.setOnClickListener(new View.OnClickListener() {

        NewJuickPreferenceActivity.MenuItem[] items = new NewJuickPreferenceActivity.MenuItem[] {
                new NewJuickPreferenceActivity.MenuItem(R.string.UserAllTimeActivityReport,
                        R.string.UserAllTimeActivityReport2, new Runnable() {
                            @Override
                            public void run() {
                                NewJuickPreferenceActivity.showChart(UserCenterActivity.this,
                                        "USER_ACTIVITY_VOLUME", "uid=" + uid);
                            }
                        }),
                new NewJuickPreferenceActivity.MenuItem(R.string.UserHoursReport, R.string.UserHoursReport2,
                        new Runnable() {
                            @Override
                            public void run() {
                                NewJuickPreferenceActivity.showChart(UserCenterActivity.this,
                                        "USER_HOURS_ACTIVITY", "uid=" + uid + "&tzoffset="
                                                + TimeZone.getDefault().getRawOffset() / 1000 / 60 / 60);
                            }
                        }) };

        @Override
        public void onClick(View v) {
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    items[position].action.run();
                }
            });
            list.setAdapter(new BaseAdapter() {
                @Override
                public int getCount() {
                    return items.length;
                }

                @Override
                public Object getItem(int position) {
                    return items[position];
                }

                @Override
                public long getItemId(int position) {
                    return position;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    LayoutInflater layoutInflater = getLayoutInflater();
                    View listItem = layoutInflater.inflate(android.R.layout.simple_list_item_2, null);
                    TextView text = (TextView) listItem.findViewById(android.R.id.text1);
                    text.setText(items[position].labelId);
                    TextView text2 = (TextView) listItem.findViewById(android.R.id.text2);
                    text2.setText(items[position].label2Id);
                    return listItem;
                }
            });
        }
    });
    blacklist_user.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mm.actionBlacklistUser();
        }
    });
    filter_user.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mm.actionFilterUser(uname);
        }
    });
    unsubscribe_user.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mm.actionUnsubscribeUser();
        }
    });
    subscribe_comments.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            enableJAM(new Runnable() {
                @Override
                public void run() {
                    JAMService.instance.client.subscribeToComments(uname);
                }
            });
        }
    });
    unsubscribe_comments.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            enableJAM(new Runnable() {
                @Override
                public void run() {
                    JAMService.instance.client.unsubscribeFromComments(uname);
                }
            });
        }
    });
}