Android searchview returns null - android

I am trying to use a SearchView in my actionbar (for only one fragment) but the SearchView keeps on returning null.
fragment
import android.os.Bundle
import android.view.*
import androidx.appcompat.widget.SearchView
import androidx.fragment.app.Fragment
import com.StockMarketAlarms.StockAlarm.R
import com.google.firebase.auth.FirebaseAuth
class SearchFragment : Fragment() {
private lateinit var auth: FirebaseAuth
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_search, container, false)
setHasOptionsMenu(true)
return root
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.search_menu, menu)
val searchView: SearchView = menu.findItem(R.id.menuitem_search) as SearchView
searchView.setMaxWidth(Int.MAX_VALUE)
}
}
its loaded in a tabactivity that extends AppCompatActivity
The search menu looks like this
search_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"
xmlns:tools="http://schemas.android.com/tools">
<android.support.v7.widget.SearchView
android:id="#+id/menuitem_search"
android:icon="#drawable/search_black_48"
app:actionViewClass="android.widget.SearchView"
android:title="#string/title_search"
app:searchIcon="#drawable/search_black_48"
app:showAsAction="collapseActionView|ifRoom"
app:queryBackground="#color/colorWhite"
app:iconifiedByDefault="false" />
</menu>
I dont understand why it returns null, I tried a lot.
Process: com.jfvh.stockalarm, PID: 1845
kotlin.TypeCastException: null cannot be cast to non-null type androidx.appcompat.widget.SearchView
at com.jfvh.stockalarm.activities.fragments.SearchFragment.onCreateOptionsMenu(SearchFragment.kt:48)
at androidx.fragment.app.Fragment.performCreateOptionsMenu(Fragment.java:2711)
at androidx.fragment.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManagerImpl.java:2711)
at androidx.fragment.app.Fragment.performCreateOptionsMenu(Fragment.java:2713)
at androidx.fragment.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManagerImpl.java:2711)
at androidx.fragment.app.FragmentController.dispatchCreateOptionsMenu(FragmentController.java:386)
at androidx.fragment.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:326)
at androidx.appcompat.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94)
at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImpl.java:2830)
at androidx.appcompat.app.AppCompatDelegateImpl.preparePanel(AppCompatDelegateImpl.java:1746)
at androidx.appcompat.app.AppCompatDelegateImpl.doInvalidatePanelMenu(AppCompatDelegateImpl.java:2026)
at androidx.appcompat.app.AppCompatDelegateImpl$2.run(AppCompatDelegateImpl.java:260)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

search_menu.xml has an error.
It should be:
<?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">
<!-- tag name should be <item> -->
<item
android:id="#+id/menuitem_search"
android:icon="#drawable/search_black_48"
app:actionViewClass="android.widget.SearchView"
android:title="#string/title_search"
app:searchIcon="#drawable/search_black_48"
app:showAsAction="collapseActionView|ifRoom"
app:queryBackground="#color/colorWhite"
app:iconifiedByDefault="false"/>
</menu>
Source: https://developer.android.com/training/search/setup#add-sv
Menus allow only two tags <group>, <item>:
And for action view class you can select basically anything. Note, that actionViewClass attribute uses app namespace prefix:
UPDATE
I noticed one more problem, that is in your fragment. When you find menu item by its id it does not return you an action view, instead what you get is MenuItem instance. It cannot be casted to any kind of View.
To fix this issue you should get the value of actionView attribute of MenuItem object.
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.search_menu, menu)
// `.actionView` was missing
val searchView: SearchView = menu.findItem(R.id.menuitem_search).actionView as SearchView
searchView.setMaxWidth(Int.MAX_VALUE)
}

Related

Can't show/hide menu items on Kotlin - Android Studio

