I have made a ToolBar, but when I add menu items in menu.xml it always shows as an overflow. How do I add it separately? Moreover the Title is shown in the middle (vertically), how do I show it at the top?
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="com.example.toolbar.MainActivity" > <item android:id="@+id/action_search" android:icon="@drawable/ic_launcher" android:title="@string/action_search" android:showAsAction="ifRoom" android:orderInCategory="1" android:menuCategory="secondary"/> <item android:id="@+id/action_settings" android:orderInCategory="100" android:title="@string/action_settings" app:showAsAction="never"/> </menu>
MainActivity.java
package com.example.toolbar; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.Toolbar; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); setSupportActionBar(toolbar); // toolbar.setNavigationIcon(R.drawable.back); toolbar.setLogo(R.drawable.ic_launcher); //toolbar.setTitle("Title"); // toolbar.setSubtitle("Subtitle"); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
activity_main.xml
<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" tools:context="com.example.toolbar.MainActivity" > <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/my_awesome_toolbar" android:layout_width="fill_parent" android:layout_height="128dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" app:theme="@style/ActionBarThemeOverlay"> </android.support.v7.widget.Toolbar> </RelativeLayout>
Add the Search View to the App Bar To add a SearchView widget to the app bar, create a file named res/menu/options_menu. xml in your project and add the following code to the file. This code defines how to create the search item, such as the icon to use and the title of the item.
All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.
OK, I got the icons because I wrote in menu.xml android:showAsAction="ifRoom"
instead of app:showAsAction="ifRoom"
since i am using v7 library.
However the title is coming at center of extended toolbar. How to make it appear at the top?
Try to do this:
getSupportActionBar().setDisplayShowTitleEnabled(false); getSupportActionBar().setDisplayHomeAsUpEnabled(false); getSupportActionBar().setDisplayShowTitleEnabled(false);
and if you made your custom toolbar (which i presume you did) then you can use the simplest way possible to do this:
toolbarTitle = (TextView)findViewById(R.id.toolbar_title); toolbarSubTitle = (TextView)findViewById(R.id.toolbar_subtitle); toolbarTitle.setText("Title"); toolbarSubTitle.setText("Subtitle");
Same goes for any other views you put in your toolbar. Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With