How change position of popup menu on android overflow button? - android

I just like to implement somethings same as popup menu in the Gmail app, anchored to the overflow button at the top-right. for that I used the same code as google tutorial for android Android popup menu, but for me show pop menu on top of edge of actionbar not under that. If you notice on pop menu of gmail overflow you saw that popmenu take place at edge of actionbar.
This is the xml that I used for popup menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/item1"
android:title="lablab"/>
<item
android:id="#+id/item2"
android:title="lablab"/>
</menu>
and at the follow is in my activity:
public void showFontSetting(View view) {
PopupMenu popup = new PopupMenu(this, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu, popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(Index.this,
"You Clicked : " + item.getTitle(),
Toast.LENGTH_SHORT).show();
break;
case R.id.item2:
break;
}
return true;
}
});
}

To overlap only, use this approach:
PopupMenu popupMenu = new PopupMenu(getContext(), this, Gravity.NO_GRAVITY, R.attr.actionOverflowMenuStyle, 0);
To get a PopupMenu with a bright background and a detailed control over the offsets use this approach:
styles.xml
<style name="PopupMenuOverlapAnchor" parent="#style/Theme.AppCompat.Light">
<item name="android:overlapAnchor">true</item>
<item name="android:dropDownVerticalOffset">0dp</item>
<item name="android:dropDownHorizontalOffset">0dp</item>
</style>
Code:
ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(getContext(), R.style.PopupMenuOverlapAnchor);
PopupMenu popupMenu = new PopupMenu(contextThemeWrapper, this);

Applying gravity helped in my case
PopupMenu popup = new PopupMenu(this, v, Gravity.RIGHT);

Add the following piece of code to your activity:
PopupWindow popupwindow_obj; // create object
popupwindow_obj=popupDisplay(); // initialize in onCreate()
popupwindow_obj.showAsDropDown(clickbtn,-40, 18); // where u want show on view click event
public PopupWindow popupDisplay() { // disply designing your popoup window
final PopupWindow popupWindow = new PopupWindow(this); // inflet your layout or diynamic add view
View view;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylayout, null);
Button item = (LinearLayout) view.findViewById(R.id.button1);
popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
return popupWindow;
}
Create an XML in res/layout directory and name it mylayout.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Window test" />
</LinearLayout>

After trying out each approach that I have found, I would think putting an anchoring view might still be an easier and simpler way, especially when you are using a more flexible layout, e.g. ConstraintLayout.
Just put an invisible view to where you want the popup menu to anchor:
<View
android:id="#+id/anchor_menu"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
app:layout_constraintStart_toStartOf="#+id/button_menu"
app:layout_constraintTop_toBottomOf="#+id/button_menu"
/>
Then use it as the anchoring view instead:
mPopupMenu = new PopupMenu(getActivity(), mPopupMenuAnchor);
Boom, it is done.

Maybe you might consider using PopupWindow instead.
popupwindow.showAsDropDown(view,x,y);
Here x and y is position of popup window relative to your view.

Related

Is it possible to customise Android PopupMenu?

Is it possible to customise PopupMenu layout ? I just need with rounded corners, is it possible to customise with the stock one ?
For custom layouts don't use PopupMenu, alternate option is a PopupWindow
PopupWindow popupwindow_obj = popupDisplay();
popupwindow_obj.showAsDropDown(clickbtn, -40, 18); // where u want show on view click event popupwindow.showAsDropDown(view, x, y);
public PopupWindow popupDisplay() {
final PopupWindow popupWindow = new PopupWindow(this);
// inflate your layout or dynamically add view
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.my_popupwindow_layout, null);
popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
return popupWindow;
}
And create this my_popupwindow_layout.xml file in the res/layout folder, which is used to inflate the custom layout for the popupWindow.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Window test" />
</LinearLayout>

ImageButton does not display menu items when pressing it

