Android Custom Popup menu with switch - android

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);
}

Related

Include layout inside popup menu items [duplicate]

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"/>

How do I insert layout into Android menu item row?

How do I insert a layout into an Android menu item row?
I don't know how to create a layout like this.
Here's what I want:
Create a custom layout like this:
<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>
In your Activity, implement the following code:
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);
}

change background color single item menu

I want popup menu like this
that show it after click on button
XML
<item
android:state_pressed="true"
android:id="#+id/fromFirstMonth"
android:title="از ابتدای سال"
android:drawable="#drawable/nav_item_background"/>
<item
android:state_pressed="true"
android:id="#+id/currentMonth"
android:title="این ماه"
android:drawable="#color/blueMenu"/>
<item
xmlns:showAsAction="always"
android:id="#+id/currentSession"
android:title="این فصل"
android:drawable="#color/white"/>
<item
xmlns:showAsAction="always"
android:id="#+id/selection"
android:title="تانتخابی"
android:drawable="#color/blueMenu"/>
</menu>
Java
hourglass.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(Products.this, hourglass);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.hourglass_item, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(Products.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu
}
});
I want set different background color for each item. i set android:drawable but this does not worked.
I can with this code can change text color but i does not know how can change background color item
Menu menu=popup.getMenu();
MenuItem item = menu.getItem(0);
SpannableString s = new SpannableString("My red MenuItem");
s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0);
item.setTitle(s);
If you want to use item tags and do this,as an alternative way you can tryBackgroundColorSpan and write the logic to reformat of the length of your strings.
s.setSpan(new BackgroundColorSpan(Color.RED), 0, s.length(), 0);
out put:
else
If i do that i'll crate a Dialog without a Title
private Dialog fakeDialogUseInstedOfMenuItem;
fakeDialogUseInstedOfMenuItem = new Dialog(MainActivity.this);
fakeDialogUseInstedOfMenuItem.requestWindowFeature(Window.FEATURE_NO_TITLE); // no Title
fakeDialogUseInstedOfMenuItem.setContentView(R.layout.my_custom_view);
and set a fixed or scroll view for that as the requirement
my_custom_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="200dp"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#789"
android:orientation="vertical">
<LinearLayout
android:id="#+id/first_lin"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option One"
android:textColor="#000"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FFF"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option Two"
android:textColor="#000"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FFF"></LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
and access items like this,
LinearLayout linearLayoutOneInDialog = (LinearLayout) fakeDialogUseInstedOfMenuItem.findViewById(R.id.first_lin);
out put:
But these things are optional ways

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

Categories

Resources