my application has a menu of 3 options, and what I want is that according to a condition that I define, it shows only 2 options, or it shows 3.
I have tried all the options that I have found on google and here, but none have worked for me.
My idea is that as soon as the view is created, the menu already shows the options that it can show according to the condition. But as I have been able to achieve it, I added a button to simulate the event.
I would greatly appreciate your help.
Here I attach the code:
1) activity_home.xml (some part)
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="ActivarMenus"/>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:menu="#menu/home_menu"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" >
2) Aqui el archivo home_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_item"
android:icon="#drawable/ic_baseline_more_vert_24"
app:showAsAction="ifRoom"
tools:ignore="MenuTitle">
<menu>
<item
android:id="#+id/newRegister"
android:icon="#drawable/ic_baseline_add_24"
app:showAsAction="always"
android:visible="true"
android:title="Nueva Orden Swab" />
<item
android:id="#+id/newAnexoo"
android:icon="#drawable/ic_baseline_add_24"
app:showAsAction="always"
android:visible="true"
android:title="Nuevo Formulario Anexo O" />
<item
android:id="#+id/sync"
android:icon="#drawable/ic_baseline_sync_24"
app:showAsAction="always"
android:visible="true"
android:title="Sincronizar" />
</menu>
</item>
</menu>
3) And a part of the HomeActivity.kt
class HomeActivity : ActivityViewBinding<ActivityHomeBinding, HomeVM>() {
private var mainMenu: Menu? = null
private lateinit var spm : SharedPreferencesManager
private val adapter: OrderAdapter by inject()
override fun inflateLayout(layoutInflater: LayoutInflater) =
ActivityHomeBinding.inflate(layoutInflater)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
//MenuMain.findItem(R.id.newAnexoo).isVisible = true
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater: MenuInflater = menuInflater
inflater.inflate(R.menu.home_menu, menu)
mainMenu = menu;
return true
}
fun ActivarMenus(view: View) {
mainMenu?.findItem(R.id.newRegister)?.isVisible = true
}
You should try this one it might be work.
if you want to change Menu Items at Run time You can use onPrepareOptionsMenu
#Override
public boolean onPrepareOptionsMenu(Menu menu){
if (//Your condition) {
menu.findItem(R.id.newRegister).setVisible(true);
}else {
menu.findItem(R.id.newRegister).setVisible(false);
}
return true;
}

Unable to show menu icons in appbar

I want to show search icon in app bar for implementing search functionality.I have created menu resource file and inflate that in MainActivity but unable to see search icon.
Below is my code:
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"
android:title="Item"
app:showAsAction="always"
app:actionViewClass="androidx.appcompat.widget.SearchView"/>
</menu>
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu,menu)
return true
}
}
Someone let me know why icon is not showing.
in your Activity's onCreate add this:
setSupportActionBar(binding.IdentifierOfYourToolBarInYourMainActivityXML)

ToolBar menu item click in a fragment (Kotlin)

In my app, i have an Activity, which has a FrameLayout in it. In this FrameLayout, there is a fragment, containing a ToolBar and a RecyclerView.
In this toolbar, i have a search button, which should start an Activity on item click. However, when i try to use onOptionsItemSelected, the apps gets built and installed succesfully, but when i try to tap that button in question, nothing happens. The Logcat, doesnt say anything either.
Can some points me what im doing wrong? Are there simpler or other easy ways to manage on ToolBar item clicks?
Fragment.kt
class FragmentTrack : Fragment() {
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater.inflate(R.layout.fragment_track, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
...
topToolbar.setNavigationOnClickListener {
val dialog = FragmentBottomSheetDrawer()
dialog.show(childFragmentManager, dialog.tag)
}
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.menu_toolbar, menu)
super.onCreateOptionsMenu(menu, inflater)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when(item.itemId) {
R.id.fsvSearch -> Toast.makeText(context, "Clicked search button", Toast.LENGTH_SHORT).show()
}
return true
}
}
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:theme="#style/Theme.Design.NoActionBar">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/topToolbar"
android:background="#color/colorPrimaryDark"
app:navigationIcon="#drawable/ic_outline_menu_24"
app:popupTheme="#style/popupMenuThemeDark"
app:titleTextColor="#android:color/white"
app:title="Revo"
app:menu="#menu/menu_toolbar"
app:layout_scrollFlags="scroll|enterAlways" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvTracks"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:clipToPadding="false"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
toolbar_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/fsvSearch"
android:icon="#drawable/ic_search_24px"
android:title="#string/search"
app:showAsAction="always"/>
<item
android:id="#+id/fsvOrder"
android:icon="#drawable/ic_sort_24px"
android:title="#string/sort"
app:showAsAction="ifRoom">
<menu>
<group android:id="#+id/sortMenu" android:checkableBehavior="single">
<item
android:id="#+id/sortIncrease"
android:title="#string/increasing"/>
<item
android:id="#+id/sortDecrease"
android:title="#string/decreasing"/>
<item
android:id="#+id/sortMArtist"
android:title="#string/artist"/>
<item
android:id="#+id/sortAlbum"
android:title="#string/albums"/>
<item
android:id="#+id/sortYear"
android:title="#string/year"/>
<item
android:id="#+id/sortDate"
android:title="#string/date"/>
</group>
</menu>
</item>
<item
android:id="#+id/fsvGrid"
android:icon="#drawable/ic_grid_on_24px"
android:title="#string/grid"
app:showAsAction="ifRoom">
<menu>
<group android:id="#+id/gridMenu" android:checkableBehavior="single">
<item
android:id="#+id/gridOne"
android:title="1"/>
<item
android:id="#+id/gridTwo"
android:title="2"/>
<item
android:id="#+id/gridThree"
android:title="3"/>
<item
android:id="#+id/gridFour"
android:title="4"/>
</group>
</menu>
</item>
</menu>
I found a solution thanks to some external help. Its possible to work on the Toolbars item in an easier way.
In the onViewCreated method, we must add:
topToolbar.inflateMenu(R.menu.menu_toolbar)
topToolbar.setOnMenuItemClickListener {
when(it.itemId) {
R.id.fsvSearch -> //your code
}
true
}
Also, if the menu gets duplicated, remove the app:menu tag in the Toolbars xml
thanks Meltix for this, I've done a lot of research but most of the posts in SO are about Java and old fashioned ActionBar. There is not much info about material ToolBar and kotlin examples.
In my case I'm working on an old app developed mostly in Java but now developing new functionalities in Kotlin. In my fragment, I wasn't able to change the ActionBar's icons and behaviour (I wanted a particular ActionBar for my fragment). The solution I found is to hide the ActionBar and create a Toolbar inflating a custom menu inside. Also I added a back arrow button (thanks to John: here).
I've came to the conclusion (maybe I'm wrong) that you can't manage ActionBars from kotlin fragments, that's why you have to use toolbars. Here is my code in case it can help someone.
MyFragment.kt
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// hide the old actionBar
(activity as AppCompatActivity).supportActionBar!!.hide()
// create the toolbar
val toolbar = binding?.myToolbar
// add the back arrow button, see function below
val resId = getResIdFromAttribute(toolbar.context, android.R.attr.homeAsUpIndicator)
toolbar.navigationIcon = ContextCompat.getDrawable(toolbar.context, resId)
// tapping on the arrow will pop the current fragment
toolbar.setNavigationOnClickListener { parentFragmentManager?.popBackStackImmediate() }
// change toolbar title
toolbar.title = "Some title"
// inflate a custom menu, see code below
toolbar.inflateMenu(R.menu.my_custom_menu)
toolbar.setOnMenuItemClickListener {
when (it.itemId) {
R.id.infoButton -> someAction()
}
true
}
}
// get back arrow icon
#AnyRes
fun getResIdFromAttribute(context: Context, #AttrRes attr: Int): Int {
if (attr == 0) return 0
val typedValueAttr = TypedValue()
context.theme.resolveAttribute(attr, typedValueAttr, true)
return typedValueAttr.resourceId
}
my_fragment.xml
...
<androidx.appcompat.widget.Toolbar
android:id="#+id/myToolBar"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:visibility="visible"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:background="#color/azulOscuro"
>
</androidx.appcompat.widget.Toolbar>
my_custom_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:jsmovil="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="#+id/infoButton"
android:icon="#drawable/ic_info_blanco"
android:title="#string/tutorial"
android:textColor="#color/blanco"
jsmovil:showAsAction="always"
/>
</menu>
Result

