Search code examples
javaandroidsearch-boxnavigation-drawer

Combining android navigation drawer with the search box of Android Persistent Search Library


I am developing an app in which I am using search bar from Android Persistent Search Library on github. In my app I have a default navigation drawer activity provided by android studio. The search bar looks like this: persistent search box

I want to open my navigation drawer on clicking the hamburger icon in the search box. Is there any way of combining these two? Currently when I click the hamburger icon a method onMenuClick() is called and a toast is displayed saying "Menu Click". How can I change this onClick to open up my navigation drawer? Any help is appreciated.

Here is the code of my activity:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SearchBox search = (SearchBox) findViewById(R.id.searchbox);
        search.enableVoiceRecognition(this);

        search.setLogoText("Enter your search term here...");
        search.setMenuListener(new SearchBox.MenuListener(){

            @Override
            public void onMenuClick() {
                //Hamburger has been clicked
                Toast.makeText(MainActivity.this, "Menu click", Toast.LENGTH_SHORT).show();
            }

        });
}

Solution

  • well you didn't show any code so that i can't tell you want to change, but this is how you can open a drawer which is at the left hand side

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id."provide your draer id here");
      drawer.openDrawer(GravityCompat.START);
    

    you have to write this code inside onMenuClick() that you may open the drawer

    and if the navigation drawer have android:layout_gravity="right", use this

    drawer.openDrawer(GravityCompat.END);