Styling Search View on Android (min21)

白昼怎懂夜的黑 提交于 2019-11-29 12:58:07
Cesarsk

OK, Seems We figured it out a solution to our problem. (thanks to my teammate Claffolo)

I honestly couldn't figure it out how to solve my issue so we preferred to create our personal Search Bar, instead of using Google's one.

This is the result we achieved: Search Bar (I replaced our logo with that home icon because we prefer to hide our logo.), pretty similar to the one I wanted, uh?

This is my activity_main.xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="?attr/colorPrimary"
        android:layout_alignParentStart="true"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="2dp"
            android:paddingEnd="16dp">

            <ImageView
                android:id="@+id/search_bar_hint_icon"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:src="@drawable/ic_search"/>

            <EditText
                android:id="@+id/search_bar_edit_text"
                android:layout_width="0dp"
                android:hint="@string/search_hint"
                android:textColorHint="@color/LightYellow"
                android:backgroundTint="@color/DarkYellow"
                android:background="@color/DarkYellow"
                android:inputType="textCapWords"
                android:layout_weight="1"
                android:layout_height="32dp" />

            <ImageButton
                android:id="@+id/search_bar_voice_icon"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:background="@drawable/ic_mic" />

        </LinearLayout>

    </android.support.v7.widget.Toolbar>

    <ImageView
        android:id="@+id/imageview_logo"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_below="@id/my_toolbar"
        android:src="@drawable/ic_home"
        android:background="@color/MainYellow"
        />
</RelativeLayout>

So as you can see it's a struct made of three elements: a EditText, which handles the input, an ImageView that is represented by that lens icon before the Search Hint TextBox and a ImageButton, the mic_icon button that handles voice_speech events. All of them are places in a linearLayout, and in a Toolbar, I had to place layout_weight = 1 field in the EditText to achieve the result you can see in the picture.

Anyway, here's how we handled our code:

MainActivity.java:

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //SETUP TOOLBAR
        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);
}
    voice_search_button = (ImageButton)findViewById(R.id.search_bar_voice_icon);
    editText = (EditText) findViewById(R.id.search_bar_edit_text);
    editText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fragmentManager.beginTransaction().replace(R.id.fragment_container, new SearchResultsFragment()).commit();
        }
    });

    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){
                fragmentManager.beginTransaction().replace(R.id.fragment_container, new SearchResultsFragment()).commit();
            }
        }
    });

To handle voice_speech events, add the following in MainActivity.java, under onCreate overrided method:

  //Voice Search Listener
        voice_search_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                promptSpeechInput();
            }
        });

And override these two methods:

/**
 * Showing google speech input dialog
 * */
private void promptSpeechInput() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
            getString(R.string.speech_prompt));
    try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(getApplicationContext(),
                getString(R.string.speech_not_supported),
                Toast.LENGTH_SHORT).show();
    }
}

/**
 * Receiving speech input
 * */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case REQ_CODE_SPEECH_INPUT: {
            if (resultCode == RESULT_OK && null != data) {

                ArrayList<String> result = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                editText.setText(result.get(0));
                //TODO AVVIARE ATTIVITA' CON TESTO RESULT.GET(0)
            }
            break;
        }

    }
}

In the end, we're still dealing with the instantSearch and filtering features thanks to this guy who answered here: How to filter a RecyclerView with a SearchView

Hope this helped someone, I will update my answer, eventually.

Have a nice day.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!