Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add buttons like refresh and search in ToolBar in Android?

enter image description here

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> 
like image 209
kunal.c Avatar asked Nov 06 '14 11:11

kunal.c


People also ask

How do I put the Search icon on my Android toolbar?

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.

How do you add action items to the action bar?

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.


2 Answers

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?

like image 51
kunal.c Avatar answered Sep 26 '22 03:09

kunal.c


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.

like image 37
menilv Avatar answered Sep 25 '22 03:09

menilv