Abdulkarim
Abdulkarim

Reputation: 587

adding menu to toolbar

am trying to add one menu item to toolbar that always shown on the right of the toolbar

menu File

<item android:id="@+id/menu_edit"
    android:title="@string/edit"
    android:icon="@drawable/ic_baseline_edit_24"
    app:showAsAction="ifRoom"
   />

xml file

<androidx.coordinatorlayout.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:fitsSystemWindows="true"
tools:context=".Profile">

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/profileAppBar"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:id="@+id/profileCollapsingToolbarLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:contentScrim="@color/mediumBlue"
        app:layout_scrollFlags="exitUntilCollapsed|scroll"

        >


        <ImageView
            android:id="@+id/profileFullImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/abstract_dark_blue_polygonal_background_1035_9700"
            android:fitsSystemWindows="true"
            android:transitionName="profileImage"
            app:layout_collapseMode="parallax" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/shape_background" />

        <androidx.appcompat.widget.Toolbar

            app:menu="@menu/profile_toolbr"
            android:id="@+id/profileToolBar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

         >

        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.CollapsingToolbarLayout>


</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

java.class

public class Profile extends AppCompatActivity {


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

    toolbar = findViewById(R.id.profileToolBar);
    collapsingToolbarLayout = findViewById(R.id.profileCollapsingToolbarLayout);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);




}

the problem is it not show up and only appears as three dots when i add this

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = new MenuInflater(Profile.this);
    menuInflater.inflate(R.menu.profile_toolbr,menu);
    return true ;
}

how can i add it to show as an icon on the toolbar i try to use app:showAsAction="always" but the same no change

Upvotes: 1

Views: 178

Answers (1)

Keyur Nimavat
Keyur Nimavat

Reputation: 3635

You should not use MenuInflater, as it will create new menu. Menu is handled by the activity itself. So you do not need to use new MenuInflater. So you have to remove it and use getMenuInflater.

Replace your onCreateOptionsMenu with this:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   getMenuInflater().inflate(R.menu.main_menu, menu);
   return super.onCreateOptionsMenu(menu);
}

Upvotes: 2

Related Questions