SearchView expand/collapse animation in Android

What I Have

I have a SearchView which does its work perfectly. But when I touch on it, it appears and disappears out of nothing. There is no transition animation playing on it and so it doesn’t look good.

  • “Activity has been destroyed” sometimes when populating viewpager with fragments
  • Not able to view video in Android Web View
  • Android Place Picker closes immediately after launch
  • What is MergeRootFrame in FrameLayout?
  • Displaying SVG files in Android
  • how can I notify a running activity from a broadcast receiver?
  • What I Want

    I want a simple slide left and slide right animation to be played on the SearchView when it is expanded and collapsed respectively.

    What I Tried

    SearchView searchView = (SearchView) menu.findItem(;
    //Get the ID for the search bar LinearLayout
    int searchBarId = searchView.getContext().getResources().getIdentifier("android:id/search_bar", null, null);
    //Get the search bar Linearlayout
    LinearLayout searchBar = (LinearLayout) searchView.findViewById(searchBarId);
    //Give the Linearlayout a transition animation.
    searchBar.setLayoutTransition(new LayoutTransition());

    but the searchBar is always null so I can’t set the layout transition on it.

    Can I get a solution for it? Is my approach correct?

    Related posts:

    Fast ways to import drawables in Android Studio?
    Hiding encryption key in Android Application
    Android SMS receiver not working
    Is it possible to use proguard in debug mode?
    Using IntelliJ with Android plugin, rather than using Android Studio
    How to set image in imageview in android?
  • Android share intent chooser
  • How to Find memory leaks from native code in android
  • pass arraylist from one activity to other
  • Is there any way to get upload progress correctly with HttpUrlConncetion
  • How to programmatically clear application data
  • Android: Ripple Background Programmatically
  • 3 Solutions collect form web for “SearchView expand/collapse animation in Android”

    You can actually just grab the LinearLayout by doing the following:

    LinearLayout searchBar = (LinearLayout) searchView.findViewById(;


    SearchView searchView = (SearchView) menu.findItem(;
    final int searchBarId = searchView.getContext().getResources().getIdentifier("android:id/search_bar", null, null);
    LinearLayout searchBar = (LinearLayout) searchView.findViewById(searchBarId);
    searchBar.setLayoutTransition(new LayoutTransition());

    After reading the @Steven answer I got the solution given below

    inside the onCreateOptionsMenu(Menu menu) method write the code

        getMenuInflater().inflate(, menu);
        final MenuItem searchItem = menu.findItem(;
        SearchView searchView = (SearchView) searchItem.getActionView();
        LinearLayout searchBar = (LinearLayout) searchView.findViewById(;
        searchBar.setLayoutTransition(new LayoutTransition());

    and here is my meet_people_menu.xml

    <menu xmlns:android=""
    View searchBar = searchView.findViewById(;
    if (searchBar != null && searchBar instanceof LinearLayout) {
         ((LinearLayout) searchBar).setLayoutTransition(new LayoutTransition());
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.