SearchLayout Android Library

repository·master·Indexed 21 days ago

https://github.com/carson-ho/search_layout

An Android custom search bar component that encapsulates search history functionality and common search UI styles. It provides the scut.carson_ho.searchview.SearchView component with customizable XML attributes for text size, color, and block height, as well as callback listeners for search and back button actions.

Tokens
2K
Snippets
6
Records
6
Agent score
26%

What's inside SearchLayout

  1. Install SearchLayout via Gradle or Maven

    master

    You can integrate the SearchLayout library into your Android project using either Gradle or Maven.

    Gradle

    Add the following dependency to your build.gradle file:

    Maven

    Add the following dependency to your pom.xml file:

    // build.gradle
    dependencies {
        compile 'com.carson_ho:SearchLayout:1.0.1'
    }
    <!-- pom.xml -->
    <dependency>
      <groupId>com.carson_ho</groupId>
      <artifactId>SearchLayout</artifactId>
      <version>1.0.1</version>
      <type>pom</type>
    </dependency>
  2. Import SearchLayout library

    master

    You can import the SearchLayout library into your Android project using either Gradle or Maven.

    ### For Gradle
    ```gradle
    dependencies {
        compile 'com.carson_ho:SearchLayout:1.0.1'
    }

    For Maven

    <dependency>
      <groupId>com.carson_ho</groupId>
      <artifactId>SearchLayout</artifactId>
      <version>1.0.1</version>
      <type>pom</type>
    </dependency>
  3. Configure SearchView attributes in XML

    master

    Use the scut.carson_ho.searchview.SearchView component in your layout XML files. You can customize the appearance using the following attributes:

    • app:textSizeSearch: Text size for the search input.
    • app:textColorSearch: Color of the search input text.
    • app:textHintSearch: Hint text displayed in the search bar.
    • app:searchBlockHeight: Height of the search block.
    • app:searchBlockColor: Background color of the search block.
    <scut.carson_ho.searchview.SearchView
            android:id:@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:textSizeSearch="5dp"
            app:textColorSearch="#3F51B5"
            app:textHintSearch="输入查询关键字"
            app:searchBlockHeight="150"
            app:searchBlockColor="#ffffff"
            />
  4. Handle search and back button clicks in SearchView

    master

    To make the SearchView interactive, you must set up callback listeners in your Activity or Fragment to handle user actions like pressing the search key on the keyboard or the back button.

    • Use setOnClickSearch(ICallBack callback) to handle the search action. The callback provides the current search string.
    • Use setOnClickBack(bCallBack callback) to handle the back button action.
    // 1. Initialize the SearchView variable
    private SearchView searchView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 2. Set the content view
        setContentView(R.layout.activity_search);
    
        // 3. Bind the component
        searchView = (SearchView) findViewById(R.id.search_view);
    
        // 4. Set action for the keyboard search button
        // The parameter passed to SearchAciton is the input string
        searchView.setOnClickSearch(new ICallBack() {
            @Override
            public void SearchAciton(String string) {
                System.out.println("Received: " + string);
            }
        });
        
        // 5. Set action for the back button
        searchView.setOnClickBack(new bCallBack() {
            @Override
            public void BackAciton() {
                finish();
            }
        });
    }
  5. Handle search and back button actions

    master

    To make the SearchView functional, you must set up callbacks for when the user clicks the search button or the back button in your Activity or Fragment.

    • setOnClickSearch(ICallBack callback): Triggered when the search button is clicked. The SearchAciton(String string) method receives the current text entered in the search bar.
    • setOnClickBack(bCallBack callback): Triggered when the back button is clicked. The BackAciton() method is called.
    // 1. Initialize variable
    private SearchView searchView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 2. Set content view
        setContentView(R.layout.activity_search);
    
        // 3. Bind component
        searchView = (SearchView) findViewById(R.id.search_view);
    
        // 4. Set search button action
        searchView.setOnClickSearch(new ICallBack() {
            @Override
            public void SearchAciton(String string) {
                // 'string' is the input content
                System.out.println("Received: " + string);
            }
        });
        
        // 5. Set back button action
        searchView.setOnClickBack(new bCallBack() {
            @Override
            public void BackAciton() {
                finish();
            }
        });
    }
  6. Configure SearchView XML attributes

    master

    The scut.carson_ho.searchview.SearchView component can be customized directly in your XML layout files using several attributes to control its appearance and behavior.

    Common attributes include:

    • app:textSizeSearch: Sets the text size for the search input.
    • app:textColorSearch: Sets the color of the search input text.
    • app:textHintSearch: Sets the placeholder hint text.
    • app:searchBlockHeight: Sets the height of the search block area.
    • app:searchBlockColor: Sets the background color of the search block area.
    <scut.carson_ho.searchview.SearchView
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:textSizeSearch="5dp"
            app:textColorSearch="#3F51B5"
            app:textHintSearch="输入查询关键字"
            app:searchBlockHeight="150"
            app:searchBlockColor="#ffffff"
            />