Weird in popup menu - android

I have created a menu icon on action bar. When it is clicked, I expect it will show something like this
But it display on the left hand side instead.
What could be this issue ?
override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
inflater!!.inflate(R.menu.fragment_menu, menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.getItemId()
if (id == R.id.more) {
var popup: PopupMenu? = PopupMenu(context!!, view!!)
popup?.menuInflater?.inflate(R.menu.pop_up_menu, popup.menu)
popup?.show()
popup?.setOnMenuItemClickListener({ item: MenuItem? ->
when (item!!.itemId) {
R.id.logOut -> {
}
}
true
})
} else
super.onOptionsItemSelected(item)
return true
}
fragment_menu
<?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/more"
android:title="menu"
app:showAsAction="always"
android:icon="#drawable/ic_more"/>
</menu>
pop_up_menu
<?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/logOut"
app:showAsAction="always"
android:title="Log Out"
/>
</menu>
The view I passing is this
#Nullable
public View getView() {
return this.mView;
}

You are passing the wrong view as what #CodeRed mentioned.
Replace
var popup: PopupMenu? = PopupMenu(context!!, view!!)
to
val vItem = getActivity().findViewById<View>(R.id.more)
var popup: PopupMenu? = PopupMenu(context!!, vItem)

Menu in ActionBar is populated automatically by system, you don't need to write code to populate it.
To achieve the menu like figure 1, fragment_menu should be like:
<menu>
<item android:title="Search" />
<item android:title="Locate" />
<item android:title="Refresh" />
<item android:title="Help" />
<item android:title="Check for update" />
</menu>
onOptionItemSelected is used to handle when above menu is clicked, not used to show the menu.
popup menu is free position menu like right click menus in windows, not the one you expect in figure 1.

Related

Android Hotwire: Add menu options to custom menu in WebFragment

I have extended the res/menu/web with a logout option:
<?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"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="#+id/menu_progress"
android:title="#string/loading"
android:visible="false"
app:actionLayout="#layout/toolbar_progress"
app:showAsAction="always"
tools:ignore="AlwaysShowAction" />
<item
android:id="#+id/logoutButton"
android:title="Logout"
app:showAsAction="always"/>
</menu>
In the WebFragment I want to handle the logout, but the action is not being called. I'm trying this by doing:
private fun setupMenu() {
toolbarForNavigation()?.inflateMenu(R.menu.web)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
Log.d("WebFragment", "Options item selected")
return when (item.itemId) {
R.id.logoutButton -> {
Log.i("WebFragment", "Logout button pressed")
requireActivity().startNewActivity(PasswordActivity::class.java)
true
}
else -> super.onOptionsItemSelected(item)
}
}
I have tried solving this by checking this StackOverflow topic, but no luck. Therefor, I am wondering if it is even possible with this package.

How to show SearchView from Toolbar only in one Fragment?

In my app i have one activity with two fragment, the app has a toolbar with search action, that search action must be visible only in the second fragment.
So how could i hide the search button and show it only when i'm in fragment2?
My menu.xml looks like this:
<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="it.gabtamagnini.visualstock.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/app_bar_search"
android:icon="#drawable/ic_search_black_24dp"
android:title="#string/cerca"
app:showAsAction="ifRoom|withText"
app:actionViewClass="android.widget.SearchView" />
</menu>
I'm using kotlin
In your second fragment:
#Override
public void onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.app_bar_search);
if(item != null)
{
item.setVisible(false);
}
}
I solved by adding the following code in my onViewCreated
setHasOptionsMenu(true)
And i had to override the onPrepareOptionsMenu like this:
override fun onPrepareOptionsMenu(menu: Menu) {
super.onPrepareOptionsMenu(menu)
val item: MenuItem = menu.findItem(R.id.app_bar_search)
item.isVisible = true
}

Options menu not responding to touch

I created a options menu in my toolbar which shows up with 3 dots icon. I has 2 problems. First, it's not looking like options menu in other apps (text misaligned) and when I run the app on phone it doesn't responds to touch, even the ripples don't show up. On the other hand options menu works when I run it in the emulator(Pixel 2 API 24)
Here's my MainActivity code:
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.options_menu, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.option_about -> {
Toast.makeText(this, "About", Toast.LENGTH_SHORT).show()
return true
}
R.id.option_exit -> {
Toast.makeText(this, "Exit", Toast.LENGTH_SHORT).show()
return true
}
else -> super.onOptionsItemSelected(item)
}
}
Here is the options_menu.xml
<?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/search"
android:icon="#drawable/ic_search_primary"
android:title="#string/search_title"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom" />
<item
android:id="#+id/option_about"
android:title="About"
app:showAsAction="never" />
<item
android:id="#+id/option_exit"
android:title="Exit"
app:showAsAction="never" />
</menu>
Actually, I was using wrong popupTheme in my toolbar.

