I am trying to create a SearchView and programatically change textcolor and add filters e.g. max length.
SearchView search = findViewById(R.id.searchView1);
TextView textView = search.findViewById(R.id.search_src_text);
textView.setTextColor(Color.rgb(0, 0, 0));
textView.setFilters(new InputFilter[]{new InputFilter.LengthFilter(15)});
This way it says Cannot resolve symbol 'search_src_text'
Then I tried:
SearchView search = findViewById(R.id.searchView1);
int id = search.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView textView = search.findViewById(id);
textView.setTextColor(Color.rgb(0, 0, 0));
textView.setFilters(new InputFilter[]{new InputFilter.LengthFilter(15)});
In this case there is No error, but only warning, that Use of getIdentifier is discouraged
My Searchview in xml:
<SearchView
android:id="@+id/searchView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/searchfield"
/>
Is there any other way to do the same thing with SearchView without any warnings and errors?
I would suggest to replace the SearchView with androidx.appcompat.widget.SearchView
and use
val searchView: SearchView = findViewById(R.id.searchView1)
val textView: TextView = searchView.findViewById(androidx.appcompat.R.id.search_src_text)
Here is some information on Why to use androidX
Sample gradle update
implementation ("androidx.coordinatorlayout:coordinatorlayout:1.2.0")
implementation ("com.google.android.material:material:1.11.0")
implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
implementation("androidx.activity:activity-compose:1.8.2")
implementation(platform("androidx.compose:compose-bom:2023.08.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
val navVersion = "2.7.6"
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.11.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
// navigation
implementation("androidx.navigation:navigation-fragment-ktx:$navVersion")
implementation("androidx.navigation:navigation-ui-ktx:$navVersion")