1

I have used a search widget in my app. I wanted to bring it in front of everything and I did it and tested it on real device. It worked fine. But when I tested it on a lollipop device, it didn't work. I then came to know that bringToFront() doesn't work well in Lollipop. So I can use setElevation() or something else(maybe setZ). But, I want to work it on every android device starting from API 17 to the latest version. How do I do that?

I am pasting my code below.

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.clear();
        MaterialSearchView searchView = (MaterialSearchView) view.findViewById(R.id.search_view); //Search experiment
        inflater.inflate(R.menu.toolbar_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
        MenuItem item = menu.findItem(R.id.search_top);
        searchView.setMenuItem(item);
        searchView.bringToFront();
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Soumya Rauth
  • 1,163
  • 5
  • 16
  • 32
  • _then came to know that `bringToFront()` doesn't work well in Lollipop_ how? can you replace `searchView.bringToFront();` with `view.bringChildToFront(searchview);` is the same thing though, – Elltz Feb 07 '17 at 12:23
  • ok let me try it out. – Soumya Rauth Feb 07 '17 at 12:38
  • nope.. Didn't work... :( @Elltz – Soumya Rauth Feb 07 '17 at 12:44
  • Have you figured it out yet? – Pierre Feb 10 '17 at 17:00
  • YES!! I did it using android:elevation. My search view was showing underneath the sliding tab layout. And the elevation of my search view was set lower than sliding tab. So I set the search elevation to 2dp and also set slidingTab's elevation to 0 dp. And it worked perfect. . – Soumya Rauth Feb 10 '17 at 18:01

2 Answers2

1

Check the elevation of the searchview. To set the elevation go to the XML file and within the searchView set android:elevation="2dp" or may be something higher if that doesnot work. The problem is, the element which hides or overlaps the searchview has a higher elevation than your searchview. You could set the overlapping element's elevation to 0dp. e.g: android:elevation="0dp". And bringToFront() won't just work on Lollipop. This hopefully solves the problem

Soumya Rauth
  • 1,163
  • 5
  • 16
  • 32
0

I have figured it out

if (android.os.Build.VERSION.SDK_INT >= 21) //Lollipop
{
    view.setZ(15);

    for (int i = 0; i < view.getParent().getChildCount(); i++)
    {
        if(view != view.getParent().getChildAt(i))
        {
            view.getParent().getChildAt(i).SetZ(5);
        }
    }
}

view.bringToFront();
for (int i = 0; i < view.getParent().getChildCount(); i++)
{
    view.getParent().getChildAt(i).invalidate();
}
view.getParent().requestLayout();
view.getParent().invalidate();

Tested on

  • Marshmallow

  • JellyBean

Pierre
  • 8,397
  • 4
  • 64
  • 80