Error: Unknown Member Menu - android

I am trying to create a popup menu, but i am getting this error, saying "unknown member 'menu' of com.popup.trial.R
What should I do?
my xml file:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/one"
android:title="One"/>
<item
android:id="#+id/two"
android:title="Two"/>
</menu>
and my java:
#Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(MainActivity.this, settings2);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();
}
});

The R class is code-generated for you by the Android build tools when you build your project.
Clean your project or Build your project
reference : Beginner Android why is "id" is "Unknown member" with R.id?
follow this link for other example. : https://www.javatpoint.com/android-popup-menu-example

Related

Popup menu appears blank on screen

I am using a popup menu in my android application whenever the button is clicked the popup menu appears blank I have inflated a menu layout file with the popup menu. The actions are triggered but the popup menu appears to be blank. here's the screenshot of popup menu.
My adapter code:
holder.more.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(context, v);
popupMenu.inflate(R.menu.expense_history_menu);
popupMenu.show();
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.Edit:
Toast.makeText(context, "Edit clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.Delete:
spent_by.remove(position);
category.remove(position);
desc.remove(position);
date.remove(position);
share_by.remove(position);
notifyItemRemoved(position);
helper.delete_spent_history(s_id.get(position));
tot_amt = helper.get_trip_total_amt(t_id);
helper.update_total_amt(t_id, tot_amt);
update_due_amt();
return true;
default:
return false;
}
}
});
}
});
My expense_history_menu layout file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/Edit"
android:title="Edit"/>
<item
android:id="#+id/Delete"
android:title="Delete"/>
</menu>
Help me to solve this problem.
Thanks in advance!
Paste this in style.xml
and make changes as required.
<style name="PopupMenu" parent="AppTheme">
<item name="android:popupBackground">#color/colorPrimary</item>
<item name="android:textColor">#color/colorAccent</item>
</style>
Don't forget to add style="#style/PopupMenu" in expense_history_menu

How to achieve a popup like facebook provided in below image? Tried with aleart dialog

I want to a pop up like this is facebook
Hello Guys,
Above is the image where you can see a popup comes over a button. I tried achieving this using AleartDialog but it opens in center. I want it below that button only.
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_show_options, null);
new AlertDialog.Builder(this)
.setView(view)
.create().show();
Any help would be appreciated. Thanks
Use Popup menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/unfriend"
android:icon="#drawable/ic_mail"
android:title="Unfriend" />
<item android:id="#+id/edit_friend_list"
android:icon="#drawable/ic_upload"
android:title="Edit FriendList"
android:showAsAction="ifRoom" />
</menu>
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_example, popup.getMenu());
popup.show();
}
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.unfriend:
//
return true;
case R.id.edit_friend_list:
return true;
default:
return false;
}
}
Hope it will help.
for more detail please visit below link.
https://www.tutlane.com/tutorial/android/android-popup-menu-with-examples
https://www.javatpoint.com/android-popup-menu-example
http://www.coderzheaven.com/2013/04/07/create-simple-popup-menu-android/
Use Pop Up menu
open menu on your button click
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/mail"
android:icon="#drawable/ic_mail"
android:title="#string/mail" />
<item android:id="#+id/upload"
android:icon="#drawable/ic_upload"
android:title="#string/upload"
android:showAsAction="ifRoom" />
<item android:id="#+id/share"
android:icon="#drawable/ic_share"
android:title="#string/share" />
</menu>
Java Code:
public void showMenu(View v) {
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.actions);
popup.show();
}
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.archive:
archive(item);
return true;
case R.id.delete:
delete(item);
return true;
default:
return false;
}
}

How to set spinner dropdown list position in android?

I want to place a dropdown list on the right. Can I do this in a standard spinner?
I think Image represented is PopUp menu, To achieve the same see the code below,
To define popup menu, we need to create a new folder menu inside of our project resource directory (res/menu/) and add a new XML (popup_menu.xml) file to build the menu.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/first_item"
android:title="first" />
<item android:id="#+id/second_item"
android:title="second" />
<item android:id="#+id/third_item"
android:title="third" />
</menu>
In your Activity,
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v, Gravity.RIGHT);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popup.getMenu());
popup.show();
}
Now Override onMenuItemClick to provide functions to each item.
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.first_item:
// do your code
return true;
case R.id.second_item:
// do your code
return true;
case R.id.third_item:
// do your code
return true;
default:
return false;
}

