My app has an options menu that is available in almost all Activities, which was created by implementing onCreateOptionsMenu(). But in one Activity there is a PopupWindow, and when this PopupWindow has focus (required for proper functioning) tapping the menu button does not bring up the options menu.
PopupWindows do not have an onCreateOptionsMenu() function. Is there some other way to add an options menu to a PopupWindow?
Alternatively, is there a way to get the options menu from the Activity behind it to show up when the user taps the menu button?
I solved this by intercepting the menu key and calling openOptionsMenu() on the activity. Here is the key listener:
OnKeyListener mMenuKeyListener = new OnKeyListener() {
#Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_MENU) {
activity.openOptionsMenu();
return true;
} else {
return false;
}
}
};
I think you have to add this key listener to every view in the PopupWindow to get it to work, so I wrote a function to do that:
public void setupMenuKeyListenerRecursive(View v) {
if (v != null) {
try {
ViewGroup viewGroup = (ViewGroup)v;
int childCount = viewGroup.getChildCount();
for (int index = 0; index < childCount; index++) {
View child = viewGroup.getChildAt(index);
setupMenuKeyListenerRecursive(child);
}
} catch (Exception e) {
}
v.setOnKeyListener(mMenuKeyListener);
}
}
Related
Within an activity, I have a custom recycler view that contains 2 textviews and 2 buttons as shown below.
The id for the recycling bin icon button is deleteButton. I would like to access this button within espresso testing so that I can mock a click.
Two methods that I have attempted and failed are shown below:
onView(withId(R.id.basket))
.perform(actionOnItemAtPosition(4, click() ));
onView(withText("Cherry"))
.perform(
RecyclerViewActions.actionOnItem(
hasDescendant(withId(R.id.deleteButton)),
ViewActions.click()
)
);
Thank you for any help in advance.
One way to click on a view inside a RecyclerView item view is by creating a custom view action:
public static ViewAction actionOnItemView(Matcher<View> matcher, ViewAction action) {
return new ViewAction() {
#Override public String getDescription() {
return String.format("performing ViewAction: %s on item matching: %s", action.getDescription(), StringDescription.asString(matcher));
}
#Override public Matcher<View> getConstraints() {
return allOf(withParent(isAssignableFrom(RecyclerView.class)), isDisplayed());
}
#Override public void perform(UiController uiController, View view) {
List<View> results = new ArrayList<>();
for (View v : TreeIterables.breadthFirstViewTraversal(view)) {
if (matcher.matches(v)) results.add(v);
}
if (results.isEmpty()) {
throw new RuntimeException(String.format("No view found %s", StringDescription.asString(matcher)));
} else if (results.size() > 1) {
throw new RuntimeException(String.format("Ambiguous views found %s", StringDescription.asString(matcher)));
}
action.perform(uiController, results.get(0));
}
};
}
Then use one of the RecyclerViewActions on your RecyclerView, then actionOnItemView as subsequent action on the item view if successful:
ViewAction itemViewAction = actionOnItemView(withId(R.id.deleteButton), click());
onView(withId(your_recycler_view)).perform(actionOnItemAtPosition(4, itemViewAction));
I have a recyclerview. After setting adapter, user should be reorder their list and save it. And afterwards it should be populate like saved one. But onclicking list items, it color has to change and one of item will open a popup. I've write my codes, but when i add both onclick and ontouch, neither of them worked. If i comment one of them, they work fine. How can i solve this?
holder.itemView.setOnTouchListener((v, event) -> {
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
touchHelper.startDrag(holder);
}
return false;
});
holder.clData.setOnLongClickListener(view -> {
row_index = position;
notifyDataSetChanged();
return true;
});
if (row_index == position) {
holder.clData.setBackgroundColor(context.getResources().getColor(R.color.blue_back));
holder.txtAmount.setTextColor(Color.WHITE);
} else {
holder.clData.setBackgroundColor(context.getResources().getColor(R.color.darkestGrey));
holder.txtAmount.setTextColor(context.getResources().getColor(R.color.blue_current));
}
/// https://github.com/sjthn/RecyclerViewDemo/blob/advanced-usecases/app/src/main/java/com/example/srijith/recyclerviewdemo/SwipeAndDragHelper.java
////// My Adapter
AdapterDatas adapter = new AdapterDatas (context, lstDatas);
SwipeAndDragHelper swipeAndDragHelper = new SwipeAndDragHelper(adapter);
ItemTouchHelper touchHelper = new ItemTouchHelper(swipeAndDragHelper);
adapter.setTouchHelper(touchHelper);
rvDatas.setAdapter(adapter);
touchHelper.attachToRecyclerView(rvDatas);
I have created a private member of the whole class called Member curMbr;
The activity (rather the fragment, since this is in a frament class) has a listview
with some contributions from members.
I also have a context menu on that list. When clicking on a contribution, I want a (customized) dialog box to show details about the member. (Member ID is part of the contribution objet. )
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
Log.d("FRGCOTIZ02", "create ctxt menu");
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
String[] menuItems = getResources().getStringArray(R.array.ar_menu_ctxt_participant);
// Get selected member
Contribution curCotis = (Contribution) (((ListView)v).getItemAtPosition(info.position));
Participant p = new Participant(helper.getDBItem(DBHelper.TABLE_PARTICIPANT,
DBHelper.COL_ID, curCotis.getParticipant()));
curMbr = new Member(helper.getDBItem(DBHelper.TABLE_MEMBER, DBHelper.COL_ID, p.getMember()));
for (int i = 0; i < menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
Log.d("FRGCOTIZ01", curMbr.getId_());
}
#Override
public boolean onContextItemSelected(MenuItem item) {
return ( applyContextMenuSelection(item) || super.onContextItemSelected(item) );
}
private boolean applyContextMenuSelection(MenuItem item) {
switch (item.getItemId()) {
case 0: // Summary
final Dialog dlg = new Dialog(this.getContext());
final String sessID;
try {
sessID = KUtil.DATE_FORMAT.format(curSess.getDate());
dlg.setContentView(R.layout.alert_show_charges);
Button btnOK = dlg.findViewById(R.id.btn_alertOK);
btnOK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setupAlertDialogCharges(dlg, sessID, curMbr.getId_());
}
});
Button btnCancel = dlg.findViewById(R.id.btn_alertCancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dlg.dismiss();
}
});
dlg.show();
} catch (Exception e){
Log.d("FRAGMENT Contribution", e.getMessage());
}
break;
case 1: // Collect
break;
case 2: // Cancel
break;
default:
break;
}
return true;
}
In method onCreateContextMenu, I can get the member and display his ID.
But in method applyContextMenuSelection, there is an exception, saying the meber is null!
Funny enough there is another variable that I am using in that method, and it works fine. Difference is, that variable has been set at creation of the fragment.
How do I solve this?
I have been studying the code for a while and the only thing I could figure was that the issue is somehow linked to the use of contextmenu. It seems to me that variables are set back to their original when the menu action is supposed to be executed. Again, I am not too sure about that.
So, the only solution I found so far was to keep that vale in a "higher" context:
When I can still read the value,
getActivity().getIntent().putExtra(HomeActivity.ID_ENTRY, curMbr.getId_());
When I want to use it,
final String mbrID = getActivity().getIntent().getStringExtra(HomeActivity.ID_ENTRY);
Is it possible to check if a ScrollView is scrolled all its way in the top?
I want to check this so I can enable a SwipeRefreshLayout, otherwise keeping it disabled.
With a ListView it could be done like this, but there's no setOnScrollListener for ScrollViews
listView.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
boolean enable = false;
if(listView != null && listView.getChildCount() > 0){
// check if the first item of the list is visible
boolean firstItemVisible = listView.getFirstVisiblePosition() == 0;
// check if the top of the first item is visible
boolean topOfFirstItemVisible = listView.getChildAt(0).getTop() == 0;
// enabling or disabling the refresh layout
enable = firstItemVisible && topOfFirstItemVisible;
}
swipeRefreshLayout.setEnabled(enable);
}
});
This link might be helpful to You. It shows, how to set scroll listener for ScrollView. Then, refer to #antonio answer.
For your case it would be:
mScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
int scrollY = mScrollView.getScrollY(); //for verticalScrollView
if (scrollY == 0)
swipeRefresh.setEnabled(true);
else
swipeRefresh.setEnabled(false);
}
});
You can use the getScrollY() method (from the View class)
In Kotlin:
scrollView.viewTreeObserver.addOnScrollChangedListener {
if (!scrollView.canScrollVertically(1)) {
// Bottom of scroll view.
}
if (!scrollView.canScrollVertically(-1)) {
// Top of scroll view.
}
}
I've been trying to use selectors in slidingmenu for 2 days without managing to make it work.
Here is what I want to do :
I've a menu which have a ListView in it. I want the selector to point at one particular item and move with the item when the listview is scrolled.
So basically the selector is pointing at the right item but when I'm scrolling, the selector don't move on my phone (android 4.0.4) but it work with the emulator (4.1.2). Do you have any idea of why the menu is not invalidating itself when I ask him to do so ?
/*Setting the sliding menu */
setBehindContentView(R.layout.menu);
setSlidingActionBarEnabled(true);
getSlidingMenu().setMode(SlidingMenu.LEFT);
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
getSlidingMenu().setFadeEnabled(true);
getSlidingMenu().setFadeDegree(0.35f);
getSlidingMenu().setShadowWidth(15);
getSlidingMenu().setShadowDrawable(R.drawable.shadow);
getSlidingMenu().setSelectorEnabled(true);
getSlidingMenu().setSelectorDrawable(R.drawable.selector);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
getSlidingMenu().setBehindWidth((int) (metrics.widthPixels * 0.8));
getSlidingMenu().setSelectedView(null);
/*Creating the content of the sliding menu*/
/*Now we generate the menu below */
maListViewPerso = (ListView) this.findViewById(R.id.listviewperso);
maListViewPerso = SlidingMenuListCreator.getListView(this, (String) this.getTitle(), maListViewPerso, isMissionSelected, isTourneeOpened);
getSlidingMenu().setOnOpenListener(new OnOpenListener() {
#Override
public void onOpen() {
int wantedPosition = 5; // Whatever position you're looking for
int firstPosition = maListViewPerso.getFirstVisiblePosition() - maListViewPerso.getHeaderViewsCount(); int wantedChild = wantedPosition - firstPosition;
if (wantedChild < 0 || wantedChild >= maListViewPerso.getChildCount()) {
} else {
selectedView = maListViewPerso.getChildAt(wantedChild);
}
getSlidingMenu().setSelectedView(selectedView);
getSlidingMenu().invalidate();
}
});
OnTouchListener mOnTouchListener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
maListViewPerso.onTouchEvent(event);
int wantedPosition = 5; int firstPosition = maListViewPerso.getFirstVisiblePosition() - maListViewPerso.getHeaderViewsCount(); int wantedChild = wantedPosition - firstPosition;
if (wantedChild < 0 || wantedChild >= maListViewPerso.getChildCount()) {
} else {
selectedView = maListViewPerso.getChildAt(wantedChild); }
getSlidingMenu().setSelectedView(selectedView);
getSlidingMenu().getmViewBehind().invalidate();
return true;
}
};
maListViewPerso.setOnTouchListener(mOnTouchListener);
After all I never find the solution.
But :
I switched to a viewpager navigation mode because that what was users preferred (and discovered that, yes, Fragments are awesome)
Google have finally made this navigation pattern "official" by adding it through the support library and drawerlayout and it works very well for me =)