Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android search bar with mic & mic events. Android searchview with mic

I am using a searchview in my application. I want to get the text typed in the searchview textbox and display it on another textview.

I can do that using searchviewListener.

searchView.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            //adapter.filterData(query);
            //display text where ever i want

            return false;
        }

        @Override
        public boolean onQueryTextChange(String query) {
            adapter.filterData(query);
            //display text where ever i want
            return false;
        }
    });

    return true;
}

Now i made the serachView with Mic using

android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"

Mic is displaying and clickable. Perfect..

Now My question is how to capture this MIC data and display text where ever i want.

like image 923
Devesh Agrawal Avatar asked Feb 08 '23 01:02

Devesh Agrawal


1 Answers

To display data which is captured from the MIC here i tried to give answer for same with the help of android developer link.

As you mentioned in question, *Mic is displaying and clickable. Perfect..how to capture this MIC data and display text where ever i want. *

You need to give intent filter for the Seachable & xml resource for the same.

To get the data which is capture by mic (which will be open as a Dialog) than you need to handle it in onNewIntent of the activity after checking intent action Intent.ACTION_SEARCH

search_voice.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v7.widget.Toolbar 
        android:id="@+id/ab_tool"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00FFFFFF"
        android:minHeight="?attr/actionBarSize">

    </android.support.v7.widget.Toolbar>
    <TextView
        android:id="@+id/txtVoiceSeachQuery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FF0000"
        android:textSize="26sp" />
</LinearLayout>

SearchVoiceTest

public class SearchVoiceTest extends AppCompatActivity {
    private TextView txtVoiceSeachQuery;
    private SearchView searchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_voice);
        Toolbar toolbar = (Toolbar) findViewById(R.id.ab_tool);
        setSupportActionBar(toolbar);
        toolbar.setTitle("Voice Text Display");
        txtVoiceSeachQuery = (TextView) findViewById(R.id.txtVoiceSeachQuery);
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            searchView.setQuery(String.valueOf(query), false);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_home_screen, menu);
        searchView =
                (SearchView) menu.findItem(R.id.search_view).getActionView();
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //adapter.filterData(query);
                //display text where ever i want
                return false;
            }

            @Override
            public boolean onQueryTextChange(String query) {
                //display text where ever i want
                if (txtVoiceSeachQuery != null) {
                    txtVoiceSeachQuery.setText(query);
                }
                return false;
            }
        });

        return true;
    }
}

AndroidManifest Declaratin of activity

 <activity
            android:name="com.example.search.voice.SearchVoiceTest"
            android:configChanges="orientation|screenSize"
            android:launchMode="singleTop" android:label="Search Voice Test"
            android:theme="@style/AppThemeSliderToolbar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.SEARCH" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/search_with_voice" />


        </activity>

res/xml/search_with_voice.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:hint="Give Your Query Here"
    android:label="@string/app_name"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer">
</searchable>

res/menu/menu_home_screen.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    >
    <item
        android:id="@+id/search_view"
        android:title="Search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="ifRoom|collapseActionView"></item>
</menu>

After assigning query to seachview we can get callback setOnQueryTextListener in onQueryTextChange so from there for test purpose value assigned to TextView there you can make your changes.

Here i have given link of output of my answer.

  • Search with voice
  • Display Captured data from mic

let me know if anything

like image 130
user1140237 Avatar answered Feb 10 '23 23:02

user1140237