Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 1990
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 31, 20222022-05-31T10:24:45+00:00 2022-05-31T10:24:45+00:00

android – How to implement a SearchView to search specific buttons in a LinearLayout?

  • 0

[ad_1]

I’m opening this discussion because I need help for a school project.

At the moment, I have a scrollable activity with some buttons, everything is built like this:
a constraint layout, with SearchView, ScrollView and a BottomNavigationBar, and inside the ScrollView there’s a LinearLayout with all my buttons.

Here is the full xml file:

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <SearchView
        android:id="@+id/BarraRicerca"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:gravity="top"
        android:iconifiedByDefault="false"
        android:imeOptions="actionDone"
        android:queryHint="Cerca un tutorial"
        android:labelFor="@id/BarraRicerca"
        android:suggestionRowLayout="@color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_weight="1"
        android:fillViewport="false"
        android:orientation="vertical"
        android:padding="10dp"
        android:layout_marginTop="60dp"
        app:layout_constraintBottom_toTopOf="@id/BottomBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/BarraRicerca">


        <LinearLayout
            android:id="@+id/Linearlayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:context="com.example.Flipper.FlipperList">

            <Button
                android:id="@+id/FlintstonesF"
                android:layout_width="300dp"
                android:layout_height="49dp"
                android:layout_gravity="center_horizontal"
                android:layout_margin="100dp"
                android:text="Flintstones" />

            <Button
                android:id="@+id/tutorial2"
                android:layout_width="300dp"
                android:layout_height="49dp"
                android:layout_gravity="center_horizontal"
                android:layout_margin="200dp"
                android:layout_weight="1"
                android:text="ciao" />

            <Button
                android:id="@+id/tutorial3"
                android:layout_width="300dp"
                android:layout_height="49dp"
                android:layout_gravity="center_horizontal"
                android:layout_margin="100dp"
                android:layout_weight="1"
                android:text="hello" />

        </LinearLayout>

    </ScrollView>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/BottomBar"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>

and the result is this, everything works

enter image description here

Now I need to implement the SearchView to actually search through my buttons, For example if I search Flintstones (or Flint, or Fli etc.) I want the Flintstones button to be the only one shown. The same goes for “ciao” and so on, I will obviously have more buttons than these.

I tried every possible solution I found on the internet but nothing seems to work. This is my code

package com.example.Flipper

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import com.example.pinballacademy.R


class FlipperList : AppCompatActivity() {
    val passaFlintstones: Button get() = findViewById(R.id.FlintstonesF)
    val bottone2: Button get() = findViewById(R.id.tutorial2)
    val bottone3: Button get() = findViewById(R.id.tutorial3)
    val searchbar: SearchView get() = findViewById(R.id.BarraRicerca)
    val linLay: LinearLayout get() = findViewById(R.id.Linearlayout)

    var listaFlipper: ArrayList<String> = ArrayList()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_flipper_list)






        /*    listaFlipper.clear();
            listaFlipper.add(passaFlintstones.text.toString())
            listaFlipper.add(bottone2.text.toString())
            listaFlipper.add(bottone3.text.toString())*/

            searchbar.isSubmitButtonEnabled=true




           searchbar.setOnQueryTextListener() {

                 fun onQueryTextChange(newText: String?): Boolean {
                    filter(newText)
                    return true
                }

                 fun onQueryTextSubmit(query: String?): Boolean {
                    filter(query)
                    return true
                }

                    // Do your task here


                }


        passaFlintstones.setOnClickListener()
        {
            val intent = Intent(this, FlintstonesScheda::class.java)
            //avvia registrazione
            startActivity(intent)
            // finish()
        }

    }

    fun filter(searchText: String?) {
        for (i in 0 until linLay.childCount) {
            val button: Button = linLay.getChildAt(i) as Button
            if (button.text.toString().contains(searchText!!)) {
                button.isVisible=true
            } else button.isVisible=false
        }
    }


    private fun SearchView.setOnQueryTextListener(function: () -> Unit) {

    }
}

As you can see I’m not even near to a solution, I hope you can help! Thank you!

[ad_2]

  • 0 0 Answers
  • 11 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 4796 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1063 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1009 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.