-2

I want to add custom layout to the menuitem in menu.xml file.

 LinearLayout actionItemLayout = (LinearLayout) menu.findItem(R.id.itemMenu).getActionView();
 TextView txtNumber = (TextView)actionItemLayout.findViewById(R.id.txt_vehicleNumber);
 TextView txtName = (TextView)actionItemLayout.findViewById(R.id.txt_vehicleName);

I have done like this but every time actionItemLayout is giving null.

Please help

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
sushma1008
  • 125
  • 5
  • 14

3 Answers3

1

It works using following code:

getMenuInflater().inflate(R.menu.menu, menu);
MenuItem item = menu.findItem(R.id.itemMenu);
MenuItemCompat.setActionView(item, R.layout.layout_menu);
LinearLayout rootView = (LinearLayout) MenuItemCompat.getActionView(item);

TextView txtNumber = (TextView) rootView.findViewById(R.id.txt_vehicleNumber);
TextView txtName = (TextView) rootView.findViewById(R.id.txt_vehicleName);

This works for me.

林果皞
  • 7,539
  • 3
  • 55
  • 70
sushma1008
  • 125
  • 5
  • 14
0

In your Toolbar.xml add this:

local:popupTheme="@style/ThemeOverlay.AppCompat.Light"

and in your style.xml define your style:

 <style name="MyMaterialTheme.PopupOverlay" 
    parent="ThemeOverlay.AppCompat.Light" >
    <item name="android:popupMenuStyle">@style/PopupMenu</item>
    <item name="android:colorBackground">@color/background</item>
    <item name="android:textColor">@color/colorAccent</item>
    </style>

and in your Activity use onCreateOptionsMenu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = activity.getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);

MenuItem filter_menu, profile_menu, purchage_menu; //Replace by your menu items

    filter_menu = menu.findItem(R.id.action_filter);
    menu_language = menu.findItem(R.id.menu_item_language);
    login_menu = menu.findItem(R.id.action_login);
    }

And use this for click:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.action_search:
      final Intent searchIntent = new Intent(MainActivity.this, 
       SearchActivity.class);
            searchIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(searchIntent);
            // Not implemented here
            return false;
        case R.id.action_filter:

            // Not implemented here
            return false;
        case R.id.action_login:

            Intent loginIntent = new Intent(MainActivity.this, 
            LoginActivity.class);
            Util.check_for_subscription = 0;
            startActivity(loginIntent);
            // Not implemented here
            return false;
0

this will help you must set your custom adapter in recyclerView.

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".DrawerActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/purple_200" />

    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <include layout="@layout/header_layout" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>

    </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

Layout preview

Skylar Ittner
  • 802
  • 11
  • 26
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 17 '22 at 12:28