Out automatic tests use views' ids to be able to click on them, so we add ids whenever possible.
For popup menus, sometimes it's needed to populate them dynamically, but as I've found, even when I do add id for each item, the id isn't found, and can't be used. Even using DDMS's feature "dump view hierarchy for UI automator" shows that no view in the popup menu has an id.
This is a sample code of what I use, to try to set an id for the a single menu item.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View v=findViewById(R.id.button);
v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, v);
final Menu menu = popupMenu.getMenu();
menu.add(0, R.id.myMenuItem, 0, R.string.app_name).setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(final MenuItem item) {
return false;
}
}) ;
popupMenu.show();
}
});
}
Note that the id is declared in "ids.xml" file, as such:
<item name="myMenuItem" type="id"/>
And this is what DDMS tool shows me :
How come this code doesn't work as expected (meaning have an id for the menu item view) ? What can I do to make the views in it to have ids? What is the correct way to add ids for menu items that are created dynamically ?
Alright, this is by no means an answer to the problem described in the question. Look at it as an alternative to replace PopupMenu
in order to achieve what was asked.
PopupMenu
After digging through the documents for PopupMenu
and its source code, I finally come to understand that PopupMenu
is not an implementation that would allow customization (my apology to the PO for the misconception in the comments).
As an alternative, a ListPopupWindow
is a preferred choice to create a popup menu with the following reasons:
PopupMenu
- ListView
.Adapter
to be defined with custom layout.PopupMenu
does, and allows attaching resource id to the item view.First of all, let's define a custom layout for the popup item (popup_item_view.xml
).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/popup_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Next, define a custom Adapter
class to manipulate the layout.
package com.example.popupmenu;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class PopupAdapter extends BaseAdapter {
public static class Item {
public final int id;
public final String title;
public Item(int id, @NonNull String title) {
this.id = id;
this.title = title;
}
}
private List<Item> mItemList = new ArrayList<>();
public PopupAdapter(@NonNull Item[] items) {
mItemList.addAll(Arrays.asList(items));
}
@Override
public int getCount() {
return mItemList.size();
}
@Override
public Item getItem(int position) {
return mItemList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Item item = getItem(position);
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.popup_item_view, parent, false);
}
convertView.setId(item.id);
TextView titleView = (TextView) convertView.findViewById(R.id.popup_text);
titleView.setText(item.title);
return convertView;
}
}
Finally, replace the PopupMenu
code with this.
PopupAdapter.Item[] items = {
new PopupAdapter.Item(R.id.popup_item_1, "item 1"),
new PopupAdapter.Item(R.id.popup_item_2, "item 2")
};
ListPopupWindow popup = new ListPopupWindow(MainActivity.this);
popup.setAnchorView(view);
popup.setAdapter(new PopupAdapter(items));
popup.setModal(true);
popup.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// do something
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// do something
}
});
popup.show();
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With