0

In my application i need to search the phone contact numbers and I want to select a number and I want to save that number and after that once I click, send button message should go to that number. Here am using more than two activities and am searching a contacts inside of one such activity..

please any one can help me..thanks in advance..

Here is my code.

public class SearchNum extends Activity {
private TextView resultText;

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

    resultText = (TextView)findViewById(R.id.searchViewResult);

    setupSearchView();
}

private void setupSearchView() {
    SearchManager searchManager = (SearchManager)     getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView = (SearchView) findViewById(R.id.searchView);
    SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
    searchView.setSearchableInfo(searchableInfo);
}

@Override
protected void onNewIntent(Intent intent) {
    if  (ContactsContract.Intents.SEARCH_SUGGESTION_CLICKED.equals(intent.getAction())) {
        //handles suggestion clicked query
        String displayName = getDisplayNameForContact(intent);
        resultText.setText(displayName);
    } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        // handles a search query
        String query = intent.getStringExtra(SearchManager.QUERY);
        resultText.setText("should search for query: '" + query + "'...");
    }
}

private String getDisplayNameForContact(Intent intent) {
    Cursor phoneCursor = getContentResolver().query(intent.getData(), null,  null, null, null);
    phoneCursor.moveToFirst();
    int idDisplayName = phoneCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
    String name = phoneCursor.getString(idDisplayName);
    phoneCursor.close();
    return name;
}
}

and my xml file for search view is

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SearchNum" >

<SearchView
    android:id="@+id/searchView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" >
</SearchView>

<TextView
    android:id="@+id/searchViewResult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/searchView"
    android:layout_centerHorizontal="true"
    android:text="@string/hello_world" />

</RelativeLayout>

Then my searchable file in xml folder is

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/findContact"
android:hint="@string/findContact"
android:includeInGlobalSearch="true"
android:queryAfterZeroResults="true"
android:searchMode="queryRewriteFromText"
android:searchSettingsDescription="@string/findContact"
android:searchSuggestAuthority="com.android.contacts"
android:searchSuggestIntentAction="android.provider.Contacts.SEARCH_SUGGESTION_CLICKED"
android:searchSuggestIntentData="content://com.android.contacts/contacts/lookup" >

<!-- allow green action key for search-bar and per-suggestion clicks -->
<actionkey
    android:keycode="KEYCODE_CALL"
    android:queryActionMsg="call"
    android:suggestActionMsg="call" />

</searchable>

and also i have taken permission in my manifest file

<uses-permission android:name="android.permission.READ_CONTACTS" />

in activity i put

 <action android:name="android.intent.action.SEARCH" />

and also here is my metadata

  <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
user3440681
  • 11
  • 1
  • 3
  • why so complex? see my answer here http://stackoverflow.com/questions/22443791/multiautocompletetextview-with-contacts-phone-numbers/22465425#22465425 – pskink Apr 18 '14 at 05:32

1 Answers1

0

You also need to add to your manifest file:

android:launchMode="singleTop"

Link: http://looksok.wordpress.com/2013/06/15/android-searchview-tutorial-edittext-with-phone-contacts-search-and-autosuggestion/

T D Nguyen
  • 7,054
  • 4
  • 51
  • 71