LoginSignup
5

More than 5 years have passed since last update.

Themeを使わずにAppCompt SearchViewのカスタマイゼーション

Last updated at Posted at 2016-06-15

いろんな原因でthemeの使用を諦めてコードでAppcomptのSearxchViewをカスタマイズしたいです。一番詰んだのはSearchIconの削除です。ソースコードは下記の通りです。ご参考になれれば幸いです。

要注意のは
searchView.setIconifiedByDefault(false);
先に設定しないとSearchIconにどんなに弄っても消えないです

コード

 SearchView searchView = (SearchView)findViewById(.....);
// hint text color
((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(.....);
// query text color
((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setTextColor(.....);
// remove search view underbar
searchView.findViewById(android.support.v7.appcompat.R.id.search_plate).setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent));
// set close button image
((ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn)).setImageResource(.....);
// remove search icon
searchView.setIconifiedByDefault(false);
ImageView image = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
image.setAdjustViewBounds(true);
image.setMaxWidth(0);
image.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
image.setImageDrawable(null);
// set query hint sting
searchView.setQueryHint(.....);

間違ったところがあったらご指摘いただけると幸いです。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5