Kingfisher Phuoc
Kingfisher Phuoc

Reputation: 8190

Button in top of SurfaceView when adding to WindowManager

This is my layout file:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:orientation="vertical">

<SurfaceView
    android:id="@+id/mediaView"
    android:layout_width="300dp"
    android:layout_height="200dp"
    android:background="@android:color/transparent" />

<Button
    android:id="@+id/btnClose"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="close" />

</FrameLayout>

This is how I added it as System alert windows:

  // hiển thị floating view
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.LEFT;
    params.x = 0;
    params.y = 100;

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    windowManager.addView(abovedFrameLayout, params);

My problem here is that no matter what I try, the button is never showed. It's always under the SurfaceView. I tested with: mSurfaceView.setZOrderMediaOverlay(false); mSurfaceView.setZOrderOnTop(false);, but nothing seems to work. Do you have any suggestion? Thanks.

Upvotes: 0

Views: 880

Answers (2)

Moien.Dev
Moien.Dev

Reputation: 1058

This code tested and surfaceviewer and botton are not overlap Xml layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btnClose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="close" />

    <SurfaceView
        android:id="@+id/mediaView"
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:background="@android:color/transparent" />

</LinearLayout>

Acitivy :

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.TYPE_TOAST,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
        PixelFormat.TRANSLUCENT);

params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;

windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

ImageView v = new ImageView(surfaceview.this);
v.setBackgroundResource(R.drawable.nimbledroid);
windowManager.addView(v, params);

Upvotes: 1

Tosin Onikute
Tosin Onikute

Reputation: 4002

Try this, you should use RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <SurfaceView
        android:id="@+id/mediaView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_gravity="bottom"
        android:padding="20dp">

        <Button
            android:id="@+id/btnClose"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="close" />

    </RelativeLayout>

</FrameLayout>

Upvotes: 1

Related Questions