I have a custom ListView. Inside the layout of the custom ListView, I have an ImageButton which acts as an overflow menu (similar to how the Menu on the ActionBar works):
layout/item_list.xml
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_more_vert_black_24dp"
android:contentDescription="#string/descr_overflow_button"
android:id="#+id/overflowMenu"
android:layout_alignParentRight="true"/>
In the Activity, I configure this ImageButton as a Popup in the onCreate method:
overflowMenu = (ImageButton) findViewById(R.id.overflowMenu);
popupMenu = new PopupMenu(this, overflowMenu);
popupMenu.setOnMenuItemClickListener(this);
I also inflated it in onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = popupMenu.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popupMenu.getMenu());
return true;
}
I have the following in menu/popup_menu.xml folder:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/report" android:title="Report"
app:showAsAction="always"/>
<item android:id="#+id/share" android:title="Share"
app:showAsAction="always"/>
</menu>
And I added this:
#Override
public boolean onMenuItemClick(MenuItem item) {
return true;
}
However, when I try to click on the ImageButton on the phone, nothing happens. It does not display the menu items as it should. What am I missing?
Firstly PopupMenu is exactly what you need . From the docs
A popup menu displays a list of items in a vertical list that's
anchored to the view that invoked the menu. It's good for providing an
overflow of actions that relate to specific content or to provide
options for a second part of a command.
Now the reason why your implementation doesn't work is because onCreateOptionsMenu and onMenuItemClick are for managing the menu for the activity so inflating the overflow menu there is pointless.
What you need to do is attach an onClickListener to your ImageButton and initialize the PopupMenu and call show() inside your ListView/RecyclerView adapter
imageButton = findViewById(R.id.overflow_menu);
PopupMenu popup = new PopupMenu(this, imageButton);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popup.getMenu());
imageButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
popup.show();
}
});
You can have a look at the docs linked above for examples as well.
This Questions is duplication.
For custom layouts you can't use a menu, you have to use to a PopupWindow
Android MenuItem Custom Layout
PopupWindow popupwindow_obj = popupDisplay();
popupwindow_obj.showAsDropDown(clickbtn, -40, 18); // where u want show on view click event popupwindow.showAsDropDown(view, x, y);
public PopupWindow popupDisplay()
{
final PopupWindow popupWindow = new PopupWindow(this);
// inflate your layout or dynamically add view
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.mylayout, null);
Button item = (Button) view.findViewById(R.id.button1);
popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
return popupWindow;
}
// Create this XML file in the res/layout folder named my layout.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Window test" />
</LinearLayout>

Android - Popup menu when list item view pressed?

i would like to implement a popup menu similar to google's play store as shown below.
so basically from what i understand, i'll need an activity and a layout for this activity with a listview defined in it. i need to create my custom adapter. also, i need to create a list layout would contain the information and a view (with the 3 dots) that will serve as the button to launch the popup menu? the issue that i'm seeing here is that how do i create a listener for this view only and how do i reference the value for that specific list item in the list view.
i don't have any code available yet as i haven't started anything related to this. i'm currently getting info in theory for now but if required i will create a sample code.
thanks.
Using popup menu it's quite simple to create a menu with these three steps:
1 - Add a click listener to the menu button using OnClickListener or as i prefer from the layout xml:
<ImageButton android:id="#+id/menu_button" android:onClick="showMenu" ... />
2 - Create the menu layout menu_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/item_settings"
android:showAsAction="ifRoom|withText"
android:title="Settings"
android:visible="true"/>
<item
android:id="#+id/item_about"
android:showAsAction="ifRoom|withText"
android:title="About"
android:visible="true"/>
</menu>
3 - Create a popup menu, inflate the xml layout and show it:
public void showMenu (View view)
{
PopupMenu menu = new PopupMenu (this, view);
menu.setOnMenuItemClickListener (new PopupMenu.OnMenuItemClickListener ()
{
#Override
public boolean onMenuItemClick (MenuItem item)
{
int id = item.getItemId();
switch (id)
{
case R.id.item_settings: Log.i (Tag, "settings"); break;
case R.id.item_about: Log.i (Tag, "about"); break;
}
return true;
}
});
menu.inflate (R.menu.menu_layout);
menu.show();
}
ActionBarCompat List PopupMenu implementation is here (with back port available because it uses ABC)!
You can also get this sample from Github or from SDK (Mr.Morgan commented below)
/sdk/samples/android-19/ui/ActionBarCompat-ListPopupMenu. Make sure to
install Samples for SDK under Android 4.4.2 (API 19)
You can use like this:
public class MainActivity extends Activity {
ListView listView_Actions;
ArrayList<String> actionsArrayList;
Button btn_ViewPopUp;
ArrayAdapter<String> actionsAdapter;
static final int CUSTOM_DIALOG_ID1 = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_ViewPopUp=(Button) findViewById(R.id.btn_ViewPopUp);
actionsArrayList=new ArrayList<String>();
actionsArrayList.add("Action 1");
actionsArrayList.add("Action 2");
}
#Override
protected void onStart() {
super.onStart();
btn_ViewPopUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialog(CUSTOM_DIALOG_ID1);
actionsAdapter = new MyCustomBaseAdapter(getApplicationContext(), R.layout.list_actions, actionsArrayList);
listView_Actions.setAdapter(actionsAdapter);
}
});
}
#Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch (id) {
case CUSTOM_DIALOG_ID1:
dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setContentView(R.layout.list_actions);
listView_Actions = (ListView) dialog.findViewById(R.id.listView_Actions);
break;
}
return dialog;
}
class MyCustomBaseAdapter extends ArrayAdapter<String>
{
public MyCustomBaseAdapter(Context context, int textViewResourceId, ArrayList<String> actionsArrayList) {
super(context, textViewResourceId,actionsArrayList);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.action_list_cell, null);
final TextView lblContactAction;
lblContactAction = (TextView) v.findViewById(R.id.txtContactAction);
lblContactAction.append(actionsArrayList.get(position));
return v;
}
}
}
Now XML files:
action_list_cell.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:background="#android:color/background_light" >
<TextView
android:id="#+id/txtContactAction"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="18dp"
android:textColor="#android:color/black" />
</LinearLayout>
list_actions.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:background="#drawable/rounded_corner_top">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#DB6A16"
android:orientation="vertical"
android:paddingBottom="2dp"
android:paddingLeft="2dp"
android:paddingRight="2dp" >
<ListView
android:id="#+id/listView_Actions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >
</ListView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
first of all you need to make your custom adapter with a view that has the 3 dots.
then in the getView() or newView() method you set the listener to the 3 dots image.
i think that PopupMenu is what you are looking for, it's is supported since API 11.
if you want to support also earlier version of the API you can use PopupMenu class provided by the support library v7.
the usage is pretty straight forward.
you define it with the id of the view you want the menu to show next to, and then you can directly inflate a menu resource there as if it was a common menu.
Now showDialog is deprecated, use PopupMenu instead
And AppCompat PopupMenu f you want to support version before V11
public class MainActivity extends Activity {
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu
}
});//closing the setOnClickListener method
}
}
You have to set the Listener of the Button in the getView()-Method of your List-Adapter.
In this getView()-Method you assign a Layout to one List-Item. if you have done this, you just have to set the Listener on this View (Button), and handle the onClick() Event.
Not sure if i understand you correctly but you can trigger this method to open a pop up dialog with a listview.
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Title if Any");
builder.setItems(R.array.listoptions, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int itemClicked) {
String[] option_array = getResources().getStringArray(R.array.listoptions);
String optionSelected = option_array[itemClicked];
}
});
return builder.create();
}
See Adding a List
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="listoption">
<item>Install</item>
<item>Add to listview</item>
</string-array>
</resources>
Hope this helps.