OptionsMenu disappears from fragment on orientation change

Icons in the option menu work just fine, but when you change orientation by rotating the screen the icons disappear.
class SingleRecipeFragment : Fragment() {
private lateinit var viewModel: SingleRecipeViewModel
private lateinit var viewModelFactory: SingleRecipeViewModelFactory
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding: FragmentSingleRecipeBinding = DataBindingUtil.inflate(inflater,
R.layout.fragment_single_recipe, container, false)
val args =
SingleRecipeFragmentArgs.fromBundle(
requireArguments()
)
val application = requireNotNull(this.activity).application
viewModelFactory =
SingleRecipeViewModelFactory(
args.recipeIndex, application
)
viewModel = ViewModelProvider(this,viewModelFactory)
.get(SingleRecipeViewModel::class.java)
binding.singleRecipeViewModel = viewModel
binding.lifecycleOwner = this
viewModel.curRecipe.observe(viewLifecycleOwner, Observer {
(requireActivity() as MainActivity).toolbar.title = it?.title
})
//Toast.makeText(context, "Recipe Number: ${args.recipeIndex}",Toast.LENGTH_LONG).show()
setHasOptionsMenu(true)
return binding.root
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.single_recipe_menu, menu)
viewModel.curRecipe.observe(viewLifecycleOwner, Observer {
val favIconDrawable: Int = if (it!!.favorite) R.drawable.ic_baseline_star_filled_24
else R.drawable.ic_baseline_star_border_24
menu.findItem(R.id.favorite).setIcon(favIconDrawable)
})
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.share -> viewModel.shareSuccess(this.requireActivity())
R.id.favorite -> viewModel.toggleFavorite()
}
return super.onOptionsItemSelected(item)
}
}
Here's the menu layout:
<?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/share"
android:enabled="true"
android:icon="#drawable/ic_baseline_share_24"
android:title="Share"
android:visible="true"
app:showAsAction="ifRoom" />
<item
android:id="#+id/favorite"
android:enabled="true"
android:icon="#drawable/ic_baseline_star_border_24"
android:title="Share"
android:visible="true"
app:showAsAction="ifRoom" />
</menu>
This fragment is launched from another fragment by NavigationUI which passes in the recipeID value. I've tried using setHasOptionsMenu(true) in the onResume of this fragment but it has no effect. Any idea why the optionsMenu is not persistent?
It seems that using setSupportActionBar(toolbar) in the onCreate() method of the MainActivity fixes the problem with the optionsMenu not being persistent. However, this breaks the method I was using to display the title on fragment windows.
The toolbar belongs to the Activity, not the fragment. So when the view is recreated after the orientation change the toolbar is being set to the app title. This doesn't happen when the fragment is first loaded since the title was being set by the fragment's onCreate(). So the solution to that was to move the custom titles to onResume() of the fragment so that it is reset ever time the view is shown on the screen.
I'm setting the title in the fragment like this, because if I let the Navigation Controller do it using the labels in navigation.xml I get a strange behavior when the fragment loads up where the custom toolbar layout being replace by the new fragment title kind of flashes out to the right.
So the boiled-down code for this solution is:
Fragment:
class SingleRecipeFragment : Fragment() {
override fun onCreateView(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
override fun onResume() {
super.onResume()
/** This will reset your title every time the fragment is redrawn **/
(requireActivity() as MainActivity).toolbar.title = "Some Custom Title"
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.single_recipe_menu, menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.share -> someMethodYouWantToRun()
R.id.favorite -> someOtherMethod()
}
return super.onOptionsItemSelected(item)
}
}
In onCreate() of your Activity, call:
setSupportActionBar(toolbar)
And here's the menu layout resource file being inflated for the options menu (no change from what's in the question):
<?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/share"
android:enabled="true"
android:icon="#drawable/ic_baseline_share_24"
android:title="Share"
android:visible="true"
app:showAsAction="ifRoom" />
<item
android:id="#+id/favorite"
android:enabled="true"
android:icon="#drawable/ic_baseline_star_border_24"
android:title="Share"
android:visible="true"
app:showAsAction="ifRoom" />
</menu>

