Include layout inside popup menu items [duplicate] - android

This question already has answers here:
How to make an overflow menu like in Chrome?
(2 answers)
Closed 4 years ago.
I try to add a popup menu in my service, for some features. I want to add some horizontal icons. I tried to add them in a layout and after include that layout in a menu item. But nothing.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_id"
android:title="TEST"
app:showAsAction="always"
app:actionLayout="#layout/menu_navigation"/>
<item android:id="#+id/downloads"
android:title="#string/downloads"
android:icon="#drawable/ic_download"
app:showAsAction="always|withText"/>
<item android:id="#+id/settings"
android:title="#string/settings"
android:icon="#drawable/ic_settings"
app:showAsAction="always|withText" />
</menu>

try to use Popup Window use custom layout, this way you will be able to add icons
LayoutInflater layoutInflater = (LayoutInflater)
MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customView = layoutInflater.inflate(R.layout.pop_window_layout,null);
Button but1 = (Button)customView.findViewById(R.id.popwinitem1);
Button but2 = (Button)customView.findViewById(R.id.popwinitem2);
Button but3 = (Button)customView.findViewById(R.id.popwinitem3);
//instantiate popup window
final PopupWindow popupWindow = new PopupWindow(customView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//display the popup window below whatever widget you want
popupWindow.showAsDropDown(text1);
//popupWindow.showAtLocation(holder.text1, Gravity.RIGHT, 0, 0);
//close the popup window on button click
but1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// yourfunction();
popupWindow.dismiss();
}
});
but2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// yourfunction();
popupWindow.dismiss();
}
});
but3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
popupWindow.dismiss();
}
});
pop_window_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/colorWhite"
android:padding="10dp">
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/popwinitem1"
android:padding="5dp"
android:textSize="15sp"
android:text="#string/save"
android:textColor="#color/colorBlack"/>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/popwinitem2"
android:text="#string/delete"
android:textSize="15sp"
android:padding="5dp"
android:textColor="#color/colorBlack"/>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/popwinitem3"
android:text="#string/cancel"
android:padding="5dp"
android:textSize="15sp"
android:textColor="#color/colorBlack"/>

Related

Set Google style bottomsheet Dialog in Android Studio App?

This is my code that gives a BottomSheet Dialog. After implementation it gives me a normal BottomSheet Dialog, how can i make this Bottom Sheet look like Google BottomSheet Dialog with rounded corners and something like a middle gray line at the top?
Even if it doesn't look exactly like the Bottomsheet Facebook and Google uses, I just want to have a rounded corner for this code.
What do I change or add in my code?
MainActivity.Java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openDialog();
}
});
}
private void openDialog() {
View view = getLayoutInflater().inflate(R.layout.sheet_main, null);
dialog = new BottomSheetDialog(this);
dialog.setContentView(view);
TextView camera_sel = (TextView) view.findViewById(R.id.camera);
TextView gallery_sel = (TextView) view.findViewById(R.id.gallery);
camera_sel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
takePhotoFromCamera();
dialog.dismiss();
}
});
gallery_sel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
takePhotoFromGallery();
dialog.dismiss();
}
});
dialog.show();
}
sheet_main.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="horizontal"
android:padding="4dp">
<TextView
android:id="#+id/camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:drawablePadding="8dp"
android:drawableTop="#drawable/ic_camera"
android:gravity="center_horizontal"
android:padding="16dp"
android:text="Camera"
android:textSize="18sp" />
<TextView
android:id="#+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:drawablePadding="8dp"
android:drawableTop="#drawable/ic_panorama"
android:gravity="center_horizontal"
android:padding="16dp"
android:text="Gallery"
android:textSize="18sp" />
</LinearLayout>
UPDATE:
I was able to achieve this by adding these to my style.xml and setting it on the theme used for my dialog activity. Thanks!
<style name="HomeTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="bottomSheetDialogTheme">#style/ThemeOverlay.Demo.BottomSheetDialog</item>
</style>
<style
name="ThemeOverlay.Demo.BottomSheetDialog"
parent="#style/ThemeOverlay.MaterialComponents.BottomSheetDialog">
<item name="bottomSheetStyle">#style/Widget.Demo.BottomSheet</item>
</style>
<style name="Widget.Demo.BottomSheet" parent="Widget.MaterialComponents.BottomSheet">
<item name="shapeAppearanceOverlay">#style/ShapeAppearanceOverlay.Demo</item>
</style>
<style name="ShapeAppearanceOverlay.Demo" parent="">
<item name="cornerSize">18dp</item>
<item name="cornerFamily">rounded</item>
</style>

