I always get that textView is null when doing this:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
It's a EditText
not a TextView
.
Try something like that:
int id = searchView.getContext()
.getResources()
.getIdentifier("android:id/search_src_text", null, null);
EditText editText = (EditText) searchView.findViewById(id);
Hope it helps.
I'm using action bar of appcompat v7 and my solusion is:
TextView searchText = (TextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
Hope this help.
If you are using AndroidX then you can do like this in kotlin.
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
val searchItem: MenuItem = menu.findItem(R.id.action_search)
val searchView: SearchView = searchItem.actionView as SearchView
val searchText: TextView = searchView?.findViewById(androidx.appcompat.R.id.search_src_text);
searchText.setTextColor(Color.BLACK)
searchText.background = resources.getDrawable(R.drawable.rounded_corner_background)
return true
}