1

I'm new to android development.

I have an activity containing a bunch of fragments, each fragment shows a different text. I set the text at runtime from strings.xml (i.e tv.setText...)

Here is an example of my strings.xml:

<string name="string1">the content I want searched, text1</string>
<string name="string2">the content I want searched, text2</string>
<string name="string3">the content I want searched, text3</string>

This is my problem:
I want to add search functionality in the app, I want to be able to search for words inside the strings and return the whole string to the user as a result. So for example if the user searches for text2, It would return the whole string.

I already read the search guide on android-developers here: http://developer.android.com/guide/topics/search/index.html

I also found a bunch of tutorials, but all of them seem to deal with data stored in SQLite databases.

Here is more of my code:
searchable.xml:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="Search" >
</searchable>

SearchableActivity:

public class SearchableActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);

        handleIntent(getIntent());
    }


    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        handleIntent(intent);
    }



    private void handleIntent(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
          String query = intent.getStringExtra(SearchManager.QUERY);
          doMySearch(query);
        }
    }

    private void doMySearch(String query) {
    }

}

Any help would be appreciated

P.S Is this the best way to do this? I have a lot of strings (>1000) I read about using databases instead but I don't know how to convert all my data into a db, and don't know how to set text from a db...etc

2 Answers 2

2

I think you need search in strings.xml file with a key, if I understand you here is the answer .

strings.xml

<string name="string1">the content I want searched, text1</string>

method for searching inside strings.xml

private String SearchForString(String message){
// get the resource id if matched any key in strings 
// message Passed string you want search for
// "string" type of what you looking for
// package name

try {
    int resId = getResources().getIdentifier(message , "string" , getPackageName());
    String stringReturned =  getResources().getString(resId);
return stringReturned;
  } catch(Exception e){
  return null;
  }
  }

call method now

SearchForString("string1");

it should return: the content I want searched, text1

Sign up to request clarification or add additional context in comments.

3 Comments

It's a good practice to have the method name start with a lower case like searchForString()
I tried your solution but what it does is basically it gives you the value of the string corresponding to the id you send in searchForString() parameter. It doesn't do anything special.
1

Instead of using string use string-array. In resource create a file and there declare like following

<?xml version="1.0" encoding="utf-8"?><resources>
<string-array name="names">
    <item>the content I want searched, text1</item>
    <item>the content I want searched, text2</item>
    <item>the content I want searched, text3</item>
</string-array>  

now in code do the following to search the string

String[] names = getResources().getStringArray(R.array.names);
for (String s : names) {
    int i = s.indexOf(searchKeyword);
    if (i >= 0) {
        // found a match
    }
}

3 Comments

I tried it in a demo app, but I seem to have some problems with this method: 1-How can I know the number of results founds. 2-How can I get the name of the item (inside the array) for the results found. 3-I managed to put the result in a custom listadapter but is there a way to configure the onclick to open the fragment which contains the text(I know this is far fetched, but I need to find a better way of implementing search in the app). I still think there must be a better way of doing all of this, can anyone else please shed a light on this issue.
if you had very basic programming knowledge you could easily solve the points you noted .. so better grow your programming skill
@stinepike But what if we are not in position to convert our string to string-array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.