0

I have a SearchView in my ListView which is in Hindi Language.. When I use the search typing hindi it filters the list fine..

I want to get filtered list when the user searches in English

For Example if the user searches "Mangal" it should filter the list containing मंगल

the following is my MainActivity Class

public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {

// Declare Variables
ListView list;
ListViewAdapter adapter;
SearchView editsearch;
String[] NameList;
ArrayList<Names> arraylist = new ArrayList<Names>();

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

    // Generate sample data

    NameList = new String[]{"मंगल स्तुति", "भिक्षु स्तुति", "कालू स्तुति",
            "तुलसी स्तुति", "महाप्रज्ञ अभिवंदना", "शासन स्तुति", "तपस्या गीत"};

    // Locate the ListView in listview_main.xml
    list = (ListView) findViewById(R.id.listview);

    for (int i = 0; i < NameList.length; i++) {
        Names Names = new Names(NameList[i]);
        // Binds all strings into an array
        arraylist.add(Names);
    }

    // Pass results to ListViewAdapter Class
    adapter = new ListViewAdapter(this, arraylist);

    // Binds the Adapter to the ListView
    list.setAdapter(adapter);

    // Locate the EditText in listview_main.xml
    editsearch = (SearchView) findViewById(R.id.search);
    editsearch.setOnQueryTextListener(this);
}


@Override
public boolean onQueryTextSubmit(String query) {

    return false;
}

@Override
public boolean onQueryTextChange(String newText) {
    String text = newText;
    adapter.filter(text);
    return false;
}
}

i thought of using getQuery to get the searched text and then compare that which the english versions but that would be very cubersome as there are lot of hindi texts on my ListView..

Also if there is a possibility to set search tags for the items in the List.. i am not sure...

Please help...

amit
  • 709
  • 6
  • 17

1 Answers1

1

I would strongly advise you to read this docs https://developer.android.com/studio/write/translations-editor

Just to give you an overview, you would need to create a String resource for each and every String in your app. So add them to your Strings.xml. Something like Mangal

Next step is to open Translations Editor from strings.xml by clicking Open Editor. Click on the global button and select Hindi from there. Add the translation in the translation field. Do the same for all the fields. Now do all your searches in English. Change your device language to Hindi. Run the app and you should see translated Strings for your app.

Hope this helps.

MXC
  • 458
  • 1
  • 5
  • 21
  • thanks for your suggestion.. but i have around 500 pages of hindi texts categorized as lists.. to put a String resource to all the hindi words in my project would be too cumbersome and not worth the time.. going through the link provided by you.. it seems that this is the only way out.. what if i change the keyboard layout to hindi when the user clicks on the search bar..? Also you mentioned to change the device language to Hindi.. i might do before running the app.. but the users of the app might not be willing to do the same... – amit Aug 03 '18 at 07:18
  • You would have to define those Strings somewhere and they will be all over the code base, so why not to put them in one place and get more benefits like localization, easy maintenance. I would definitely, put them in my `String.xml` file. You can set the local programmatically as well, check this SO answer https://stackoverflow.com/questions/2900023/change-app-language-programmatically-in-android?answertab=oldest#tab-top I have not tried the solution personally but looks like it will do the job. GIve it a try, I would suggest. – MXC Aug 03 '18 at 11:45
  • .. i tried to add the translations to the Strings XML File.. but could not get the search to work to sort out my list .. as the text only translated the static text which is visible in the screen.. it doesnt sort the list to show all **मंगल** values in the list when i search **"mangal"** – amit Aug 07 '18 at 08:12