I am developing an application in which i want to show a one custom menu item which shows count and rest menu items are default type of item as shown in image below.
My code of menu.xml is as below:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/menu_1"
android:icon="#drawable/icon_1"
android:orderInCategory="101"
android:title="#string/option_1"
myapp:showAsAction="always"/>
<item
android:id="#+id/menu_2"
android:icon="#drawable/icon_2"
android:orderInCategory="102"
android:title="#string/option_2"
myapp:showAsAction="always"/>
<item
android:id="#+id/menu_3"
android:icon="#drawable/icon_3"
android:orderInCategory="103"
android:title="#string/option_3"
myapp:showAsAction="always"/>
<item
android:id="#+id/menu_4"
android:icon="#drawable/icon_4"
android:orderInCategory="104"
android:title="#string/option_4"
myapp:showAsAction="always"/>
<item
android:id="#+id/menu_5"
android:orderInCategory="105"
android:title="#string/option_5"
myapp:showAsAction="never"
android:actionLayout="#layout/actionbar_badge_layout" />
</menu>
My code of actionbar_badge_layout.xml is :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:id="#+id/actionbar_badge_layout" >
<TextView
android:id="#+id/actionbar_notifcation_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:padding="10dp"
android:text="#string/option_1"
android:textColor="#android:color/black"
android:layout_toLeftOf="#+id/actionbar_notifcation_count_textview"/>
<TextView
android:id="#+id/actionbar_notifcation_count_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="10dp"
android:text="99"
android:textColor="#android:color/white"
android:background="#drawable/badge_circle" />
</RelativeLayout>
In my fragment what i done is :
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_myfragment, container,false);
setHasOptionsMenu(true);
((SlidingBaseActivity) getActivity()).setActionBarTitle("My Fragment");
((SlidingBaseActivity) getActivity()).keepMenuClosed(false);
((SlidingBaseActivity) getActivity()).makeActionOverflowMenuShown();
initView(view);
dialog = Helper.showPleaseWaitDialog(getActivity());
getdata();
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.findItem(R.id.option_1).setVisible(true);
menu.findItem(R.id.option_1).setOnMenuItemClickListener(this);
menu.findItem(R.id.option_2).setVisible(true);
menu.findItem(R.id.option_2).setOnMenuItemClickListener(this);
menu.findItem(R.id.option_3).setVisible(true);
menu.findItem(R.id.option_3).setOnMenuItemClickListener(this);
menu.findItem(R.id.option_4).setVisible(true);
menu.findItem(R.id.option_4).setOnMenuItemClickListener(this);
menu.findItem(R.id.option_5).setVisible(true);
menu.findItem(R.id.option_5).setOnMenuItemClickListener(this);
RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.option_5).getActionView();
TextView textViewMenuText = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview);
textViewMenuText.setText("Notification ");
TextView textViewCount = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_count_textview);
textViewCount.setText("99");
super.onCreateOptionsMenu(menu, inflater);
}
But i am getting null pointer exception when i navigate to the respective fragment.
I follow the max voted answer from the this link.
What i am missing or doing wrong? Please guide me.
In your onCreateOptionsMenu you are trying this:
RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.option_5).getActionView();
The 'id' to the item is wrong here. You have set the id as 'menu_5' and trying to fetch with 'option_5'. Which I see is exactly your 'title' to the menu item.
Related
I want to make an app with a custom toolbar with a menu on the toolbar. The problem I have is that when I run the app and I click on the menu on the right side of the toolbar, the options do not appear but the menu box appears. One of the options closes the app and if I click the menu area where the exit option is the app closes.
Here is my activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/toolbar_color"
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:title="#string/app_name"
app:logo="#drawable/applogo">
</androidx.appcompat.widget.Toolbar>
Here is my mainActivity.java:
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if(id == R.id.exit)
{
this.finishAffinity();
}
return true;
}
}
This is my menu.xml file:
<?xml version="1.0" encoding="utf-8"?>
<item
android:title="#string/new_entry"
android:id="#+id/add">
</item>
<item
android:title="#string/about"
android:id="#+id/about">
</item>
<item
android:title="#string/exit"
android:id="#+id/exit">
</item>
I created the toolbar xml I set it in MainActivity .. I created the menu xml and I inflate it in MainActivity from all the tutorials these are the steps that I had to go through to get here, yet I don't know why the items in the menu xml file won't show up in the emulator.
Big thanks to Gabriele Mariotti. In his words: " The issue is the text color in the popup menu." .All I had to do was to was to add the following line of code to the Toolbar xml.
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
Try this one I have made a demo for you it is working I have tested it on the emulator as well as the device.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/teal_700"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Hello World!" />
</RelativeLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return true;
}
}
my_menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/item1"
android:title="Item 1"
app:showAsAction="never" />
<item
android:id="#+id/item2"
android:title="Item 2"
app:showAsAction="never" />
<item
android:id="#+id/item3"
android:title="Item 3"
app:showAsAction="never" />
<item
android:id="#+id/item4"
android:title="Item 4"
app:showAsAction="never" />
</menu>
This question already has answers here:
How to make an overflow menu like in Chrome?
(2 answers)
Closed 4 years ago.
How to create menu items aligned horizontally in the first row like google chrome
I created the first item with a menu child but by this way i got a row that send me to the other menu, So how to do that using menu file ? i don't want to use Dialog
Here is my menu file :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="#+id/toolbar_menu"
android:visible="true">
<item
android:id="#+id/item_tabs"
android:icon="#drawable/ic_tabs_24dp"
android:title="#null"
app:showAsAction="always"/>
<item
android:id="#+id/icon_row_menu_id"
android:title="#null"
android:visible="true">
<menu>
<item
android:id="#+id/item_back"
android:icon="#drawable/ic_arrow_back_24dp"
android:title="#string/go_back" />
<item
android:id="#+id/item_forward"
android:icon="#drawable/ic_arrow_forward_24dp"
android:title="#string/go_forward" />
<item
android:id="#+id/item_bookmark_page"
android:icon="#drawable/ic_star_border_24dp"
android:title="#string/bookmark_this_page" />
<item
android:id="#+id/item_download_page"
android:icon="#drawable/ic_download_24dp"
android:title="#string/download_page" />
<item
android:id="#+id/item_info"
android:icon="#drawable/ic_info_outline_24dp"
android:title="#string/view_site_information" />
</menu>
</item>
<item
android:id="#+id/item_new_tab"
android:title="#string/new_tab" />
<item
android:id="#+id/item_new_incognito_tab"
android:title="#string/new_incognito_tab" />
<item
android:id="#+id/item_bookmarks"
android:title="#string/bookmarks" />
<item
android:id="#+id/item_history"
android:title="#string/history" />
<item
android:id="#+id/item_downloads"
android:title="#string/download" />
<item
android:id="#+id/item_settings"
android:title="#string/settings" />
</group>
</menu>
And here how i use it :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
mainMenu = menu;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
You can use app:actionLayout="#layout/layout_name" property of item tag like
<item
android:id="#+id/menu_id"
android:title="#string/title"
app:showAsAction="never"
app:actionLayout="#layout/layout_name"/>
and your layout_name is any layout file like
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
// add your horizontal menu item here
</LineearLayout>
and you can access this item
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem item = menu.findItem(R.id.menu_id);
Linearlayout rootView = (LinearLayout)item.getActionView();
YourControlClass control = (YourControlClass)
rootView.findViewById(R.id.control_id);
return true;
}
you can also try with PopUp window this may help you check above codes
popup.xml layout file not menu,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/cardview_light_background"
android:layout_margin="20dp">
<LinearLayout
android:layout_margin="5dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
//your images with horizontal
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
// your vertical list view
</LinearLayout>
</LinearLayout>
and you can Add the following code to the onClick() method:
LayoutInflater layoutInflater
= (LayoutInflater) context
.getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
ActionMenuView.LayoutParams.WRAP_CONTENT,
ActionMenuView.LayoutParams.WRAP_CONTENT, true);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
View parent = view.getRootView();
popupWindow.showAtLocation(parent, Gravity.TOP|Gravity.END,0,0);
I am selecting multiple item in listview for delete . I can delete multiple item . The code is as follows :
smsListView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
// Capture total checked items
final int checkedCount = smsListView.getCheckedItemCount();
// Set the CAB title according to total checked items
mode.setTitle(checkedCount + " Selected");
View v = smsListView.getChildAt(position
- smsListView.getFirstVisiblePosition());
// Calls toggleSelection method from ListViewAdapter Class
((SmsArrayAdapter) arrayAdapter).toggleSelection(position,v);
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.delete:
// Calls getSelectedIds method from ListViewAdapter Class
SparseBooleanArray selected = ((SmsArrayAdapter) arrayAdapter)
.getSelectedIds();
// Captures all selected ids with a loop
for (int i = (selected.size() - 1); i >= 0; i--) {
if (selected.valueAt(i)) {
SMSItem selecteditem = (SMSItem) arrayAdapter.getItem(selected.keyAt(i));
// Remove selected items following the ids
arrayAdapter.remove(selecteditem);
}
}
// Close CAB
mode.finish();
return true;
default:
return false;
}
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
((SmsArrayAdapter) arrayAdapter).removeSelection();
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
});
But I want to change color of the rows that I have selected . Currently there are no color on selection item in the list .
I have tried the following .
Step no 1: write below line to to your listview item layout
android:background="?android:attr/activatedBackgroundIndicator"
Step no 2: write below line to style.xml file
<style name="AppTheme" parent="#style/Theme.AppCompat.Light">
<item name="actionBarStyle">#style/MyActionBar</item>
<item name="android:activatedBackgroundIndicator">#drawable/muliple_selected_item</item>
</style>
Step no 3: Create muliple_selected_item.xml into Drawable folder and write below code.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="#color/lvMultipleSelectionColor" />
<item android:state_checked="true" android:drawable="#color/lvMultipleSelectionColor" />
<item android:state_pressed="true" android:drawable="#color/lvMultipleSelectionColor" />
<item android:drawable="#android:color/transparent" />
</selector>
But by this code , all itemsof the list are colored when I select any item of the listview . I want to change background color to those items only which I have selected .
How can I do this ?
The listview layoput is as follows :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/smsItemContainerRelativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal|top"
android:text="SMS Inbox"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/unread_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="-20dp"
android:background="#drawable/notification_bg"
android:gravity="center"
android:text="88"
android:textColor="#android:color/background_light"
android:textSize="20sp"
android:textStyle="bold"
android:layout_alignParentRight="true"
android:visibility="invisible" />
</RelativeLayout>
<ListView
android:id="#+id/SMSList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
</LinearLayout>
The rows layout is as follows :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/muliple_selected_item" >>
<TextView
android:id="#+id/textView_from"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SMS From"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView_sms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="SMS : " />
<TextView
android:id="#+id/textView_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="Time : " />
</LinearLayout>
</LinearLayout>
The muliple_selected_item.xml is as follows : .
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="#color/lvMultipleSelectionColor" />
<item android:state_checked="true" android:drawable="#color/lvMultipleSelectionColor" />
<item android:state_pressed="true" android:drawable="#color/lvMultipleSelectionColor" />
<item android:drawable="#android:color/transparent" />
</selector>
You need to set Multiple Select Choice mode to your ListView
smsListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
You need to create Drawable file for your ListView's row Layout and use Drawable as background in that layout.
Here is Drawable file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/menuitem_press_background_color" android:state_activated="true" />
<item android:drawable="#color/white" />
</selector>
EDIT:
remove changeBackgroundColor() used in selectView() and check if it works
You can refer this Selecting multiple items in ListView. it might help you
Just put the line below in your single row xml file(the xml file in which you define the define the layout for single row of listview):-
android:background="?android:attr/activatedBackgroundIndicator"
And no need to edit any more xml files. For more details, please visit the link below :-
Click here to go to link ...!
Hope this helps :)
You can use drawables for that like below,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_activated="true"
android:drawable="#android:color/darker_gray" />
</selector>
and set it as background of your row layout,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_item_background_activated" >
</RelativeLayout>
You can Achieve it in this way:-
First add property(variable) isSelected to SMSItem class with default Value false.
on onItemCheckedStateChanged method Execution set value for variable isSelected.
on getView method of Adapter you have to chech value for isSelected variable. then make a decison base on it, if false then set default
background and if true the set another background to perticular row.
call notifyDataSetChanged() method on Adepter from onItemCheckedStateChanged.
on Your Request, Im posting Code:-
add this to onItemCheckedStateChanged method
SMSItem selecteditem = (SMSItem) arrayAdapter.getItem(selected.keyAt(i));
selecteditem.setSelection(checked);
notifydatasetChanged();
and manage getView yourself
How I can force menu item to show under overflow icon. I am providing app support from api level 8 and above. for now it is showing as actionview .
My menuLayout is as follows.
//main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_menu_setting"
android:title="#string/menuItemSetting"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_signout"
android:title="#string/menuItemLogout"
app:showAsAction="ifRoom"/>
</menu>
in Java class
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
I have tried with other options of showAsAction but none of them worked. Please suggest me how I can show above two menu itme under overflow icon(when i click on over these two will appearer as actionlist.)
layout for custom action bar
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#drawable/header"
android:orientation="vertical" >
<TextView
android:id="#+id/tvActionBarTitle"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:gravity="center"
android:textColor="#FFFFFF"
android:textSize="30sp" />
<Button.../>
<!-- your code for button -->
</LinearLayout>
You can call below function in onCreate and define button in this function as I have defined appName text view and its OnClick event
private void createCutomActionBarTitle()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
{
this.getActionBar().setHomeButtonEnabled(true);
}
this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
this.getActionBar().setBackgroundDrawable(drawable);
this.getActionBar().setIcon(logo);
LayoutInflater inflator = LayoutInflater.from(this);
View v = inflator.inflate(R.layout.custom_action_bar, null);
((TextView) v.findViewById(R.id.tvActionBarTitle)).setText(Global.ACTIVITY_NAME);
this.getActionBar().setCustomView(v);
}
Hope this will be useful for you :)
But clicking physical button of device will not trigger this button you have to look for it, as I don't have any idea about it
Could you post java code ? Do you have code below in your activity ?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.menu, menu);;
return super.onCreateOptionsMenu(menu);
}
Edit :
I alway use code like this(when i click action_settings it will show action list contains setting and bar, i think just replace android:title="#string/action_settings" with android:icon)
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/action_settings">
<menu>
<item
android:enabled="true"
android:visible="true"
android:title="setting"
android:id="#+id/setting">
</item>
<item
android:enabled="true"
android:visible="true"
android:title="B"
android:id="#+id/bar">
</item>
</menu>
</item>
<item
android:id="#+id/action_settings1"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/action_settings"/>
</menu>
Hope it helps
I want to create a customized menu and put that menu in a specific layout that is in the center of the RelativeLayout, i tried to make it like that but didn't work:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Home" >
<LinearLayout
android:id="#+id/grid1"
android:layout_width="750dp"
android:layout_height="400dp"
android:background="#android:color/darker_gray"
android:layout_centerInParent="true"
android:orientation="vertical" >
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/Main"
android:title="#string/main"/>
<item android:id="#+id/Sub"
android:title="#string/sub"/>
</menu>
</LinearLayout>
</RelativeLayout>
in Java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//RelativeLayout layout = (RelativeLayout) findViewById(R.layout.activity_home);
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
P.s I also tried to make it in a separate file then add it but when doing that it only shown in the actionbar.
It is not possible to use menu directly into your layout. Instead create a menu layout in res/menu folder. And then inflate it with Pop up menu listener inside any button click event. There is a fine example of it in the following link.
You can also Use PopMenuWindow class to use customized layout as popUpMenu.
http://www.javatpoint.com/android-popup-menu-example
Try following steps:
1) create menu.xml in menu folder(/res/menu/menu.xml)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_setting_menu"
android:title="#string/menu_setting"
app:showAsAction="collapseActionView"/>
<item
android:id="#+id/action_logout"
android:title="Logout"
app:showAsAction="collapseActionView"/>
</menu>
2) create function showMenu(view) in Activity class
private void showMenu(View view){
PopupMenu popupMenu = new PopupMenu(Activity.this, view);//View will be an anchor for PopupMenu
popupMenu.inflate(R.menu.menu);
Menu menu = popupMenu.getMenu();
popupMenu.setOnMenuItemClickListener(this);
popupMenu.show();
}
3) set Anchor for menu ,where you want to show PopupMenu
LinearLayout layout = findViewById(R.id.grid1);//You can use other view as anchor
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showMenu(view);// call function to show PopupMenu
}
});
Put android:showAsAction="never" in menu items to show item in menu.
like
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/Main"
android:title="#string/main"
android:showAsAction="never"/>
<item android:id="#+id/Sub"
android:title="#string/sub"
android:showAsAction="never"/>
</menu>
put menu layout in menu folder (subfolder of res)