Search code examples
javaandroidandroid-searchandroid-api-levels

How to make Search Activity to hit API every character input - AndroidX


I want to make similiar like this Activity that can hit API everytime I type a character ExampleSearch.java

SearchActivity.java

public class SearchActivity extends AppCompatActivity {

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

    Intent intent = getIntent();
    handleIntent(intent);
}



// Handles the intent that carries user's choice in the Search Interface
private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);

        Log.i("Main", "Received query: " + query);
    } else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
        Bundle bundle = this.getIntent().getExtras();

        assert (bundle != null);

        if (bundle != null) {

        }
    }
}

// Menu callbacks ______________________________________________________________________________
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_searchactivity, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    if (id == R.id.action_search) {
        // Calls Custom Searchable Activity
        Intent intent = new Intent(this, SearchActivity.class);
        startActivity(intent);

        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

activity_search.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"

    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:layout_gravity="center_vertical"

    android:focusable="true"
    android:clickable="true"
    android:background="?android:attr/selectableItemBackground">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/RecyclerView_SearchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout>

menu_searchactivity.xml > toolbarmenu

<menu
    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"
    tools:context="edsilfer.com.br.edsilfer.Main">

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="asdasdasdasd"

        tools:ignore="AppCompatResource" />

    <item
        android:id="@+id/action_search"
        android:orderInCategory="200"
        android:title="Search"
        android:icon="@drawable/search"
        app:showAsAction="ifRoom"/>
</menu>

Iam not sure how can I make toolbar design like that Your experience will help me a lot ...............................................................................................


Solution

  • youreditext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
            }
    
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    // your logic
            }
    
            @Override
            public void afterTextChanged(Editable editable) {
    
    
    
            }
        });