How to add subview of some view in xml layout

I am a beginner programming android. I am searching for info how to create transparent sub view on current window layout.
I have created simple layout this is source:
<?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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple text" />
</LinearLayout>
Now i want to create a View when navigation button is pressed. That View i want to add on top of this layout with transparent about 40%. It should look something like this:
Also it should be easy to add Buttons, Drop box or else and i could easy remove this View.
Maybe someone did this and could me share ideas, how do do this ?
Thanks.
check out this and do programatically
view.getBackground().setAlpha(100); // to make background transparent
you can use PopupWindow to show on top of parent view as following
View mainview ;
PopupWindow popupwindow;
public void onCreate(Bundle savedInstanceState){
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mainview = inflater.inflate(R.layout.main, null, false);
setContentView(mainview);
// load sub menu from xml layout in popupwindow
View submenu_popup = inflater.inflate(R.layout.submenu_popup, null, false);
// make backgraound transparent of popup submenu
submenu_popup.getBackground().setAlpha(100);
popupwindow = new PopupWindow(submenu_popup ,300,300,false);
popupwindow.setOutsideTouchable(true);
popupwindow.setTouchable(true);
}
// call it on click of button or menu to show submenu
public void onClickButton(){
int x=0,y=0;
// show popupwindow on x, y position in main view (parent view) by using this
popupwindow.showAtLocation(mainview , Gravity.NO_GRAVITY, x, y);
}
May be you should learn how to use "android:theme" property

inflate popup menu items programmatically

I have a button in my activity, pressing upon which, the following method gets called -
private ArrayList<ListView> myListViewList;
private ArrayList<MyListAdapter> myAdapterList;
public void onPopupButtonClick(View button) {
Log.v(TAG, "onPopupButtonClick");
PopupMenu popup = new PopupMenu(this, button);
Menu menu = popup.getMenu();
// how do I display the myListViewList as items of in this popup menu
// i.e how do I inflate myListViewList into menu object?
}
I know how to do this programmatically for an activity - creating linearlayouts and listViews and finally using addContentView on the activity's context.
But finding it hard to do the same for the popup menu scenario as described above.
Check this code for Pop-up window
LayoutInflater inflater = (LayoutInflater) PopupMain.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout = inflater.inflate(R.layout.popup,
(ViewGroup)findViewById(R.id.popup_menu_root));
PopupWindow pw = new PopupWindow(layout, 300,100, true);
pw.dismiss();
pw.showAtLocation(layout, Gravity.BOTTOM, 0, 10);
And your New pop-up layout should be as follows :
Popup.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_menu_root"
android:background="#FFFFFF"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="20dip" >
<TextView android:id="#+id/popup_menu_button1"
android:text="Copy"
android:textColor="#000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
This line decide the Height and width of your New View(Pop up window)
PopupWindow pw = new PopupWindow(layout, 300,100, true);
and pw.showAtLocation(layout, Gravity.BOTTOM, 0, 10);
tells where the Pop-up window should displays..

Categories

Resources