EditText setOnCreateContextMenuListener handle result - android

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;
}
});
}

Related

It's showing unreachable statement error

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;
}

Context menu single click Android

I'm new to the Android development. I was trying to add a context menu to my app. I understood that by default it requires long click on the button to open the context menu. But I need to make it to appear on the single click. I tried all the other solutions here in stackoverflow but none of them are really helping me.
I have posted my code below. kindly let me know what are the modifications to be done to make it working.
public class ThirdActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third_layout);
confirmButton = (Button) findViewById(R.id.confirmButton);
registerForContextMenu(confirmButton);
}
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select Menu");
menu.add(0, v.getId(), 0, "Action 1");
}
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Action 1") {
//do something
}
}
just :
public class ThirdActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third_layout);
confirmButton = (Button) findViewById(R.id.confirmButton);
confirmButton .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
confirmButton .performLongClick();
}
});
registerForContextMenu(confirmButton);
}
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select Menu");
menu.add(0, v.getId(), 0, "Action 1");
}
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Action 1") {
//do something
}
}
You can simply write:
confirmButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
view.showContextMenu();
}
});

Contextual Action Bar in Webview text Selection

I found a lot of question about this problem, but I can't fix it. I am creating custom contextual action bars (CAB) within WebView i followed this Tutorial
My problem is CAB working fine in setOnLongClickListener but i cant able to select the text.
I referred these links:
Link 1, Link 2
Edit :
mywebview.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mActionMode = MainActivity.this.startActionMode(new ActionBarCallBack());
return true;
}
});
Implement the ActionMode.Callback interface:
class ActionBarCallBack implements ActionMode.Callback {
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
mode.getMenuInflater().inflate(R.menu.contextual_menu, menu);
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
}
I Tried like this :
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
mActionMode = Contents.this.startActionMode(new ActionBarCallBack());
}
Help me where i am wrong. Thank in advanced

android single item context menu

I have an onLongClickListener set up for a layout. On long click I need a context menu with singe "Delete" option. What is the most simple way to manage that? Thanks
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ....
mView = someView;
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Menu Title");
MenuItem remove = menu.add("Delete");
remove.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
//doStuff...
}
});
super.onCreateContextMenu(menu, v, menuInfo);
}
and in onLongPress or something you can call openContextMenu

How to make a conditional list item in a Android ContextMenu

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);

Categories

Resources