I tired some codes but I couldn't fix it. My manu.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=".MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_feedback"
app:showAsAction="never" />
How I can get a weblink in this xml? Thanks a lot.
Example for a ContextMenu
First override onCreateContextMenu:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(/*your menu id*/, menu);
}
Then override the onContextItemSelected an open a webview or the play store app:
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()){
case action_settings: {
//your code
return true;
}
default:
return super.onContextItemSelected(item);
}
}
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"/>
Here is the preparation code:
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.language_menu, menu);
MenuItem menuItem = menu.findItem(R.id.arabic);
if (UtilityPreferenceManager.getSelectedLanguage() == UtilityPreferenceManager.LanguageArabic) {
menuItem.setChecked(true);
} else {
menuItem.setChecked(false);
}
menuItem = menu.findItem(R.id.english);
if (UtilityPreferenceManager.getSelectedLanguage() == UtilityPreferenceManager.LanguageEnglish) {
menuItem.setChecked(true);
} else {
menuItem.setChecked(false);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
item.setChecked(true);
switch (item.getItemId()) {
case R.id.arabic:
if (UtilityPreferenceManager.getSelectedLanguage() != UtilityPreferenceManager.LanguageArabic) UtilityPreferenceManager.changeLanguage();
return true;
case R.id.english:
if (UtilityPreferenceManager.getSelectedLanguage() != UtilityPreferenceManager.LanguageEnglish) UtilityPreferenceManager.changeLanguage();
return true;
default:
return super.onContextItemSelected(item);
}
}
and here is the menu xml:
<group android:checkableBehavior="single">
<item android:id="#+id/arabic"
android:title="#string/arabic" />
<item android:id="#+id/english"
android:title="#string/english" />
</group>
The problem is that the menu always appears with English selected. I am sure that the language preference is saved correctly in preferences. In fact, the if condition is working fine, but it seems there is something overriding the selected menu item after finishing onCreateContextMenu
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 listview the fisrt time i when i click to row it open the context menu then i override the oncontextitemselected
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case CALL_ID:
{
AdapterContextMenuInfo info2 = (AdapterContextMenuInfo) item.getMenuInfo();
String phone=mDbHelper.getPhone(info2.id);
String toDial="tel:"+phone.toString();
startActivity(new Intent(Intent.ACTION_DIAL,Uri.parse(toDial)));
return true;
}
}
return super.onContextItemSelected(item);
}
this do correctly but when i
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case CALL_ID:
{
AdapterContextMenuInfo info2 = (AdapterContextMenuInfo) item.getMenuInfo();
String phone=mDbHelper.getPhone(info2.id);
String toDial="tel:"+phone.toString();
startActivity(new Intent(Intent.ACTION_DIAL,Uri.parse(toDial)));
return true;
}
return super.onMenuItemSelected(featureId, item);
}
}
the app crashed can u show me the difference between them
Try this:
context_menu.xml (res/menu/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>
Context Menu:
#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);
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
That should work.