I want to show the Overflow menu on ImageButton click.
menu/menu_scrollable_tab.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
tools:context="com.example.scrollingtab.activity.ScrollableTabsActivity">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="always"
android:icon="#drawable/info" />
<item android:id="#+id/action_settings1"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="#+id/action_settings2"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
I have disabled the default Action bar and created Action bar using layout. I am not using default action bar or toolbar since i need to customize the action bar more.
layout/actionbar.xml
<?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"
android:layout_width="match_parent"
android:layout_height="50dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:background="#color/actionbar_bg"
android:weightSum="2">
<ImageButton android:id="#+id/ib_navigation"
android:background="#drawable/nv_drawer_24x24"
android:layout_width="0dp"
android:layout_weight=".3"
android:layout_height="50dp"/>
<View
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1.1"/>
<ImageButton android:id="#+id/ib_navigation1"
android:background="#drawable/info"
android:layout_width="0dp"
android:layout_weight=".3"
android:layout_height="50dp"/>
<ImageButton android:id="#+id/ib_navigation2"
android:background="#drawable/ic_drawer"
android:layout_width="0dp"
android:layout_weight=".3"
android:layout_height="50dp"/>
</LinearLayout>
</RelativeLayout>
As per the client requirement I have designed the action bar using layout and the look and feel are good.
The default OnCreateOptionsMenu(Menu menu) method is not populating the overflow menu. Now i want to populate the overflow menu on clicking the ImageButton with id= ib_navigation2. I don't have any idea to implement this.
Can any one help me on doing this.
Thanks
You need to override the method for creating you own menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
The buttonclick can call openOptionsMenu();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openOptionsMenu();
}
});
Or else
public myOnClickMethod(View v) {
openOptionsMenu();
}
See here for more information about menus
http://developer.android.com/guide/topics/ui/menus.html
Hope this will work for you
Related
I'm struggling with 2 menu items that should display on a toolbar in their icon fashion, but they always display on the toolbar in a drop-down menu fashion.
Here is my code so far,
MainActivity.java
package com.yardimobileinterns.apptoolbar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
//menu icons are inflated
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//inflate the menu. This adds items to the action bar, if it is present
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id) {
case R.id.miCompose:
composeMessage();
break;
case R.id.miProfile:
showProfileView();
break;
}
return super.onOptionsItemSelected(menuItem);
}
private void composeMessage() {
getSupportActionBar().setTitle("Compose");
}
private void showProfileView() {
getSupportActionBar().setTitle("Profile");
}
}
activity_main.xml :
<?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:fitsSystemWindows="true"
android:orientation="vertical">
<include
layout="#layout/toolbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_gravity="center_horizontal"
android:layout_marginTop="100dp"
/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button"
android:layout_gravity="center"
android:layout_marginTop="100dp"
/>
</LinearLayout>
toolbar_main.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:titleTextColor="#android:color/white"
/>
menu_main.xml : -here is the part that should make the difference
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<item
android:id="#+id/miCompose"
android:icon="#drawable/menu"
android:title="#string/item1"
app:showAsAction="always">
</item>
<item
android:id="#+id/miProfile"
android:icon="#drawable/week_view"
android:title="#string/profile"
app:showAsAction="always">
</item>
</menu>
Any help is welcomed.
You are going good but just one line correction change this line in menu.xml
xmlns:app="http://schemas.android.com/apk/res-auto"
Remove the 's' from http
You can menu_main.xml like ...bellow
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:balloonberry="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/miCompose"
balloonberry:showAsAction="always"
android:icon="#drawable/menu"/>
<item
android:id="#+id/miProfile"
balloonberry:showAsAction="always"
android:icon="#drawable/week_view"/>
</menu>
Make Change Like this. It will show both items.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<item
android:id="#+id/miCompose"
android:icon="#drawable/menu"
android:title="#string/item1"
android:orderInCategory="1"
app:showAsAction="always">
</item>
<item
android:id="#+id/miProfile"
android:icon="#drawable/week_view"
android:title="#string/profile"
android:orderInCategory="2"
app:showAsAction="always">
</item>
</menu>
I'm using android.support.v7.widget.Toolbar
Here's my tool bar layout
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" >
<ImageButton
android:id="#+id/scannerSettingsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:background="#android:drawable/ic_menu_preferences"
/>
<ToggleButton
android:id="#+id/scannerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:layout_gravity="end"
android:textOn="SCAN"
android:textOff="STOP"/>
<ProgressBar
android:id="#+id/progress_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:layout_gravity="end"
/>
</android.support.v7.widget.Toolbar>
Here's my icons in the toolbar layout
I would like a drop down menu to appear when the user clicks on the #id/scannerSettingsButton which is the wrench styled icon boxed in with red farthest to the right.
Here is an example of a drop down menu
Any idea how to add a drop down menu to my ImageButton?
Thanks
Create your menu.xml file like this
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu1"
android:icon="#mipmap/ic_launcher"
android:title="menu"
app:showAsAction="ifRoom|withText" >
<menu>
<item
android:id="#+id/submenu1"
android:title="sub_menu1" />
<item
android:id="#+id/submenu2"
android:title="sub_menu2" />
<item
android:id="#+id/submenu3"
android:title="sub_menu3" />
</menu>
</item>
</menu>
Override onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_toolbar,menu);
return true;
}
Override onOptionsItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.submenu1:
//add your method
return true;
case R.id.submenu2:
//add your method
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I have an ImageButton, as well as an TextView defined within my android.support.v7.widget.Toolbar. I also have a menu item, but this is not showing up when I run the app.
I have claeed the getMenuInflater().inflate(R.menu.menu, menu) in my activity, but not sure what I am missing here.
Here is my tool_bar.xml:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorPrimary"
android:id="#+id/tool_bar">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_nav_icon"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textSize="30dp"
android:layout_marginLeft="20dp"
android:textColor="#ffffff"/>
</android.support.v7.widget.Toolbar>
Here is my menu item:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:icon=" #drawable/ic_search"
android:title="Search"
app:showAsAction="always" />
</menu>
And here is my HomeActivity.java class
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar)findViewById(R.id.tool_bar);
this.setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{
//implement logic here to get selected item
return super.onOptionsItemSelected(menuItem);
}
Why is the menu item not showing?
try this,
add this code in home.xml
<include android:id="#+id/tool_bar" layout="#layout/tool_bar"></include>
and also change in style
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
I'm using Searchview with no action bar (it goes straight to the menu). But i want to add a NavigationView also. The problem is that i can't see a hamburger icon in the ActionBar (because i don't have one), also my SearchView appears in the NavigationView (because it in my menu.xml).
Here is my menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
<item
android:id="#+id/search_nav"
android:checked="true"
android:icon="#drawable/search"
android:title="Search" />
<item
android:id="#+id/favs_nav"
android:checked="false"
android:icon="#drawable/favorites"
android:title="Favorites" />
</group>
<item
android:id="#+id/search"
android:icon="#drawable/search"
android:title="search_title"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="always" />
activity_main.xml:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:id="#+id/main_activity"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/cardList"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#dcdcdc"
app:headerLayout="#layout/drawer_header"
app:itemTextColor="#color/black"
app:menu="#menu/menu_main" />
</android.support.v4.widget.DrawerLayout>
Part of my MainActivity:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search_nav:
Toast.makeText(this, "selected all", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
return true;
case R.id.favs_nav:
Toast.makeText(this, "selected movies", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
return true;
}
return true;
}
i am showing map in main content view which is a fragment. i want to show a listView over the main content view having map on a button click in an action bar or swipe the screen from left. i have tried alot but all in vain. my problem is how can i show the listview over the main content fragment such that the list view comes over the map.
upto now the listview appears when i swipe but it appears below the map in the fragment
i hav'nt add the functionality for clicking the button in actionbar. i am just swiping the list view from left. thanks in advance
my code is
DrawerClass.java
public class DrawerClass extends Activity {
DrawerLayout drawerLayout;
View drawerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.drawarclass_layout);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerView = (View) findViewById(R.id.drawer);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.actionbaricons_layout, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
drawerclass_layout.xml
<LinearLayout
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="#+id/mapid"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:id="#+id/drawer"
android:layout_width="140dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/background_light"
android:orientation="vertical"
android:padding="5dp" >
<fragment
android:id="#+id/fragment1"
android:name="open.way.iscope.MyListFragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
actionbaricons_layout.xml
<item
android:id="#+id/category"
android:gravity="center"
android:icon="#drawable/ic_action_select_all"
android:showAsAction="always"
android:title="#string/categories"/>
<item
android:id="#+id/previous"
android:icon="#drawable/ic_action_previous_item"
android:showAsAction="always"
android:title="#string/previous"/>
<item
android:id="#+id/next"
android:icon="#drawable/ic_action_next_item"
android:showAsAction="always"
android:title="#string/next"/>
<item
android:id="#+id/save"
android:icon="#drawable/ic_action_save"
android:showAsAction="always"
android:title="#string/save"/>
This is happening because the drawer element is not the root element in your drawerclass_layout.xml. Make Drawer element as your root element and the map fragment as the child in the layout file and you will be able to see your slider over the top of the main fragment that has map. Here is the code snippet.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- google map -->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
Hope this would help!!