I'm trying to make a toast on item selected but it's showing unreachable statement error.
My code is:
btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.performLongClick();
}
}
);
registerForContextMenu(btn);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("For Optimum Results");
menu.add(0,v.getId(),0,"Hi");
menu.add(0,v.getId(),0,"Hello");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
if(item.getTitle()=="Hi"){
Toast.makeText(this,"hi",Toast.LENGTH_SHORT).show();
}
return true;
}
you have two type of error:
1)return super (return super.onContextItemSelected(item)) , it means that you return your method at first line so lines below not execute.
2)wrong comparing String (item.getTitle()=="Hi")
correct code should be like this:
#Override
public boolean onContextItemSelected(MenuItem item) {
//return super.onContextItemSelected(item);//remove this line
if(item.getTitle().equal("Hi")){// also maybe you want to check not null for item.getTitle()
Toast.makeText(this,"hi",Toast.LENGTH_SHORT).show();
}
return true;
}
Related
I have set setOnCreateContextMenuListener to edittext. onCreateContextMenu method is called user long press on edittext. and it opens context menu with 'done' and 'copy' options.
But my question is how can I handle when user select done option or copy options?
Can I get any event when user click on done button or copy button. so I can get selected text via clip manager?
edit.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
Log.i("TAG", "onCreateContextMenu");//it is printing while context menu is created.
}
});
Thanks.
onContextItemSelected is the event you should use. but it has no View argument to access selected item. Here is a trick to access to selected View.
public class MainActivity extends Activity
{
protected View selectedView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView)findViewById(R.id.lv1);
registerForContextMenu(lv);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
this.selectedView = v;
super.onCreateContextMenu(menu, v, menuInfo);
}
#Override
public boolean onContextItemSelected(MenuItem item)
{
switch(item.getId())
{
case R.id.Context_Edit:
// access to view with this.selectedView
break;
case R.id.Context_Delete:
// access to view with this.selectedView
break;
default:
return super.onContextItemSelected(item);
}
return true;
}
}
You can get your both button event like following example
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView)findViewById(R.id.lv1);
registerForContextMenu(lv);
}
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId() == R.id.lv1)
{
menu.setHeaderTitle("Select Action");
menu.add(0, v.getId(), 0, "Edit");
menu.add(0, v.getId(), 0, "Delete");
}
}
#Override
public boolean onContextItemSelected(MenuItem item)
{
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
if(item.getTitle() == "Edit")
{
// do something
}
if(item.getTitle() == "Delete")
{
// do something
}
return true;
}
Hope it will help you.
You must to override the onLongClick of the editText and setting one CAB. This is example:
private ActionMode.Callback action = new ActionMode.Callback() {
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (item.getItemId() == android.R.id.copy){
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
Log.i("CAB",String.valueOf(clipboard.getPrimaryClip().getItemAt(0).getText()));
}
return true;
}
};
private EditText edtText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtText = (EditText) findViewById(R.id.edtText1);
registerForContextMenu(edtText);
edtText.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
((EditText)v).setTextIsSelectable(true);
((EditText)v).setCustomSelectionActionModeCallback(action);
return false;
}
});
}
I'm trying to use a floating context menu and I wonder if it's possible to activate this menu, by pressing the image in the ImageView?
My first problem is how to handle registerForContextMenu and the ImageView? I searched and find most examples with GridView and ListViews.
I have made the menu in xml and I should I use this method in the activity with a switch:
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
}
Just like the others, you get your View in onCreateContextMenu, based on that you inflate the menu for the proper item.
registerForContextMenu(imageView);
The method above expects any View class.
Each time you call registerForContextMenu() for a different View, onCreateContextMenu() will be called for you to handle the proper menu creation.
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId == R.id.youtImageView) {
getMenuInflater().inflate(R.menu.image_menu, menu);
}
}
Based on the item id you decide for which View the menu was clicked. You must me sure the id's of menu items for different views are not the same.
When the item from a context menu is clicked, you will receive the onContextItemSelected() callback with MenuItem that was clicked
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.image_menu_item_do_something:
doSOmething();
return true;
default:
return super.onContextItemSelected(item);
}
}
Make sure that you have these methods in onCreate:
ImageView image = (ImageView) findViewById(R.id.image_view);
registerForContextMenu(image);
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openContextMenu(image);
}
});
And in context_menu_main.xml, that it looks similar to this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_option"
android:title="#string/action_option_text" />
</menu>
Finally, you'll need to override these two methods as follows:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.context_menu_main, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_option:
Log.e("TAG", "Option");
return true;
default:
return super.onContextItemSelected(item);
}
}
I have a context menu on a ListView.
I would like to add a special-case context list item for logged in users.
Let's say there is a list of comments. But ONLY for the logged in user's comments, there is a special context list-item called "Edit" (Obviously you don't want other users to be able to edit comments outside of their own.
Here is my class extending application in which I usually check in for logged in users:
public class MyApp extends Application {
public static boolean isUserLoggedIn = false;
public static String username = null;
public static SharedPreferences logInState;
public static int ratescreen = 0;
public static boolean userLogin() {
return MyApp.isUserLoggedIn = true;
}
public static boolean userLogout() {
return MyApp.isUserLoggedIn = false;
}
public static void setUser(String s) {
MyApp.username = s;
}
#Override
public void onCreate() {
super.onCreate();
String PREFS_NAME = "LoginState";
logInState = getSharedPreferences(PREFS_NAME,
MODE_PRIVATE);
}
}
Here is my context menu:
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.reviews_context, menu);
menu.setHeaderTitle("Mark Comment as ...");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.helpful:
new HelpfulTask().execute();
Toast.makeText(getActivity(), "You have voted this up!",
Toast.LENGTH_SHORT).show();
return true;
case R.id.unhelpful:
new UnHelpfulTask().execute();
Toast.makeText(getActivity(), "You have voted this down!",
Toast.LENGTH_SHORT).show();
return true;
case R.id.spam:
new SpamTask().execute();
Toast.makeText(getActivity(),
"You have reported this as Spam or Offensive.",
Toast.LENGTH_SHORT).show();
return true;
// Would like to add fourth option here but conditional if it is a comment from the currently logged in user.
}
return false;
}
Simple you add your Edit item depend on user login status in onCreateContextMenu
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
// check if user logged-in so add Edit item to context menu.
if (userLogin()) {
menu.add(0, MENU_ITEM_EDIT, 0, R.string.menu_edit);
}
//Add normall others menu items
menu.add(0, MENU_ITEM_CALL, 0, R.string.menu_callContact);
}
You can cast ContextMenuInfo to AdapterView.AdapterContextMenuInfo for ListView than change its content accessing to adapter (your own implementation).
AdapterView.AdapterContextMenuInfo contextMenuInfo = (AdapterView.AdapterContextMenuInfo)menuInfo;
MyAdapter adapter = (MyAdapter)getListView().getAdapter();
adapter.remove(contextMenuInfo.position);
I having some difficulty implementing a context menu into my android application. My first problem was I was trying to implement OnCreateContextMenu inside of OnCreate but I kept getting an error saying:
void is an invalid type for the variable onCreateContextMenu
I fixed this problem by putting onCreateContextMenu outside of OnCreate. Now my problem lies with OnContextItemSelected. My error occurs on the line: public boolean onContextItemSelected(MenuItem menu). The errors are:
implements android.view.View.OnLongClickListener.onLongClick
Syntax error, insert "}" to complete MethodBody
Here is the code:
BaconStripsButton.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
boolean onContextItemSelected(MenuItem item)
{
if (item.itemId() = 0)
{
Toast ringtone = Toast.makeText(startingPoint.this, "Ringtone added Successfully!", Toast.LENGTH_SHORT);
return true;
}
return false;
}
}
});
Any help would be appreciated. Thanks, Justin
No need to use onContextItemSelected Inside onlongClick of button.Just Override OnContextItemSelected(); and register ContextMenu to btn.No need to setOnlongClickListener.
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
Then override
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
editNote(info.id);
return true;
case R.id.delete:
deleteNote(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}
then finally registerContextMenu(button);
You may use the ListView and implement the onCreateContextMenu in the OnCreate of the Activity.
like this:
myList.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
// add some sublist
menu.setHeaderTitle(R.string.collect_title);
menu.add(0, 1, 0, R.string.delete_string);
menu.add(0, 2, 0, R.string.move_to_project_string);
menu.add(0, 3, 0, R.string.move_to_action_string);
}
});
Ok, so now after reading I do actually have a menu on long press like I wanted...the only problem is that it doesn't actually get the sound file and save it
I am wondering what did I do wrong now? Here is the code I used:
Button SoundButton1 = (Button) findViewById(R.id.money);
registerForContextMenu(SoundButton1);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Save as...");
menu.add(0, MENU_RINGTONE, 0, "Ringtone");
menu.add(0, MENU_NOTIFICATION, 0, "Notification");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Ringtone"){function1(item.getItemId());}
else if(item.getTitle()=="Notification"){function2(item.getItemId());}
else {return false;}
return true;
}
public void function1(int id){
Toast.makeText(this, "Ringtone saved", Toast.LENGTH_SHORT).show();
}
public void function2(int id){
Toast.makeText(this, "Notification saved", Toast.LENGTH_SHORT).show();
}
You are not comparing Strings properly. This test:
if(item.getTitle()=="Ringtone")
checks if the object returned by getTitle() is the same object as "Ringtone". This will always be false. Similarly with the else if test. You actually want to compare the value of getTitle with your String. You should replace them with the String#equals() method. Here is the code:
if(item.getTitle().equals("Ringtone")){function1(item.getItemId());}
else if(item.getTitle().equals("Notification")){function2(item.getItemId());}
else {return false;}
If you are using a ListFragment then you also need to do
getListView().setOnCreateContextMenuListener(this);
instead of
registerForContextMenu(listView);
it seems.