Android ListView with context menu

Does anyone know any library, how to achieve this or any sorcery has been made for this.
a list view with context menu? I don't want to use the one with long click context menu.
Thanks
Just Create menu.xml in res/menu folder Like below example
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_item1" android:title="#string/menu_item1"/ >
<item android:id="#+id/menu_item2" android:title="#string/menu_item2" >
<!-- "file" submenu -->
<menu>
<item android:id="#+id/sub_menu_item1"
android:title="#string/sub_menu_item1" />
<item android:id="#+id/sub_menu_item2"
android:title="#string/sub_menu_item2" />
</menu>
Create an image Button like bellow example in your layout
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_overflow_holo_dark"
android:contentDescription="#string/descr_overflow_button"
android:onClick="showPopup" />
Create Method that display your popup-menu.
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
// action is your menu.xml file
// This activity implements OnMenuItemClickListener
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.actions);
popup.show();
}
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
// your menu id and perform action
case R.id.archive:
archive(item);
return true;
case R.id.delete:
delete(item);
return true;
default:
return false;
}
}
and you can follow This tutorial
It's called a PopupMenu and can be placed anywhere. The documentation is here: http://developer.android.com/reference/android/widget/PopupMenu.html
Normally you would have an ImageButton with an overflow image resource and set the on click listener to display the PopupMenu using the ImageButton as the anchor view.
here is sample of CARDLIB
Please take a look you 'll found solution
HERE IS CODE

How to create a custom PopupMenu in Android

How can I replicate something like I made below in Balsamiq?
I made this menu, but it is only displaying the text of the items (not the icons). Is it possible to display both the title and icon in a PopupMenu?
Here is my create_post_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_photo"
android:icon="#drawable/ic_action_camera"
android:title="#string/action_photo"
android:showAsAction="always|withText" />
<item
android:id="#+id/action_video"
android:icon="#drawable/ic_action_video"
android:title="#string/action_video"
android:showAsAction="always|withText" />
<item
android:id="#+id/action_text"
android:icon="#drawable/ic_action_edit"
android:title="#string/action_text"
android:showAsAction="always|withText" />
<item
android:id="#+id/action_link"
android:icon="#drawable/ic_action_web_site"
android:title="#string/action_link"
android:showAsAction="always|withText" />
</menu>
Edit
Here are my onCreateOptionsMenu and onOptionsItemSelected methods:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_new) {
View menuItemView = findViewById(R.id.action_new);
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.inflate(R.menu.create_post_menu);
popupMenu.show();
return true;
} else if(item.getItemId() == R.id.action_search) {
return true;
} else if(item.getItemId() == R.id.action_settings) {
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
return true;
} else if(item.getItemId() == R.id.action_help) {
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
I resolved this issue by simply putting the create_post_menu inside of the item whose icon is a +.
For example, I have (using AppCompat):
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_new"
android:icon="#drawable/ic_action_new"
android:title="#string/action_new"
app:showAsAction="always">
<menu>
<item
android:id="#+id/action_photo"
android:icon="#drawable/ic_action_camera"
android:title="#string/action_photo"
app:showAsAction="always|withText" />
<item
android:id="#+id/action_video"
android:icon="#drawable/ic_action_video"
android:title="#string/action_video"
app:showAsAction="always|withText" />
<item
android:id="#+id/action_text"
android:icon="#drawable/ic_action_text"
android:title="#string/action_text"
app:showAsAction="always|withText" />
<item
android:id="#+id/action_place"
android:icon="#drawable/ic_action_place"
android:title="#string/action_place"
app:showAsAction="always|withText" />
<item
android:id="#+id/action_more"
android:title="#string/action_more"
android:visible="false"
app:showAsAction="always|withText" />
</menu>
</item>
...(more menu items here)
</menu>
Without AppCompat, you could just get rid of the XML Namespace app by replacing app with android.
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;
public class MainActivity extends Activity {
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);//your created butto
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu
}
});//closing the setOnClickListener method
}
}
I hope my previous answer Here can help you.
If you just want a similar popup menu, you can use ActionProvider. It's more powerful.
If you want it as a true menu, you can use custom PopupMenu.

Categories

Resources