Android add listview to OptionsMenu - android

I want to add a ListView to the options menu so when clicked it will pop up and allow the user to scroll through it and interact it without the same as any other listview.
using something this for the XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="#+id/menuGroupsList" android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</menu>
and this for the code:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menu, menu);
ListView list = (ListView)findViewById(R.id.menuGroupsList);
}
returns null from the findViewById.
Is this at all possible?
Thanks

Don't use MenuInflater. Instead, create a custom layout with a list in it.
Override onPrepareOptionsMenu(..) and create a Dialog which you inflate with your custom layout.
Dialog dlg = new Dialog(context);
dlg.setContentView(R.layout.splashscreen);
dlg.show();

Related

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>

Apply a layout to ActionBar menu item and make it clickable

I am trying to use icon fonts with ActionBar menu item using a custom layout. Applying a layout to the menu item does solve the problem and the icon font renders perfect but doing so make the menu item un-clickable. Is there a way to solve this?
I am trying to avoid using a custom action bar because I want to use font icons instead of images.
Here is my code so far:
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="48dp"
android:layout_height="match_parent"
android:layout_gravity="right">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#style/action_bar_icons"
android:text="#string/ic_sliders"
android:clickable="true"/>
</RelativeLayout>
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="com.hackerrank.projectx.SearchResultsActivity">
<item
android:id="#+id/action_filters"
android:title="#string/action_filters"
android:showAsAction="always"
android:onClick="onFilterClick"
android:actionLayout="#layout/action_search_result_filter" />
<item
android:id="#+id/action_toggle_view"
android:title="#string/action_custom_view"
/>
</menu>
You Can setOnClickListener on the actionLayout something like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(mMenuResource, menu);
MenuItem item = menu.findItem(R.id.notfication);
FrameLayout badgeLayout = (FrameLayout) MenuItemCompat.getActionView(item);
badgeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent not = new Intent(DrawerActivity.this,NotificationActivity.class);
not.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(not);
}
});
return super.onCreateOptionsMenu(menu);
}
If you are using it inside a fragment try this:
onCreateView()
{
setHasOptionMenu(true);
...`enter code here`
}

How change position of popup menu on android overflow button?

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.

Add ActionBar Items with a fade animation

I have a ViewPager where one of its belonging fragments has its own ActionBar item, so that when you slide to that page the item comes forth in the ActionBar, slide to another page the item goes away.
I would like this to happen with a fade-in/fade-out animation - but don't know how.
I've tried the following. But it gives me a NullPointerExecption on itemView.startAnimation(fade_in);
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.intruders_list, menu);
// Setup animation
Animation fade_in = AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in);
fade_in.setInterpolator(new AccelerateInterpolator());
fade_in.setDuration(250);
// Animate
MenuItem deleteItem = menu.findItem(R.id.action_delete);
View itemView = deleteItem.getActionView();
itemView.startAnimation(fade_in); // NPE HERE
super.onCreateOptionsMenu(menu, inflater);
}
You are getting a NullPointerException because you are trying to get an action view where none is set. getActionView() is returning null in you case.
To solve this issue you need to set one with deleteItem.setActionView(R.layout.layout_action_view);.
Alternatively you can set it with android:actionLayout="#layout/layout_action_view" on your item in intruders_list.xml.
layout_action_view.xml could look as simple as this:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/actionButtonStyle"
android:id="#+id/iv_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_menu_rotate" />

Is it possible to use sliding tabs and a spinner in the action bar in Android

I currently have an Activity with sliding tabs on it and I have been using ActionBarSherlock, but I wanted to have a spinner in the action bar, so that the user could select items from the spinner and each item is a specific file which opens up values to re-populate the views on each tab. I have looked to see how you can do this and so far have not had any luck. I don't want them to both navigate through fragments, but instead have the spinner to re-populate views on the tabs. I looked at a similar question Is it possible to use dropdown AND tabs as navigation in the action bar? which is how I want it to look, but his question was to use two different types of navigation. Please can anyone help me and let me know if this is possible. Thank you in advance. I have also just looked at an Android application that I have which is the Eurosport app and that has the type of layout that I am looking for if anyone has that application to see what I mean.
Here is my code:
public class SlidingTabsActivity extends SherlockFragmentActivity
{
ViewPager viewPager;
TabsAdapter tabsAdapter;
ActionBar actionBarTabs;
Spinner spinner;
ArrayAdapter<String> arrayAdapter;
LayoutInflater spinnerLayoutInflater;
View spinnerView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
viewPager = new ViewPager(this);
viewPager.setId(R.id.pager);
setContentView(viewPager);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, R.array.device_description);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerLayoutInflater = (LayoutInflater) this.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
spinnerView = spinnerLayoutInflater.inflate(R.layout.spinner, null);
spinner = (Spinner) spinnerView.findViewById(R.id.tabsSpinner);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
actionBarTabs = getSupportActionBar();
actionBarTabs.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBarTabs.setCustomView(spinnerView);
actionBarTabs.setDisplayShowCustomEnabled(true);
actionBarTabs.setDisplayHomeAsUpEnabled(true);
tabsAdapter = new TabsAdapter(this, viewPager); // Declares the tabs adapter class with the view pager view
/* Adds fragments to the tabs adapter */
tabsAdapter.addTab(actionBarTabs.newTab().setText("PV"), Fragment_1.class, null);
tabsAdapter.addTab(actionBarTabs.newTab().setText("CONFIG"), Fragment_2.class, null);
tabsAdapter.addTab(actionBarTabs.newTab().setText("DIAG"), Fragment_3.class, null);
}
Here is my Spinner code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="#+id/tabsSpinner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
I get this message in the logcat when pressing the Spinner:
11-26 12:22:44.282: W/InputEventReceiver(7217): Attempted to finish an input event but the input event receiver has already been disposed.
11-26 12:22:44.282: W/InputMethodManagerService(525): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy#4216a290 attribute=null, token = android.os.BinderProxy#42618330
So, first of all create a markup file to place your Spinner, e.g:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:spinnerMode="dropdown" >
</Spinner>
</RelativeLayout>
Next create a menu resource, e.g. main_menu.xml and put it to res/menu folder:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_change_content"
android:actionLayout="#layout/spinner_layout"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/empty">
</item>
</menu>
Paid your attention that we specified custom action layout for this menu item - this is a layout we have created earlier and it contains our Spinner. Next in your Activity(or Fragment) you must inflate this menu:
Use this if you creating this menu in Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
...// init spinner, will be explained below
return true;
}
Or if you creating menu in Fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.card_storage, menu);
...// init spinner
}
If you creating this menu for Fragment don't forget to call setHasOptionMenu(true) somewhere in Fragment.onCreate().
Put this instead of ... // init spinner
MenuItem item = menu.findItem(R.id.action_change_content); // id of our custom menu item
CustomSpinnerAdapter adapter = new CustomSpinnerAdapter(); // your adapter
Spinner spinner = (Spinner) item.getActionView().findViewById(R.id.spinner);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
I hope I didn't missed out something. Good luck!

Categories

Resources