0

First image Second image

In my application, there are five activities containing very long text. I want to add find facility to my activity, so that when a user types something to the search bar then if the activity contains that word then the word should scroll to top automatically being highlighted.I am able to call the Search Dialog (as instructed in the Android Developer page). Can you guys help me compare the input in the search dialog with the texts in my activity and scroll it to top?

MainActivity.java

 public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.search:
            onSearchRequested(); //opens the search dialog
            break;

    return super.onOptionsItemSelected(item);
}

SearchableActivity.java

public class SearchableActivity extends AppCompatActivity {

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

        //Get the intent, verify the action and get the query
        Intent intent = getIntent();
        if (intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            doMySearch(query);
        }
    }

    private void doMySearch(String query) {
        //what to do here???
    }
}

Here the searchable activity is a different activity. How do I compare the search input with a text which is in another activity (MainActivity.java)?

glennsl
  • 28,186
  • 12
  • 57
  • 75
Neo
  • 7
  • 5

1 Answers1

0

What you trying to do is:

  • Take Certain Input Value from an activity (input in the dialog from MainActivity), then use that value to compare with something else in the another activity (SearchableActivity)

There should be few method to do so, I would suggest a simple method which is:

  1. Store the Value Input to a Global Variable that can be access Global in the whole application.
  2. Then used the store value in the Global Variable to do comparison the any activity you want to.

To store something to Global variable, you can use the SharedPreference Method: Android Shared preferences example

This Method store Specify Value into a Global Variable where all activity can be access and Store Permanently only until you command to delete. Something like a 'Session' of a Website.

In your case, I will make use of the sharedpreference by:

  1. When user enter something in the search dialog, I will store the value in a SharedPreference Variable.
  2. Then when come to the SearchableActivity that need to use the input value of search dialog to do comparison, I will call the SharedPreference Variable to do comparison.

For your information:

Share Preference is very useful to store Value that used for Permanent usage of the application and its Scope is covering all the Activity in your Application. You can have Great Control of it, because you can specify to Store, Rewrite, Delete the value whenever you want, in wherever you want.

Hope This Would Help.

I am a Student
  • 1,570
  • 4
  • 21
  • 36
  • Hi Eric, thanks for your response. I don't want to store value in Shared Preference. What I want is kind of find facility as in Adobe Reader/Android Studio. – Neo Mar 23 '18 at 03:24
  • @Neo the purpose to store the input value from another activity in Shared Preference is to allow you to able to use that Shared Preference to compare the search input with a text in MainActivity. Maybe I misunderstood your requirement, could you further clarify what you trying to do? – I am a Student Mar 23 '18 at 03:29