Action overflow menus for elements of a list view - android

The overflow actions menu looks exactly like what I need, but it seems to only work within action bars. However, I'd like this kind of three-dots-menu on the right side of each element of a list view.
Is there a built-in view that can be used for this?

You can use an ImageView and a PopupMenu.
Context context = ...
ImageView overflow = ...
PopupMenu popup = new PopupMenu(context, overflow);
popup.inflate(R.menu.your_menu_resource);
popup.setOnMenuItemClickListener(...);
overflow.setOnClickListener(new View.OnClickListener() {
#Override
public final void onClick(View v) {
popup.show();
}
}
overflow.setOnTouchListener(popup.getDragToOpenListener());

Related

PopupMenu - how to?

I am beginner in android and try to make an app that popup a menu when imageView is clicked. Actually it works but popup at right side and I want to be at center.
So, I searched for it on web and come across ListPopupWindow, PopupWindow classes. I tried various methods of this classes as per my knowledge/ability but I am not able to achieve this.
Guide me to do that. Here is my code
imageview setonclickListener(new View.onclickListener() {
#Override public void onClick(View view){
PopupMenu popup = new PopupMenu(Info4 Activity.this, imageview1);
Menu menu = popup.getMenu();
for (int i = 0; i < (int) (subjects.size()); i++) {
val = subjects.get((int) (i));
menu.add(val);
}
popup.show();
}
});
PopupMenu:
The popup will appear below the anchor if there is room, or above it if there is not. In your situation the anchor is imageView, so you can not center the popup in screen.
PopupWindow:
Try this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);// here v is any View only needed for WindowToken
}
});
Other better ways:
May be you could be think about using DialogFragment, which is customizable and flexible.

How to replace the actionbar by a popup menu?

I want to achieve 1 of these options for my EditText :
Replace the actionbar that appear on the top by a popup menu instead. Something like this for exemple:
Or make the actionbar floating and child of my current view (in some way same a first option)
i need this because i add my view via windowManager.addView(view, Layout_Params); and in this way i have some trouble with the actionbar on the top (it is displayed blank)
actually i do this to show the actionbar :
#Override
public ActionMode startActionMode(ActionMode.Callback callback) {
Activity host = (Activity) this.getContext();
return host.getWindow().getDecorView().startActionMode(callback);
}
but it's don't work, it's show me an empty white actionbar on the stop instead :( i think i need to create myself the ActionMode but i don't know how to do it.
Ok. You can hide the actionbar when your edittext is gained focus. Then you need to show the popup menu where ever you want.
EditText t = new EditText(this);
t.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b){
getSupportActionBar().hide();
//now show your popup menu at the top position
}else{
getSupportActionBar().show();
//here you dismiss the menu.
}
}
});

HomeAsUpIndicator Custom View

I'm looking at adding a custom view for the HomeAsUpIndicator whenever there are new message. I want to bring attention to the hamburger icon when there are new messages, and then use the default hamburger icon when there aren't new messages.
I have two functions to display custom icon and display default, but I want to use a custom View.
protected void setCustomIcon() {
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
mDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_menu_hamburger);
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawerLayout.openDrawer(mDrawerList);
}
});
}
protected void setDefaultIcon() {
mDrawerToggle.setDrawerIndicatorEnabled(true);
}
Is there a way to use a custom View instead of a resId or a drawable?

Add Up-Navigation Properties in Custom ActionBar Button in Android

I want to create my own ActionBar Layout.
Like this (created in Paint for example)
Is it possible to give the second Button the Up-Navigation Properties? So if I press it, it finish this Activity and starts it's parent.
I want to have the burger Icon for the Navigation Drawer, the Up-Icon for Up-Navigation and the Title of the Activity.
Is it possible? Or is there a solution already?
Actually, it's fairly easy(though it's little hacky) to do.
First, create a drawable for back button(preferably - as a selector, to distinguish pressed/normal state:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/back_button_pressed"/>
<item android:drawable="#drawable/back_button"/>
</selector>
Next, set this drawable to the logo of the toolbar toolbar.setLogo(R.drawable.back_button_selector);
Then the only thing left is to set click-listener.
View logoView = getToolbarLogoIcon(toolbar);
logoView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
...
private View getToolbarLogoIcon(Toolbar toolbar){
//check if contentDescription previously was set
boolean hadContentDescription = android.text.TextUtils.isEmpty(toolbar.getLogoDescription());
String contentDescription = String.valueOf(!hadContentDescription ? toolbar.getLogoDescription() : "logoContentDescription");
toolbar.setLogoDescription(contentDescription);
ArrayList<View> potentialViews = new ArrayList<>();
//find the view based on it's content description, set programatically or with android:contentDescription
toolbar.findViewsWithText(potentialViews,contentDescription, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
//Nav icon is always instantiated at this point because calling setLogoDescription ensures its existence
View logoIcon = null;
if (potentialViews.size() > 0) {
logoIcon = potentialViews.get(0);
}
//Clear content description if not previously present
if (hadContentDescription) {
toolbar.setLogoDescription(null);
}
return logoIcon;
}
(Thanks Nicola's post here). Or if you are not scared of reflection, it can be easily done like this:
try {
Field declaredField = toolbar.getClass().getDeclaredField("mLogoView");
declaredField.setAccessible(true);
View logoView = (View) declaredField.get(toolbar);
logoView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
} catch (Exception ex) {
//error
}
Another possible solution would be to set custom layout to the ActionBar.
Though, I'm advocating to follow the UI/UX guidelines and double-check, if navigation drawer is essential in the secondary activity.

Showing a List View when a button is clicked: Android

I am trying to implement a drop down list when a button is clicked.
So, I have a text view and a button in a navigation bar(nav.xml) and a corresponding list view. This navigation bar is included in another page( products.xml)
when the button is clicked i get the list view right below the button(which is what i want to acheive) but its my moving all the contents on the current page downwards, even the text view which is placed in nav bar moved downwards.
I am totally new to Android, any sample examples or a way how to achieve it
???
Sounds like you need a Spinner. It's the equivalent of a drop down list for Android. You can find an example here.
So, for our need we need to use ListPopupWindow.
The link to official description:
http://developer.android.com/reference/android/widget/ListPopupWindow.html
Let's dive in the code:
we have our own method:
public void downloadBtnSelected(View anchor) {
final ListPopupWindow lpw = new ListPopupWindow(this);
String[] data = { ".png", ".pdf", ".jpg", ".jpeg" };
PopupAdapter pa = new PopupAdapter(data, this);
lpw.setAdapter(pa);
//setting up an anchor view
lpw.setAnchorView(anchor);
//Setting measure specifications. I'v used this mesure specs to display my
//ListView as wide as my anchor view is
lpw.setHeight(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
lpw.setWidth(anchor.getRight() - anchor.getLeft());
// Background is needed. You can use your own drawable or make a 9patch.
// I'v used a custom btn drawable. looks nice.
lpw.setBackgroundDrawable(this.getResources().getDrawable(
android.R.drawable.btn_default));
// Offset between anchor view and popupWindow
lpw.setVerticalOffset(3);
lpw.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
/// Our action.....
lpw.dismiss();
}
});
lpw.show();
}
and the button with an onClickListener to call this method:
Button btn = new Button(this);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
downloadBtnSelected(v);
}
});
we pass the View v argument as our anchor, in order to let our PopupWindow to know where to display itself. It will be displayed in the bottom-left corner of our anchor view if there is enough room below. If not- it will displa

Categories

Resources