[FIXED] 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

Issue

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.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    SearchView sv;

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

        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);
            }
        });

        
    }
}


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>



Solution

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)

  2. Add a new button onto your screen that will be the search button.

  3. Add onClick() listener to your search button that takes the text from the search view and loops over your button list. IF button.text != searchView.text then change visibility of that button. button.visibility = View.Invisible.

Answered By – CodeGorilla

Answer Checked By – Robin (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published