I am trying to figure out an easy way for a user to select a word, preferably by long pressing on the word in a TextView. Basically, I have a TextView filled with text and I would like the user to have the ability to long press the word and then display a contextmenu so I can execute a database search? Is this possible? I can also switch to an EditText as long as I can make it look like a TextView. Make sense?
Thanks.
Very simple.
First create your TextView and registerForContextMenu():
private AdapterContextMenuInfo info;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView text = (TextView) findViewById(R.id.txtbtn);
text.setText("Click Me!");
registerForContextMenu(text);
}
Then build your ContextMenu:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
info = (AdapterView.AdapterContextMenuInfo)menuInfo;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.call:
String phone="555-555-555";
String toDial="tel:"+phone.toString();
Uri uri = Uri.parse(toDial);
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
return true;
default:
return super.onContextItemSelected(item);
}
}
context_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/call"
android:title="CALL" />
</menu>
Related
When I click a menu item, I want to generate a context menu with options for the user to select.
But its throwing null pointer exception.
# menu_main.xml #
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.bremachitra.mynotepad.MainActivity">
<item
android:id="#+id/action_view"
android:title="View"
app:actionViewClass="android.widget.ImageButton" />
</menu>
MainActivity.java
if(id == R.id.action_view)
{
ImageButton viewButton = (ImageButton) findViewById(R.id.action_view);
registerForContextMenu(viewButton);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
Toast.makeText(this,"context",Toast.LENGTH_SHORT).show();
getMenuInflater().inflate(R.menu.menu_view,menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.action_grid:
Toast.makeText(this,"grid view",Toast.LENGTH_SHORT).show();
return true;
case R.id.action_list:
Toast.makeText(this,"List view",Toast.LENGTH_SHORT).show();
return true;
default:
return super.onContextItemSelected(item);
}
your error is for imageview.
You cannot directly access a menu source.
You should use inflater(or menuInflater) or Menu object:
viewButton = (ImageButton) menu.findViewById(R.id.action_view);
This link is an example
put imageview in onCreateOptonsMenu() method and use menu parameter for findViewById:
viewButton = (ImageButton) menu.findViewById(R.id.action_view);
I have created a Context Floating Menu like this:
I added a header too (it is not shown in this picture).
It works perfect but i want to change :
Background
Color/drawable between the header and the first item
The color of the header
And other settings
Can someone show me an example in the styles.xml file how to change some of those settings?
EDIT: To be more specific i will show my code:
Here i register my view for the context menu:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListItemView demoItem1 = (ListItemView) findViewById(R.id.demoItem1);
registerForContextMenu(demoItem1);
}
Here i create and inflate the menu:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("List Actions");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
Here are the options if clicked:
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.dublicate:
return true;
case R.id.edit:
return true;
case R.id.delete:
return true;
case R.id.rename:
return true;
default:
return super.onContextItemSelected(item);
}
}
Here is the context_menu.xml :
<item
android:id="#+id/dublicate"
android:title="#string/context_menu_item_dublicate">
</item>
<item
android:id="#+id/edit"
android:title="#string/context_menu_item_edit"/>
<item
android:id="#+id/delete"
android:title="#string/context_menu_item_delete"/>
<item
android:id="#+id/rename"
android:title="#string/context_menu_item_rename"/>
I have a list view connected to a database, showing a all the entries. I want a menu to show up if the user long clicks a item in the listview(database entry), showing options to edit or delete the entry. how can i do this.
Till now, I have tried using a onItemLongClick listener and a toast in it showing the id of the view long clicked.
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
String res = Long.toString(id);
Toast toast = Toast.makeText(this, res, Toast.LENGTH_SHORT);
toast.show();
return true;
}
First you need to register your context menu on list view.
lv = (ListView) findViewById(R.id.list_view);
registerForContextMenu(lv);
Then, just override activity methods.
/**
* MENU
*/
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.list_view) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_list, menu);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.add:
// add stuff here
return true;
case R.id.edit:
// edit stuff here
return true;
case R.id.delete:
// remove stuff here
return true;
default:
return super.onContextItemSelected(item);
}
}
Here is an example of menu_list.xml file (you have to put the file in the res/menu folder).
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/add"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/menu_add" />
<item android:id="#+id/edit"
android:icon="#android:drawable/ic_menu_edit"
android:title="#string/menu_edit" />
<item android:id="#+id/delete"
android:icon="#android:drawable/my_icon_delete"
android:title="#string/menu_delete" />
</menu>
Hope it will help.
Instead of using onItemLongClick you can use
public void onCreateContextMenu(final ContextMenu menu,
final View v, final ContextMenuInfo menuInfo) {
...
}
where you setup the options for edit and delete or whatever you need to.
The actions for the item selected from the context menu can be processed in
public boolean onContextItemSelected(final MenuItem item)
For more information on context menu see here.
For a step by step tutorial visit here.
Edit
The second link is broken as it was quite old.
But I guess you can refer one of the other highly voted answer to see all the steps involved,
Another approach:
//Deleted individual cart items
//on list view cell long press
cartItemList.setOnItemLongClickListener (new OnItemLongClickListener() {
#SuppressWarnings("rawtypes")
public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) {
final CharSequence[] items = { "Delete" };
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Action:");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
cart = cartList.get(position);
db.removeProductFromCart(context, cart);
new AlertDialog.Builder(context)
.setTitle(getString(R.string.success))
.setMessage(getString(R.string.item_removed))
.setPositiveButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(CartDetailsActivity.this, HomeScreen.class);
startActivity(intent);
}
})
.show();
}
});
AlertDialog alert = builder.create();
alert.show();
//do your stuff here
return false;
}
});
You can call Activity.openOptionsMenu() in your click item method
check here
http://developer.android.com/reference/android/app/Activity.html#openOptionsMenu%28%29
**
after register your context menu on list view
**
override onCreateContextMenu Method like this
#Override
public void onCreateContextMenu(ContextMenu menu,View v, ContextMenu.ContextMenuInfo menuInfo){
if (v.getId() == R.id.listView){
AdapterView.AdapterContextMenuInfo info =(AdapterView.AdapterContextMenuInfo)menuInfo;
MenuItem mnu1=menu.add(0,0,0,"Delete");
MenuItem mnu2=menu.add(0,1,1,"Share");
}
}
then coding for each item that can be selected
#Override
public boolean onContextItemSelected(MenuItem menuItem){
AdapterView.AdapterContextMenuInfo info=(AdapterView.AdapterContextMenuInfo)menuItem.getMenuInfo();
switch (menuItem.getItemId()) {
case 0:
Toast.makeText(this, "Delete Selected", Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(this, "Share Selected", Toast.LENGTH_LONG).show();
break;
default:
break;
}
return true;
}
Use registerForContextMenu(); to link context menu to any View successor.
To access to selected ListItem data, simple use the AdapterView.AdapterContextMenuInfo.
E.g.:
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo menuinfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
long selectid = menuinfo.id; //_id from database in this case
int selectpos = menuinfo.position; //position in the adapter
switch (item.getItemId()) {
case 1:
doSomething(selectid);
break;
case 2:
doSomethingElse(selectpos);
}
return super.onContextItemSelected(item);
}
A quick note for those still struggling, there're two methods
registerForContextMenu(list);
unregisterForContextMenu(list);
Make sure you pick the first.
I'm doing the code in the same way i've always done it & cant see where I am wrong:
#Override
public void onCreate(){
...
this.registerForContextMenu(lv);
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu_device_item_remove, menu);
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/context_menu_item_remove_id"
android:title="Remove" />
<item android:id="#+id/context_menu_item_clear_all_id"
android:title="Clear all" />
</menu>
And as you can see.. effect is:
App crashes at click on third or forth element because of use of AdapterContextMenuInfo.position:
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
System.out.println("## info.position: "+info.position);
...
Have you ever encountered this? & How did you escaped it?
Apparently BUG was due to having some 2 call of this.registerForContextMenu(lv); (first one in supper class) .. so yeah, my bad.
try the following code:
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(0, MENU_ITEM_SEND_MSG, 0, "Send a Message");
menu.add(0, MENU_ITEM_MAKE_A_CALL, 0, "Make a Call");
}
public boolean onContextItemSelected(MenuItem item) {
SharedPreferences server_sp = getApplicationContext()
.getSharedPreferences("server", MODE_PRIVATE);
String server = server_sp.getString("Server", "server");
switch (item.getItemId()) {
case MENU_ITEM_SEND_MSG:
//do ur stuff
case MENU_ITEM_MAKE_A_CALL:
//do ur stufff
break;
}
return false;
}
I have a button which opens a context menu with a list of various options. Only one option can be selected at a time so I want to have a radio button next to each of them highlighting which item is currently selected. When I select an item from the context menu the radio button is selected and closes. Once I click the button to open the context menu the previously selected item is not selected.
How do I get the context menu to remember which item was previously selected?
Secondly, when the activity is first created a default option is selected. How do I set an initial default which can then be overwritten when another context menu item is selected? I can set android:checked="true" in the XML but can this be overwritten when a different item is selected?
Java:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browse);
Button buttonView = (Button) this.findViewById(R.id.button_view);
buttonView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
registerForContextMenu(v);
openContextMenu(v);
}
});
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle(R.string.menu_title);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case(R.id.item1):
if(item.isChecked()) {
item.setChecked(false);
} else {
item.setChecked(true);
}
break;
case(R.id.item2):
if(item.isChecked()) {
item.setChecked(false);
} else {
item.setChecked(true);
}
break;
case(R.id.item3):
if(item.isChecked()) {
item.setChecked(false);
} else {
item.setChecked(true);
}
break;
default:
return super.onContextItemSelected(item);
}
return true;
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="#+id/context_menu" android:checkableBehavior="single">
<item android:id="#+id/item1"
android:title="#string/context_menu_item1" />
<item android:id="#+id/item2"
android:title="#string/context_menu_item2" />
<item android:id="#+id/item3"
android:title="#string/context_menu_item3" />
</group>
</menu>
Any assistance would be greatly appreciated!
The problem is that your items don't save the state between displaying of menu.
So each time you call setChecked, it works only for the currently shown menu and resets for the next.
You should save the checked state in an external structure like a boolean array for example.
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle(R.string.menu_title);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
// loop for menu items
for (int i = 0; i < menu.size(); ++i) {
MenuItem mi = menu.getItem(i);
// check the Id as you wish
if (mi.getItemId() == R.id.item2) {
mi.setChecked(true);
}
}
}
In order to search a menu item, you can also use the findItem function:
MenuItem mi = menu.findItem(R.id.item2)
Well, you have to save your user input somewhere and set the checked states explicitly after inflating the layout.
By the way, you could write your item selected switch like so:
switch (item.getItemID()) {
case R.id.item1:
case R.id.item2:
case R.id.item3:
item.setChecked(!item.isChecked());
}