Java – Android OnCreateOptionsMenu projects do not work with actionLayout

Android OnCreateOptionsMenu projects do not work with actionLayout… here is a solution to the problem.

Android OnCreateOptionsMenu projects do not work with actionLayout

I need to display a layout in my options menu.

Menu .xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/action_likes"
        android:showAsAction="never"
        android:actionLayout="@layout/likes_layout"/>

<item
        android:id="@+id/settings"
        android:showAsAction="never"
        android:title="Settings"
        />
</menu>

shape_notification.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke android:color="#080808" android:width="2dp"/>
    <corners android:radius="5dp" />
    <solid android:color="#85ea32"/>
</shape>

likes_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:id="@+id/likes"
        android:linksClickable="false"
        android:text="Likes"
        android:gravity="center_vertical|center_horizontal"
        android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:layout_gravity="center"
        android:id="@+id/notif_count"
        android:background="@drawable/shape_notification"
        android:text="0"
        android:textSize="16sp"
        android:gravity="center"
        android:padding="2dp"
        android:singleLine="true"
        android:layout_margin="5dp"
        android:textColor="#fff"/>
</LinearLayout>

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) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);

return true;
    }
}

The list of options is visible. Settings items are visible, but prefer that the layout is blank.

Has anyone done an Action layout for hidden elements? Please help me

Solution

Change android:actionLayout

to app:actionLayout so that your menu item declaration should look like

  <item
    android:id="@+id/action_likes"
    app:showAsAction="never"
    app:actionLayout="@layout/likes_layout"/>

Update:

You must use PopupWindow to create a custom drop-down menu. Code fragment that needs to be implemented to archive it:

menu.xml

<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="project.com.example.MainActivity"

>
<item
    android:id="@+id/item1"
    android:title="@string/action_settings"
    android:icon="@drawable/ic_launcher"
    app:actionViewClass="android.widget.ImageButton"
    app:showAsAction="always">

<menu>
</menu>
</item>

</menu>

MainActivity.java

   @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);
    locButton = (ImageButton) menu.findItem(R.id.action_settings).getActionView();
    locButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PopupWindow popupwindow_obj = popupDisplay();
            popupwindow_obj.showAsDropDown(locButton, -40, 18);
        }
    });
    return true;
}
  public PopupWindow popupDisplay()
{

final PopupWindow popupWindow = new PopupWindow(this);

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View view = inflater.inflate(R.layout.listViewLayout, null);
    ListView listView=(ListView)view.findViewById(R.id.list);
     populate listview with custom view which will be shown in drow down menu 
    popupWindow.setFocusable(true);
    popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setContentView(view);

return popupWindow;
}

Hope this helps.

Related Problems and Solutions