Android Custom Popup menu with switch

How can I customize the menuItems in a popup menu? I need a switch for the first menuitem. Here is what I got so far:
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="android.oli.com.fitnessapp.activities.LiveSelectActivity">
<item
android:icon="#drawable/ic_access_alarm_white_24dp"
android:orderInCategory="100"
app:showAsAction="always"
android:visible="true"
android:title="#string/action_settings"
android:onClick="showPopup"/>
</menu>
menu_popup.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/one"
android:title="One"/>
<item
android:id="#+id/setTime"
android:title="Two"
android:onClick="showTimePickerDialog"/>
</menu>
Activity snippet
public void showPopup(MenuItem menuItem){
View view = findViewById(R.id.action_alarm);
PopupMenu popup = new PopupMenu(this, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_popup, popup.getMenu());
popup.show();
}
You can use popupwindow since it allows to use custom layouts.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:padding="5dp">
<Switch
android:id="#+id/mySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="Play with the Switch" />
<TextView
android:id="#+id/switchStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/mySwitch"
android:layout_marginTop="22dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
and in ur activity implement this method:
/* you should refer to a view to stick your popup wherever u want.
** e.g. Button button = (Button) findviewbyId(R.id.btn);
** if(popupWindow != null)
** showPopup(button);
**/
public void showPopup(View v) {
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.popup_filter_layout, null);
popupWindow = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
//TODO do sth here on dismiss
}
});
popupWindow.showAsDropDown(v);
}

remove black border from inflated layout

I am using PopupWindow, which is working fine, but the problem is after inflating the layout view that is shown with black border.
final PopupWindow popupWindow = new PopupWindow(context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.pop_up_window_menu_layout, null);
popupWindow.setContentView(view);
pop_up_window_menu_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/solid_white"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:text="SAVE"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#color/black"
android:textSize="#dimen/txt_size_sliding_list" />
I've tried to remove border by using popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)), but problem is still same.
Can anyone tell me how to remove unwanted black border?
I don't see any border around my popup windows, but I do set them up slightly different than you. Hopefully this points you in the right direction.
popup_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#f5f5f5"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/btn_close"
android:text="Close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/main_view"
tools:context=".MainActivity">
<Button
android:id="#+id/btn_popup"
android:text="Popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/txt_text"
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
View popupView;
Button btnPopup;
Button btnClose;
PopupWindow popupWindow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
popupView = getLayoutInflater().inflate(R.layout.popup_view, null);
btnClose = (Button) popupView.findViewById(R.id.btn_close);
btnClose.setOnClickListener(this);
popupWindow = new PopupWindow(popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
btnPopup = (Button) findViewById(R.id.btn_popup);
btnPopup.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.btn_popup) {
popupWindow.showAtLocation(findViewById(R.id.main_view), Gravity.NO_GRAVITY, 100, 200);
//popupWindow.showAsDropDown(btnPopup, 10, 10);
} else if (v.getId() == R.id.btn_close) {
popupWindow.dismiss();
}
}
}
If you getting a black border this means that the default background is set.
this is how you remove it :
popup.setBackgroundDrawable(null);
Try this
Just try this and let me know..
Be sure to create your Pop-up window referencing your custom theme:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="DialogTheme" parent="android:Theme.Dialog">
<!-- Fill the screen -->
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<!-- No backgrounds and No titles , and no Window Floating -->
<item name="android:windowBackground">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<!-- Set background what you want-->
<item name="android:background">#ff0000</item>
</style>
</resources>
This is because you are sending context inside popup window constructor. -
final PopupWindow popupWindow = new PopupWindow(context);
Change this by popup window view -
View popupView = getLayoutInflater().inflate(R.layout.popup_view, null);
PopupWindow popupWindow = new Pop

How can I change popup item width size?

