Reputation: 519
In my activity_main.xml i have this toolbar:
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#770000ff"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
In Manifest there is that in <application>
:
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
In res/values/style.xml
i have
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
The MainActivity.java
file is that:
import ... ;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
}
}
Now, the toolbar has only the application name and nothing else. How can i add items with icon such as https://i.sstatic.net/y29jV.png ?
Upvotes: 11
Views: 29627
Reputation: 2338
<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:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
<item
android:id="@+id/action_search"
android:orderInCategory="200"
android:title="Search"
android:icon="@drawable/ic_search"
app:showAsAction="ifRoom"
></item>
<item
android:id="@+id/action_user"
android:orderInCategory="300"
android:title="User"
android:icon="@drawable/ic_user"
app:showAsAction="ifRoom"></item>
</menu>
@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 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Upvotes: 26
Reputation: 546
Or you can add needed items in your xml file like this:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_height"
android:background="@color/blue"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/toolbar_margin"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageview_back"
style="@style/toolbar_imageview_back"/>
<TextView
android:id="@+id/textview"
style="@style/toolbar_title"/>
<ImageView
android:id="@+id/imageview_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_inbox_edit"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
Upvotes: 10
Reputation:
There is menu resource file present in app>src>main>res>menu.menu.main file. There you need to add the items you want to display ,like
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
<item
android:id="@+id/any_Id"
android:orderInCategory="2"
android:title="ANY_TITLE"
app:showAsAction="ifRoom" />
Then in your activity code , use these two methods
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main,menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.any_Id: //Your task
return true;
default:return super.onOptionsItemSelected(item);
}
}
Upvotes: 0
Reputation: 313
The icon on the left is used for navigation, and can be customized by
toolbar.setNavigationIcon(R.drawable.icon_to_be_used);
The icons on the right are created by creating an XML menuin /res/menu/ and then inflating it in the activity using:
<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:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
<item
android:id="@+id/action_search"
android:orderInCategory="200"
android:title="Search"
android:icon="@drawable/ic_search"
app:showAsAction="ifRoom"
></item>
<item
android:id="@+id/action_user"
android:orderInCategory="300"
android:title="User"
android:icon="@drawable/ic_user"
app:showAsAction="ifRoom"></item>
</menu>
And in your activity:
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Upvotes: 1
Reputation: 978
First you need to add items to your menu like this:
<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"
xmlns:appcompat="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search_white_24dp"
android:orderInCategory="100"
android:title="@string/search"
appcompat:showAsAction="always" />
<item
android:id="@+id/action_settings"
android:orderInCategory="2"
android:title="@string/action_settings"
app:showAsAction="never" />
<item
android:id="@+id/action_profile"
android:orderInCategory="1"
android:title="@string/action_profile"
app:showAsAction="never"
/>
</menu>
And then in your activity add :
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
That's It.
Upvotes: 0