android.content.res.Resources$NotFoundException: Resource ID #0xffffffff. cannot make search view with query

please help. i got this error but don't know what is going on. i tried to make a search view in my fragment.
i make this code for menu item
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search"
android:title="search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
and make this code in my fragment
override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
menu?.clear()
inflater?.inflate(R.menu.menu_home, menu)
val searchItem = menu?.findItem(R.id.action_search)
val searchView = MenuItemCompat.getActionView(searchItem) as SearchView
searchView.backgroundColorResource = Color.WHITE
searchView.queryHint = "Search Review Title"
searchView.setOnQueryTextListener(object: SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
presenter.getSearchReviewList(query)
return true
}
override fun onQueryTextChange(query: String?): Boolean {
presenter.getSearchReviewList(query)
return true
}
})
super.onCreateOptionsMenu(menu, inflater)
}
but i got this error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mqa.android.moviereview, PID: 22280
android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:204)
at android.content.res.Resources.getColor(Resources.java:949)
at android.content.res.Resources.getColor(Resources.java:925)
at org.jetbrains.anko.CustomViewPropertiesKt.setBackgroundColorResource(CustomViewProperties.kt:36)
at com.mqa.android.moviereview.module.fragment.home.HomeFragment.onCreateOptionsMenu(HomeFragment.kt:97)
at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:2561)
at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:3321)
at android.support.v4.app.FragmentController.dispatchCreateOptionsMenu(FragmentController.java:331)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:379)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94)
at android.support.v7.app.AppCompatDelegateImpl$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImpl.java:2549)
at android.support.v7.app.AppCompatDelegateImpl.preparePanel(AppCompatDelegateImpl.java:1589)
at android.support.v7.app.AppCompatDelegateImpl.doInvalidatePanelMenu(AppCompatDelegateImpl.java:1869)
at android.support.v7.app.AppCompatDelegateImpl$2.run(AppCompatDelegateImpl.java:227)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
please help me what is going on
searchView.backgroundColorResource = Color.WHITE is the problem. You need to set a resource id here but Color.WHITE is a color value, not a resource id. You can try
searchView.backgroundColorResource = android.R.color.white
You're using a Color value, not a resource reference, so you can't use
searchView.backgroundColorResource = Color.WHITE
As an alternative to Henry's answer, you can use the old Java-style method with
searchView.setBackgroundColor(Color.WHITE)
Use
getApplicationContext().getColor(R.color.white);
And put your color values in xml file, such as
res/values/colors.xml
looking like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffff</color>
//other colors...
</resources>

Categories

Resources