1

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"

4
  • Take a look at your import statement at the top of the class, make sure it is importing the correct R Commented Jul 2 at 9:50
  • @Lawrence Gimenez Will do...but I how do I determine if it is the correct R? Commented Jul 2 at 16:47
  • It has to be your application plus R, so like import com.coolapp.R Commented Jul 2 at 20:47
  • @Lawrence Gimenez There was no import R at the top of the class. So I added "import.packagename.R;" and Android Studio greyed out the line and message says "Unused import statement." I then did "Build / Clean Project" and "File / Invalidate Caches / Invalidate and Restart" and I get the same error message "Cannot resolve symbol 'R'" for the search_close_btn. Commented Jul 2 at 23:13

1 Answer 1

1

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"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.