0

I want to display the menu icon to left of the toolbar and the search icon to be in the actual place of the menu icon

This is my main.xml file inside the main folder

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">


    <item
        android:id="@+id/my_search"
        android:icon="@drawable/ic_search_black_24dp"
        android:title="search"
        app:showAsAction="always">

    </item>
    <item
        android:id="@+id/my_menu_icon"
        android:icon="@drawable/ic_menu_black_24dp"
        android:title="side_menu"
        app:showAsAction="always">

    </item>

</menu>

This is the result of this page: my result

And this is my MainActivity.java:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);//affect the menu file to the main activity and call this last one

        return true;
    }
}
Bulat
  • 720
  • 7
  • 15
zak dev
  • 3
  • 1

1 Answers1

0

I prefer use Toolbar instead of ActionBar, and Customize layout in xml form. you can change AppTheme like this inside /src/main/res/values/style.xml:

<style name="AppTheme" parent="Base.Theme.AppCompat.Light">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
</style>

do not forget to set AppTheme in manifest, like this:

<application
        android:theme="@style/AppTheme"
        //and other properties
</application>

then you can use android Toolbar inside layouts:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:fitsSystemWindows="true"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary">

           <LinearLayout
               android:layout_width="match_parent"
               android:layout_height="?attr/actionBarSize"
               android:orientation="horizontal">

            <ImageButton
                android:id="@+id/menu_btn"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="left"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:src="@drawable/ic_menu_black_24dp""/>

            <TextView
                android:id="@+id/title"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:gravity="left"
                android:text="App Title"
                android:textSize="16dp"
                android:textStyle="bold"/>

            <ImageButton
                android:id="@+id/search_btn"
                android:layout_width="?attr/actionBarSize"
                android:layout_height="match_parent"
                android:layout_gravity="right"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:src="@drawable/ic_search_black_24dp"/>

           </LinearLayout>

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    // and other views
    // .
    // .
    // .

</android.support.design.widget.CoordinatorLayout>
Community
  • 1
  • 1
Arvin Rokni
  • 479
  • 5
  • 16
  • it's worked for me sir @arvin-rokni thank's, i have a question :i putted the code you gave me inside my activitymain.xml ,and it did what i exactly wanted,so should i now delete my main.xml file inside the main folder,or i should leave it for further pages??,i dont know exactly what will be its role for now in my app!! – zak dev May 28 '19 at 21:39