I want to make a search bar for this code to search by buttons (between btn1 and btn2 and btn3) and hide the non-matched results

Viewed 62

I am trying to add a search bar that enables me to search by buttons, search between btn1 and btn2 and btn3. I tried a lot of codes but all of them were focused on TextView and not on buttons. So hope to help me and if I missed details please let me know to describe more.

The two files (.java and .xml) are typed below. thank you.

file.java

package com.example.myapp1;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;


public class MainActivity extends AppCompatActivity {

    SearchView sv;


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

        getSupportActionBar().setTitle("My First App");



        Button btn1 = findViewById(R.id.btn1);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                startActivity(intent);
            }});


        Button btn2 = findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MainActivity3.class);
                startActivity(intent);
            }});

        Button btn3 = findViewById(R.id.btn3);

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MainActivity4.class);
                startActivity(intent);
            }});



        List<Button> buttonList = new ArrayList<>();

  
        buttonList.add(view.findViewById(R.id.btn1));
        buttonList.add(view.findViewById(R.id.btn2));
        buttonList.add(view.findViewById(R.id.btn3));

        sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String s) {
                for(int i = 0; i < buttonList.size(); i++) {

                    String buttonText = (String) buttonList.get(i).getText();

                    if(buttonText.toLowerCase(Locale.ROOT).equals(s.toLowerCase())) {
                        buttonList.get(i).setVisibility(View.VISIBLE);
                    } else {
                        buttonList.get(i).setVisibility(View.INVISIBLE);
                    }
                }

                return false;
            }
            @Override
            public boolean onQueryTextChange(String s) {
                if(s.isEmpty()) {
                    for(int i = 0; i < buttonList.size(); i++) {
                        buttonList.get(i).setVisibility(View.VISIBLE);
                    }
                }
                return false;
            }
        });
    }
}



file.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="ExtraText"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:layout_marginHorizontal="10dp"
        android:orientation="vertical"
        android:weightSum="2">

        <Button
            android:id="@+id/btn1"
            android:layout_width="match_parent"
            android:layout_height="90dp"
            android:textAllCaps="false"
            android:textSize="25dp"
            android:layout_marginHorizontal="10dp"
            android:layout_weight="1"
            android:background="@drawable/gradient"
            android:textColor="@color/white"
            android:text="Absolute Pressure" />
        <Space
            android:layout_width="match_parent"
            android:layout_height="15dp"
            android:layout_weight="1" />
        <Button
            android:id="@+id/btn2"
            android:layout_width="match_parent"
            android:layout_height="90dp"
            android:textAllCaps="false"
            android:textSize="25dp"
            android:layout_marginHorizontal="10dp"
            android:layout_weight="1"
            android:background="@drawable/gradient"
            android:textColor="@color/white"
            android:text="Acoustic Flowmeter" />
        <Space
            android:layout_width="match_parent"
            android:layout_height="15dp"
            android:layout_weight="1" />
        <Button
            android:id="@+id/btn3"
            android:layout_width="match_parent"
            android:layout_height="90dp"
            android:textAllCaps="false"
            android:textSize="25dp"
            android:layout_marginHorizontal="10dp"
            android:layout_weight="1"
            android:background="@drawable/gradient"
            android:textColor="@color/white"
            android:text="Bernoulli Number" />
        <Space
            android:layout_width="match_parent"
            android:layout_height="15dp"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>
1 Answers

What do you mean by search by buttons? Are you looking to search the text that is held within the button?

Here is a way you can go about this:

  1. Make a list that contains all the instances of your buttons. List(findViewById(R.id.btn1), etc)

     List<Button> buttonList = new ArrayList<>();
    
     buttonList.add(view.findViewById(R.id.button_search1));
     buttonList.add(view.findViewById(R.id.button_search2));
     buttonList.add(view.findViewById(R.id.button_search3));
    
  2. We are going to use the listeners available to the search view to do the work.

     sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    
         // You can use this for when the submit is clicked
         @Override
         public boolean onQueryTextSubmit(String s) {
             // We loop over the button list
             for(int i = 0; i < buttonList.size(); i++) {
    
                 // We get the text of the button
                 String buttonText = (String) buttonList.get(i).getText();
    
                 // We turn both the button text and search result to lowercase
                 // So that results are not case sensitive
    
                 // If they match
                 if(buttonText.toLowerCase(Locale.ROOT).equals(s.toLowerCase())) {
                     // We make button visible
                     buttonList.get(i).setVisibility(View.VISIBLE);
                 } else {
                     // If it doesn't, we make invisible
                     buttonList.get(i).setVisibility(View.INVISIBLE);
                 }
             }
    
             return false;
         }
    
         // You can use this when the text is changing.
         @Override
         public boolean onQueryTextChange(String s) {
             // If text is empty, reset results
             if(s.isEmpty()) {
                 for(int i = 0; i < buttonList.size(); i++) {
                     buttonList.get(i).setVisibility(View.VISIBLE);
                 }
             }
    
             return false;
         }
     });
    
Related