I use popup menu with custom items.Problems like this
Red lines represents to default width size.
but i want custom smaller width size of items(represents red lines).
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainLayout.this, mSikBtn);
popup.getMenuInflater().inflate(R.menu.poupup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
return true;
}
});
popup.show();//showing popup menu
}
});
Layout xml file
<ImageView
android:id="#+id/popupBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:src="#drawable/selector_sik"
android:layout_weight="1"
android:layout_margin="10dip"
/>
popup_menu.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/one"
android:title="A)."/>
...
<item
android:id="#+id/three"
android:title="E)."/>
</menu>
Similar question: stackoverflow.com
I need to custom popup menu
Just like you, I tried a lot to change the width of the popup menu but was unsuccessful.
Somehow, I found a solution you might be looking for.
First, instead of using popup menu, I used popup window.
Step1: Making a row layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="#android:drawable/list_selector_background"
android:orientation="vertical"
android:padding="3dp">
<TextView
android:id="#+id/ItemA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="15dp"
android:layout_marginTop="3dp"
android:text="A)"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/ItemB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:text="B)"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/ItemC"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:text="C)"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/ItemD"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:text="D)"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
</LinearLayout>
Step 2: Made a method to initialize popup window:
private PopupWindow initiatePopupWindow() {
try {
mInflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = mInflater.inflate(R.layout.row, null);
//If you want to add any listeners to your textviews, these are two //textviews.
final TextView itema = (TextView) layout.findViewById(R.id.ItemA);
final TextView itemb = (TextView) layout.findViewById(R.id.ItemB);
layout.measure(View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED);
mDropdown = new PopupWindow(layout,FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,true);
Drawable background = getResources().getDrawable(android.R.drawable.editbox_dropdown_dark_frame);
mDropdown.setBackgroundDrawable(background);
mDropdown.showAsDropDown(pop, 5, 5);
} catch (Exception e) {
e.printStackTrace();
}
return mDropdown;
}
Step 3: Simply calling it in the onCreate():
public class MainActivity extends Activity {
ImageButton red, blue;
private PopupWindow mDropdown = null;
LayoutInflater mInflater;
Button pop;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pop = (Button)findViewById(R.id.button1);
pop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
initiatePopupWindow();
}
});
}
}
Hope this helps you..If it does, accept my answer..:)
Here's the screenshot:
http://imageshack.com/a/img585/7388/dxjo.png
I don't think you can set layout:width in menu, so you might have to create your own view layout. This might help:
Set menu items centered and full-width with ActionBarSherlock
http://developer.android.com/guide/topics/resources/menu-resource.html

how to handle onclick event of button inside popup window in android

In my application, I have a button initially on the screen, and in onclick of the button, a popup window should open. In the popup window, I have an imagebutton, and onclick of this button, I want to start an activity. The popup window opens, but I don't understand how to handle the onclick of the imagebutton inside the popup window.
In main.xml, I have a button, and in popup_example.xml, I have an imagebutton.
My Java code is as follows:
final LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final Button b=(Button)findViewById(R.id.btn);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_example,(ViewGroup)findViewById(R.layout.main)));
pw.showAtLocation(v, Gravity.LEFT,0,0);
pw.update(8,-70,150,270);
//if onclick written here, it gives null pointer exception.
ImageButton img=(ImageButton)findViewById(R.id.home);
img.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent.....
}
});
//if onclick is written here it gives runtime exception.
});
and I have two xml layouts.........
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ghj" />
</LinearLayout>
popup_example.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#8E2323">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5px">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5px"
android:background="#000000">
<ImageButton android:id="#+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:src="#drawable/vitalss"
android:layout_weight="1"
android:background="#8E2323"/>
</TableLayout>
</TableLayout>
</LinearLayout>
You have to find the button into the Popup view:
View pview = inflater.inflate(R.layout.popup_example,(ViewGroup)findViewById(R.layout.main));
PopupWindow pw = new PopupWindow(pview);
pw.showAtLocation(v, Gravity.LEFT,0,0);
pw.update(8,-70,150,270);
//if onclick written here, it gives null pointer exception.
ImageButton img=(ImageButton)pview.findViewById(R.id.home);
img.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent.....
}
});
In main activity button onClick you can use:
popupWindow.getContentView().findViewById(R.id.buttonInPopup).setOnClickListener(new OnClickListener(...)
This will not give error in Inflater and work properly:
LayoutInflater layoutInflater = getLayoutInflater();
View pview = layoutInflater.inflate(R.layout.popup_example, (ViewGroup)findViewById(R.layout.main));
PopupWindow pw = new PopupWindow(pview);
pw.showAtLocation(v, Gravity.LEFT,0,0);
pw.update(8,-70,150,270);
//if onclick written here, it gives null pointer exception.
ImageButton img=(ImageButton)pview.findViewById(R.id.home);
img.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent.....
}
});
best solution :)
Pass onCclickMethod from xml
<ImageButton
android:id="#+id/save_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
**android:onClick="onClick"**
android:contentDescription="#string/save"
android:src="#drawable/save" />
it wroked from me..

Categories

Resources