Search code examples
androidlistviewsearchviewhindi

How to use SearchView for a Hindi ListView


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...


Solution

  • 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.