Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide a layout when focus on it is lost in XML

I have the following xml file:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/FrameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
</android.support.v4.view.ViewPager>

<LinearLayout
    android:id="@+id/musicLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="10dp"
    android:focusable="true"
    android:visibility="visible" >

    <ImageView
        android:id="@+id/backward"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/backward" />

    <ImageView
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        android:src="@drawable/stop" />

    <ImageView
        android:id="@+id/pausePlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        android:src="@drawable/play" />

    <ImageView
        android:id="@+id/forward"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        android:paddingRight="10dp"
        android:src="@drawable/forward" />

</LinearLayout>

The framelayout is for an actionbar with tabs which are the viewpager in the above layout. The actionbar works perfect. The problem that I want to solve is that the last linearlayout, which is actually a media player floating in the bottom of the screen, needs to be hidden when I press outside the linearlayout! I have tested the onfocuschangelistener in the musicLayout and onclick in the pageviewer but nothing happens! What am i doing wrong?
Edit:
In case it helps, every tab is a fragment with its own layout which is added to the viewPager.

like image 284
user2065529 Avatar asked Nov 04 '22 04:11

user2065529


1 Answers

I think the easiest solution to this problem would be to add a transparent view that matches parent of your ViewGroup. Basically, you would have the ViewPager, LinearLayout and View at the same level. You can then register an onClickListener on your transparent View and that should do the trick.

Layout should be something as simple as this :

<View android:id="@+id/transparent_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:clickable="true"/>
like image 124
ben Avatar answered Nov 14 '22 01:11

ben