Menu always hidden to three dots instead of shown in action bar

I have an activity with navigation drawer. The activity contain a view pager that hold 3 fragment. I've inflate the menu successfully. But, instead of showing the icon in action bar, it's hidden in three dots.
I've menu code below :
home_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/search_menu"
android:icon="#drawable/baseline_search_24"
android:title="Search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView">
</item>
</menu>
onCreateOptionsMenu() code :
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val inflater = MenuInflater(this)
inflater.inflate(R.menu.home_menu, menu)
val searchItem: MenuItem? = menu!!.findItem(R.id.search_menu)
searchItem?.let {
it.actionView?.let {
val searchView: SearchView = it as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(p0: String?): Boolean {
return false
}
override fun onQueryTextChange(p0: String?): Boolean {
return false
}
})
}
}
return true
}
What's wrong ?
Is another menu file will affected to that menu ?
Here's another menu files :
activity_home_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<item
android:id="#+id/admin_menu"
android:visible="true"
android:title="Admin Menu">
<menu>
<item
android:id="#+id/manage_user"
android:icon="#drawable/ic_group_gray_24dp"
android:title="Manage User" />
<item
android:id="#+id/manage_schedule"
android:icon="#drawable/baseline_calendar_today_24"
android:title="Manage Schedule" />
</menu>
</item>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_edit_profile"
android:icon="#drawable/baseline_edit_24"
android:title="Edit Profile" />
<item
android:id="#+id/nav_logout"
android:icon="#drawable/baseline_exit_to_app_24"
android:title="Logout" />
</group>
</menu>
bottom_navigation_drawer_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/home_menu"
android:icon="#drawable/baseline_home_24"
android:title="Home"/>
<item
android:id="#+id/search_user_menu"
android:icon="#drawable/baseline_search_24"
android:title="Search User"/>
<item
android:id="#+id/schedule_request_menu"
android:icon="#drawable/baseline_inbox_24"
android:title="Request"/>
</menu>
If you want to show a menu icon then try this.
you should add showAsAction attribute to always or ifRoom
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_refresh"
android:showAsAction="ifRoom"
android:title="#string/action_refresh"/>
<item
android:id="#+id/action_filter"
android:icon="#drawable/ic_filter"
android:showAsAction="always"
android:title="#string/action_filter"/>
</menu>

Custom suggestions in SearchView in menu

I have a searchview in my menu (toolbar). It performs a search query in my recycler view and it works fine. I need to add a custom search suggestions to my searchview and I can't find any way to add it to a default searchview menu item. I have shared the code of my menu items and search query.
Menu XML file:
<?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/actionSearch"
android:title=""
android:icon="#drawable/ic_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"/>
<item
android:id="#+id/actionAdd"
android:title=""
android:icon="#drawable/ic_add"
app:showAsAction="always|collapseActionView"/>
<item
android:id="#+id/actionImport"
android:orderInCategory="100"
android:title="Import from phone"
app:showAsAction="never"/>
<item
android:id="#+id/actionExport"
android:orderInCategory="100"
android:title="Export as VCF"
app:showAsAction="never" />
<item
android:id="#+id/actionSettings"
android:orderInCategory="100"
android:title="Settings"
app:showAsAction="never" />
</menu>
Search Query:
private fun search(searchView: SearchView) {
searchView.queryHint = "Search Here"
searchView.setIconifiedByDefault(false)
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean = false
override fun onQueryTextChange(newText: String): Boolean {
initRecentRecyclerView()
mAdapter2.getFilter().filter(newText)
recentAdapter.getFilter().filter(newText)
return true
}})
MenuItemCompat.setOnActionExpandListener(search, object : MenuItemCompat.OnActionExpandListener {
override fun onMenuItemActionExpand(item: MenuItem): Boolean {
initRecentRecyclerView()
return true
}
override fun onMenuItemActionCollapse(item: MenuItem): Boolean {
recentRecyclerView.visibility = View.GONE
return true
}
})
searchView.setOnFocusChangeListener({ _: View, b: Boolean ->
if(!b){
recentRecyclerView.visibility = View.GONE
}
})
searchView.setOnCloseListener{
recentRecyclerView.visibility = View.GONE
true
}
searchView.setOnQueryTextFocusChangeListener({ view: View, b: Boolean ->
if(!b){
try {
recentRecyclerView.visibility = View.GONE
}catch(e: Exception){}
}
})
}
actionSearch is the id of my searchView, searchView is the reference I have used in my kotlin code. I need to show a list of suggestions in my searchview. Hope that someone can help me out!

Categories

Resources