In my Android project, I have an onCreateOptionsMenu(Menu menu) to show a SearchView. The below line sets up an "X" graphic as a close button for the SearchView, which is shown when the SearchView is open:
ImageView clearButton = mSearchView.findViewById(androidx.appcompat.R.id.search_close_btn);
Android Studio throws an error message on this line, "Cannot resolve symbol 'R'".
What am I missing here?
I have read other answers on StackOverflow to "Build / Clean Project" or "File / Invalidate Caches / Invalidate and Restart" which works sometimes to clear the error but I am looking for a solution to avoid the error in the first place.
MainActivity's SearchView code:
getMenuInflater().inflate(R.menu.mainactiv_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
menu.findItem(R.id.action_search).setVisible(false);
SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
mSearchView = (SearchView) searchItem.getActionView();
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
mSearchView.setIconifiedByDefault(true);
mainactiv_menu.xml
<item android:id="@+id/action_search"
android:title="@string/search_title"
android:icon="@drawable/ic_action_search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
android:orderInCategory="1"
app:showAsAction="always|collapseActionView" />
AndroidManifest.xml
<activity
android:name=".MainActivity" >
<intent-filter tools:ignore="ExtraText">
<action android:name="android.intent.action.SEARCH" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name" >
</searchable>
IDE is Android Studio Narwhal | 2025.1.1
build.gradle(:app) has "implementation libs.appcompat"
libs.version.toml has 'appcompat = "1.7.1"'
gradle.properties has "android.useAndroidX=true"
You do not import R, that is done by the system. This error is usually caused by a spelling mistake in the manifest file. As a start in the android name tag use the full package instead of just .MainActivity.
eg andriod:name = "com.app.AppName.MainActivity"