I've just followed the advices provided by this video and this documentation as I've been trying to add a SearchView to my app lately.

I took the following steps so far:

  • added the search menu item:

        app:actionViewClass="androidx.appcompat.widget.SearchView" />
  • added the following pieces to the onCreateOptionsMenu() in my MainActivity.java (but still don't know how to perform the final search):

    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView)
            new SearchView.OnQueryTextListener() {
                public boolean onQueryTextSubmit(String s) {
                    return true;
                public boolean onQueryTextChange(String s) {
                    return true;
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    ComponentName componentName = new ComponentName(getApplicationContext(), SearchableActivity.class);
  • created a basic searchable.xml configuration:

    <?xml version="1.0" encoding="utf-8"?>
    <searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:hint="Buscar" >
  • added a searchable acvitity to the manifest file:

    <activity android:name=".ui.activity.SearchableActivity"
        android:launchMode="singleTop" >
            <action android:name="android.intent.action.SEARCH" />
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable" />
  • and created a sketch of the SearchableActivity.java with a sketch of a handler for the query:

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

From this point forward, I don't know how to proceed to exhibit the search results considering my Adapter which fills a ListView getting data from a JSON file kept in a server.

Apart from the sketch of the SearchableActivity.java malfunction, everything is okay, but showing no result as expected since there are still many blanks.

I'd rather just using the MainActivity.java and the current adapter to show the results, but now I'd just want to understand what I must do to make this work at least.

Just in case, I'm putting here the adapter so as it helps you to understand the issue:

public class MyListAdapter extends ArrayAdapter<Subreddit> {

    List<Subreddit> subreddits;
    Context context;
    private LayoutInflater mInflater;
    private SubredditList subredditList;

    // Constructors
    public MyListAdapter(Context context, SubredditList subredditList, String sortingColumn, boolean reverse) {
        super(context, 0, subredditList.getSubreddits(sortingColumn, reverse));
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.subredditList = subredditList;
        subreddits = subredditList.getSubreddits(sortingColumn, reverse);

    public static String getDate(String stringData) throws ParseException {
        SimpleDateFormat inputDate = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy", Locale.getDefault());
        Date date = inputDate.parse(stringData);
        SimpleDateFormat outputDate = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
        return outputDate.format(date);

    public Subreddit getItem(int position) {
        return subreddits.get(position);

    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            View view = mInflater.inflate(R.layout.layout_row_view, parent, false);
            viewHolder = ViewHolder.create((LinearLayout) view);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();

        Subreddit item = getItem(position);

        if (subreddits.get(position).getIcon().equals("")) { //url.isEmpty()
        } else {
        DecimalFormat formatter = new DecimalFormat("###,###,###");
        String decimalFormat = formatter.format(item.getMembers()).replace(",", ".");
        try {
            viewHolder.textViewAge.setText(getDate(new Date(item.getAge() * 1000).toString()));
        } catch (ParseException e) {
        if (subreddits.get(position).getNSFW()) {
        } else {

        if (position % 2 == 1) {
        } else {

        return viewHolder.rootView;


    private static class ViewHolder {
        public final LinearLayout rootView;
        public final FrameLayout frameIcon;
        public final ImageView imageViewIcon;

        private ViewHolder(LinearLayout rootView,
                           FrameLayout frameIcon,
                           ImageView imageViewIcon,
        ) {
            this.rootView = rootView;
            this.frameIcon = frameIcon;
            this.imageViewIcon = imageViewIcon;

        public static ViewHolder create(LinearLayout rootView) {
            FrameLayout frameIcon = rootView.findViewById(R.id.frameIcon);
            ImageView imageViewIcon = rootView.findViewById(R.id.imageViewIcon);

            return new ViewHolder(rootView,



I thank you advance and I'm available to show more details about the code if needed.

Related posts

Recent Viewed