Settings menu is created in activity_settingsmenu.xml .
Settings icon is in activity_settingsicon.xml .
How to link both the activities so that on clicking settings icon, the settings menu will be displayed?
(Just started learning Android Application development - beginner)
create menu folder in res.Create xml for e.g menu_main.xml under menu folder.
<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="in.nfnlabs.stormit.Parent">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
If u want to add icon, use this:
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:icon="#drawable/bookmark"
android:orderInCategory="100"
app:showAsAction="always" />
In your activity class inflate the menu like below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.addnew, menu);
return true;
}
If you want to perform actions in menu this can be achieved by onOptionsItemSelected
public boolean onOptionsItemSelected(MenuItem item) {
int i = item.getItemId();
if(i==R.id.action_settings) {
Toast.makeText(getApplicationContext(), "Bookmark", Toast.LENGTH_SHORT).show();
}
}
Related
I use navigation drawer in my app, set from right to left. I want it to only open when I press the options menu button is pressed(3 dots).
I tried this thread
Open navigation drawer when options menu button is pressed
but not working.
menu.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:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
When you are pressing the 3 dots android will reveal the menu items that are collapsed.
If you want to open the drawer from a menu item, I suggest you find an icon representing the 3 dots (or keep the drawer like icon) and:
Menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/ic_options"
android:icon="#drawable/ic_menu_dots"
android:showAsAction="always" />
</menu>
Code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.ic_options:
mDrawerLayout.openDrawer(your view);
return true;
}
return super.onOptionsItemSelected(item);
}
Like Surrender Kumar said, you have to open the drawer manually but from onOptionsItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
mDrawerLayout.openDrawer(your view);
return true;
}
return super.onOptionsItemSelected(item);
}
You need to put showAsAction=ifRoom on your menu and an icon:
<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:title="#string/action_settings"
android:icon="#drawable/action_settings"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
</menu>
I need to inflate custom menu in Fragment.
I have only one menu item.But the icon is not displaying.
Can someone tell what is wrong with my code
My menu.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" >
<item
android:id="#+id/search"
android:icon="#android:drawable/ic_search_category_default"
app:showAsAction="always"
android:title="Search"/></menu>
And I set in onCreateView()
setHasOptionsMenu(true);
getActivity().invalidateOptionsMenu();
And inflating the menu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.menu, menu);
}
Resulting screen attached below. I need to have the search icon instead of the menu overflow icon.
I know I'm a little late for the party, but hope I can help someone else. Today I was facing this SAME problem.
I fixed using android:showAsAction="always" instead of app:showAsAction="always"
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/bluetooth_status_off"
android:orderInCategory="0"
android:icon="#drawable/bluetooth_red"
android:title="#string/app_name"
android:showAsAction="always" />
</menu>
on the showAsAction is underlined with red (warning), but works fine.
I Think You should use
<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" >
<item
android:id="#+id/search"
android:icon="#drawable/ic_search_category_default"
app:showAsAction="always"
android:title="Search"/></menu>
and
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mymenu, menu);
return true;
}
Sorry to late but put this code in your onCreate()
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
You have to add the following property app:showAsAction="ifRoom"
Make sure, you've added a menu item without an icon.
Example:
<?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/next_btn"
android:checked="true"
android:icon="#android:drawable/ic_input_add"
android:title="Item"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_settings"
android:title="Settings" />
</menu>
Thanks to action_settings, it'll automatically add an overflow icon.
make a menu with the code below...
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_search"
android:title="Search"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>
and set it as your menu..
This is default behaviour of the Overflow menu from the ActionBar.
If you want to show the Icons in the overflow menu, override the onMenuOpened method in your activity with this code snippit:
#Override
public boolean onMenuOpened(int featureId, Menu menu) {
if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
if(menu.getClass().getSimpleName().equals("MenuBuilder")){
try{
Method m = menu.getClass().getDeclaredMethod(
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
}
catch(NoSuchMethodException e){
Log.e("MyActivity", "onMenuOpened", e);
}
catch(Exception e){
throw new RuntimeException(e);
}
}
}
return super.onMenuOpened(featureId, menu);
}
If you're running your code on Android 3.0+, the icons in the menu are not shown by design.
This is a design decision by Google.
You can read more about this on Android developers blog.
Copy your menu xml code.
Delete your menu file
Now create a new menu file in res->menu directory
And name it with different name
Now Run
I have an action bar that puts everything in a menu in the top right, which the user clicks and the menu options open up.
I inflate the action bar menu with this on each activity I use it:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main2, menu);
return true;
}
And my xml for main2.xml is:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_searchHome"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Seach"/>
</menu>
My question is do I put an onclick in the item in the xml and if so where do I put the onclick method it calls? Do I need to put it in every activity I launch this action bar in?
If you add an onClick attribute on your menu item like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_searchHome"
android:orderInCategory="100"
android:showAsAction="never"
android:onClick="doThis"
android:title="Seach"/>
</menu>
Then in your activity:
public void doThis(MenuItem item){
Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
}
Note:
ActionBarSherlock is deprecated. Unless you are developing an app for Android 4.0 or older, please don't use it. But if you are using the library, you will have to import
import com.actionbarsherlock.view.MenuItem;
and not
import com.android.view.MenuItem;
In addition, you could do something like this: ActionBar Sherlock Menu Item OnClick
which #adneal mentions.
In my opinion
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onCreateDialog(getTaskId());
}
});
}
<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:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="#+id/add_text_id" android:title="Add"
android:icon="#drawable/ic_add_btn"
android:orderInCategory="100" app:showAsAction="ifRoom" />
I have menu file like
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_select_all"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_select_all"/>
<item
android:id="#+id/action_deselect_all"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_deselect_all"
android:visible="false"/>
</menu>
and i want to show only one at a time, when i click on one, other show be invisible. can we not get activity's menu by any method of activity.
In onCreateOptionsMenu(Menu menu)
after inflate do this
if (CONDITION) {
MenuItem item = menu.findItem(R.id.action_select_all);
item.setVisible(false);
}
else
{
MenuItem item = menu.findItem(R.id.action_deselect_all);
item.setVisible(false);
}
Make sure that you call invalidateOptionsMenu(); when you need to refresh menu
I have created a menu which is activated on clicking the menu button on the device however it is showing as a list
My in res/menu/menu.xml code is as follows:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menuRefresh"
android:icon="#drawable/ic_menu_refresh"
android:title="Refresh"/>
<item android:id="#+id/menuAbout"
android:icon="#drawable/ic_menu_info_details"
android:title="About"/>
</menu>
and in my main activity i have:
//Initiate Menu XMl file
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return true;
}
/**
* Even handling for individual menu items selected
* Identity single menu item by its id
*/
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.menuRefresh:
Toast.makeText(MainActivity.this, "Refresh Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menuAbout:
Toast.makeText(MainActivity.this, "About Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
If i select an item from the list, i get the notification as expected.
How do i make the menu look like the Options menu on the android dev site?
That is what the options menu looks like on new devices, a list. If you want your options to be part of the top bar (called the Action Bar), add android:showAsAction to your xml.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menuRefresh"
android:icon="#drawable/ic_menu_refresh"
android:title="Refresh"
android:showAsAction = "always"/>
<item android:id="#+id/menuAbout"
android:icon="#drawable/ic_menu_info_details"
android:title="About"
android:showAsAction = "always"/>
</menu>
For demo purposes, I chose the attribute to be "always", but you have more options:
"ifRoom"
"never"
"withText"
"always"
"collapseActionView"
Even in new devices, you can set the theme to an old one and the menu will display in the old 6-item table layout:
<style name="AppBaseTheme" parent="android:Theme">