Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

android - Remove the SearchIcon as hint in the searchView

I am doing an application where for example when i click on the image(it is a searchView)

enter image description here

the search pad opens !

and it looks like enter image description here

but here the default search icon (magnifier) gets displayed but this dissappears as soon as some text is entered

enter image description here

but i dont want that magnifier to be displayed even the image is clicked for the first time

and here i am not using any xml file

my code is

public class MainActivity extends Activity {

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        RelativeLayout relative = new RelativeLayout(this);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
        relative.setLayoutParams(params);
        setContentView(relative);

        SearchView searchView = new SearchView(this);
        traverseView(searchView, 0);
//      searchView.setIconifiedByDefault(false);
        LayoutParams searchViewparams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//      searchViewparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        searchView.setLayoutParams(searchViewparams);
        relative.addView(searchView);
    }
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    private void traverseView(View view, int index) {
        if (view instanceof SearchView) {
            SearchView v = (SearchView) view;
            for(int i = 0; i < v.getChildCount(); i++) {
                traverseView(v.getChildAt(i), i);
            }
        } else if (view instanceof LinearLayout) {
            LinearLayout ll = (LinearLayout) view;
            for(int i = 0; i < ll.getChildCount(); i++) {
                traverseView(ll.getChildAt(i), i);
            }
        } else if (view instanceof EditText) {
            ((EditText) view).setTextColor(Color.GREEN);
            ((EditText) view).setHintTextColor(Color.BLACK);
        } else if (view instanceof TextView) {
            ((TextView) view).setTextColor(Color.BLUE);
        } else if (view instanceof ImageView) {
            ((ImageView) view).setImageResource(R.drawable.ic_launcher);
        } else {
            Log.v("View Scout", "Undefined view type here...");
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In my app i've used android.support.v7.widget.SearchView and to hide search icon this worked for me :

<android.support.v7.widget.SearchView
    ...
    app:iconifiedByDefault="false"
    app:searchIcon="@null"
/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...