Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add items to android toolbar

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 http://i.stack.imgur.com/y29jV.png ?

like image 767
SctALE Avatar asked May 10 '16 09:05

SctALE


People also ask

How do I add items to Action Bar?

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.

How do I add three dots to my android toolbar?

Right-click res folder and create a new Resource Directory of type menu. Right-click the menu directory and create a new resource file. From there you can drag and drop a menu item from the XML design. That should give you the 3 dots.


1 Answers

First a Menu xml file and Put it in res/menu folder

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

Add These methods in Activity File

@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);
}
like image 50
Nadeem Iqbal Avatar answered Oct 12 '22 23:10

Nadeem Iqbal