Android Toolbar Menu items not showing when I have - android

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">

Related

action bar for android browser

I am creating a personal browser app.i want to add edittext,back_button,forward_button and home_button on action bar. But when I set the EditText on action bar,then it was not visible. Then I removed the action bar and set the toolbar so that it was working well. But when I set back_button, forward_button and home_button in it, it was not visible. Then I again add the action bar. But it is now looking like a screenshot below.what is wrong in my codind? please suggest me .this is my screenshot
and i want to set like this
this is my style.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
this is my toolbar.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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<EditText
android:layout_width="190dp"
android:layout_height="wrap_content"
android:id="#+id/myEditText"
android:hint="search"
android:background="#ffffff" />
</android.support.v7.widget.Toolbar>
</LinearLayout>
this is my edittext.xml(extra)
EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_address_bar"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:hint="MY"
android:imeOptions="actionSearch"
android:inputType="textUri" >
</EditText>
this is my 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/menu_search"
app:showAsAction="always"
android:title="Search"
android:actionLayout="#layout/edit">
</item>
<item
android:id="#+id/item_home"
android:icon="#drawable/ic_action_home"
android:title="Home"
app:showAsAction="always"></item>
<item
android:id="#+id/item_pre"
android:icon="#drawable/ic_action_back"
android:title="Back"
app:showAsAction="always"></item>
<item
android:id="#+id/item_forw"
android:icon="#drawable/ic_action_forw"
android:title="Forward"
app:showAsAction="always"></item>
</menu>
and this is my mainActivity.java
public class MainActivity extends AppCompatActivity {
Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return super.onCreateOptionsMenu(menu);
}
private void initToolbar() {
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbar.setTitleTextColor(Color.WHITE);
mToolbar.setTitle("gnappo");
mToolbar.showOverflowMenu();
setSupportActionBar(mToolbar);
}
}
thanks to advance
here you go i solve your problem
MainActivty.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.pro.salman.toolbar.MainActivity">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimaryDark"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:id="#+id/toolbar">
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:hint="Search"
android:visibility="invisible"
android:singleLine="true"/>
</android.support.v7.widget.Toolbar>
</RelativeLayout>
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:title="Forward"
android:id="#+id/forward"
android:icon="#drawable/ic_arrow_forward_white_24dp"
app:showAsAction="always"
android:orderInCategory="100"/>
<item
android:title="Backward"
android:id="#+id/backward"
android:icon="#drawable/ic_arrow_back_black_24dp"
app:showAsAction="always"
android:orderInCategory="90"/>
<item
android:title="Home"
android:id="#+id/Home"
android:icon="#drawable/ic_home_white_24dp"
app:showAsAction="always"
android:orderInCategory="80"/>
<item
android:title="Search"
android:id="#+id/search"
android:icon="#drawable/ic_search_white_24dp"
app:showAsAction="always"
android:orderInCategory="70"/>
</menu>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private EditText mEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)(findViewById(R.id.toolbar));
mEditText = (EditText)(findViewById(R.id.editText));
setSupportActionBar(toolbar);
setTitle("");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.search)
{
mEditText.setVisibility(View.VISIBLE);
}
return super.onOptionsItemSelected(item);
}
}
now result
the edittext is hidden when user press the search button the edittext will show up
Salman500's answer is the best answer.just change your them in res> values> style like this:-
<style name="Appname" parent="Them.AppCompat.light.NoActionBar"
and enjoy.

How to add drop down menu to widget in android.support.v7.widget.Toolbar

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);
}
}

menuInflater to create a menu in the onCreateOptionsMenu not working

I have used a menuInflater to create a menu in the onCreateOptionsMenu method of your activity. I have used this to display arrows on the toolbar for a calendar so that the user can go to previous or next month. But for some reason the arrows are not getting displayed
Pls can someone help.
MonthGridActivity:
private MonthGridFragment monthGridFragment;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_calendar_grid, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_previous:
monthGridFragment.loadLastMonth();
return true;
case R.id.action_next:
monthGridFragment.loadNextMonth();
return true;
case R.id.all_events:
monthGridFragment.showAllEvents();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar_grid);
monthGridFragment = new MonthGridFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.activity_calendar_grid_container, monthGridFragment)
.commit();
}
menu_calendar_grid.xml
<item
android:id="#+id/action_previous"
android:title="#string/prev_month"
android:orderInCategory="100"
app:showAsAction="ifRoom"
android:icon="#drawable/arrow_previous" />
<item
android:id="#+id/action_next"
android:title="#string/next_month"
android:orderInCategory="100"
app:showAsAction="ifRoom"
android:icon="#drawable/arrow_next" />
<item
android:id="#+id/all_events"
android:title="#string/view_all_events"
android:orderInCategory="101"
app:showAsAction="never"
android:icon="#android:drawable/ic_menu_view" />
MonthGridFragment.java
public void loadNextMonth() {
calendar.setTime(CalUtil.addMonth(calendar.getTime(), 1));
refresh();
}
public void loadLastMonth() {
calendar.setTime(CalUtil.subtractMonth(calendar.getTime(), 1));
refresh();
}
activity_calendar_grid.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame">
<android.support.v7.widget.Toolbar
android:id="#+id/myActivity_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
fragment_calendar_grid.xml
<FrameLayout 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:orientation="vertical"
android:id="#+id/calendar_grid_layout">
<include layout="#layout/calendar_grid_header" />
<GridView
android:paddingTop="1dp"
android:gravity="center"
android:layout_gravity="center"
android:layout_margin="0dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/calendar_grid_view"
android:numColumns="7"
android:stretchMode="columnWidth"
android:background="#color/white_gray"
android:horizontalSpacing="1dp"
android:verticalSpacing="1dp" />
in onCreate method create a toolbar:
Toolbar toolbar = (Toolbar) findViewById(R.id.myActivity_toolbar);
setActionBar(toolbar);
so in the layout add the view:
<Toolbar
android:id="#+id/myActivity_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
UPD:
You have to use either Toolbar from the support library or the standard one. If MonthGridActivity extends from Activity, use Toolbar (not android.support.v7.widget.Toolbar) in the layout. Otherwise MonthGridActivity must be extended from AppCompatActivity

Show overflow menu on Image button click

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

list view not shown over main fragment having map

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!!

Categories

Resources