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..
Related
I am trying to generate popup window containing dynamically generated text. log output clearly shows that the desired text is actually set to textView but I can't understand why it is not showing on the app.
MainActivity.java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
linearLayout = (LinearLayout) findViewById(R.id.main_activity_parent_layout);
int id = item.getItemId();
if(id == R.id.info_button){
layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
//Updating a textview in different layout xml from the main activity
LinearLayout vi = (LinearLayout)layoutInflater.inflate(R.layout.activity_info, null);
TextView textView = vi.findViewById(R.id.info);
textView.setText(R.string.main_activity_info);
Log.i("MainActivity",String.valueOf(textView.getText()));
ViewGroup container = (ViewGroup)layoutInflater.inflate(R.layout.activity_info,null);
popupWindow = new PopupWindow(container, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//Note showAtLocation method should always in end for proper rendering of poup Window
popupWindow.showAtLocation(linearLayout,Gravity.TOP,0,100);
}
return super.onOptionsItemSelected(item);
}
activity_info.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:padding="8dp"
android:text="hello"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#android:color/black"/>
</LinearLayout>
popup window show hello which is hardcoded text.
You are inflating R.layout.activity_info two times and losing the TextView you are changing in the process. Make the following change to keep the first inflation and your modification:
popupWindow = new PopupWindow(vi, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,true);
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>
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>
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.
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