1

I'm going to create a remote control with a dynamic appearance. So, I want to add all buttons programmatically. Each button has background, icon, and ?selectableItemBackgroundBorderless drawables. According to this guide I've used two layouts to get the borderless background above button's background. The problem is, the view that is added to parent layout, is displayed above the view that is added to child layout, and covers it.

fragment_remote_control.xml:

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

    <FrameLayout
        android:id="@+id/frag_remote_button_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent" />

</FrameLayout>

Fragment's onCreateView():

public View onCreateView(@NonNull LayoutInflater inflater,
                         @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {

    FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.fragment_remote_control,
            container, false);
    FrameLayout buttonLayout = layout.findViewById(R.id.frag_remote_button_container);

    mRemoteControl = new ACRemoteControl(mDevice, mIRControl);
    mRemoteControl.drawInterface(inflater.getContext(), layout, buttonLayout,
            ScreenInfo.getByResources(getResources()));

    return layout;
}

RemoteControl.java:

public abstract class RemoteControl {

    private static int getSelectableItemBackgroundResID(@NonNull Context context) {

        TypedValue outValue = new TypedValue();
        if (Build.VERSION.SDK_INT >= 21) {
            context.getTheme()
                    .resolveAttribute(R.attr.selectableItemBackgroundBorderless, outValue, true);
        } else {
            context.getTheme()
                    .resolveAttribute(R.attr.selectableItemBackground, outValue, true);
        }
        return outValue.resourceId;
    }

    protected ImageButton placeCircleButton(@NonNull Context context,
                                            @NonNull FrameLayout layout,
                                            @NonNull FrameLayout buttonLayout,
                                            int x, int y, int iconResID) {

        Resources res = context.getResources();
        int sizeFull = res.getDimensionPixelSize(R.dimen.ir_bg_circle_full_size);
        int size = res.getDimensionPixelSize(R.dimen.ir_bg_circle_size);
        FrameLayout.LayoutParams lp;

        View bg = new View(context);
        lp = new FrameLayout.LayoutParams(sizeFull, sizeFull);
        lp.leftMargin = x - sizeFull / 2;
        lp.topMargin = y - sizeFull / 2;
        layout.addView(bg, lp);
        bg.setBackgroundResource(R.drawable.ir_button_circle_bg);

        ImageButton imageButton = new ImageButton(context);
        lp = new FrameLayout.LayoutParams(size, size);
        lp.leftMargin = x - size / 2;
        lp.topMargin = y - size / 2;
        buttonLayout.addView(imageButton, lp);
        imageButton.setBackgroundResource(getSelectableItemBackgroundResID(context));
        imageButton.setImageResource(iconResID);

        return imageButton;
    }

    public abstract void drawInterface(@NonNull Context context,
                                       @NonNull FrameLayout layout,
                                       @NonNull FrameLayout buttonLayout,
                                       @NonNull ScreenInfo screenInfo);

    protected RemoteControl(byte type) {
        this.type = type;
    }
}

ACRemoteControl.java

public class ACRemoteControl extends RemoteControl {

    private final IRControl mIRControl;

    @Override
    public void drawInterface(@NonNull Context context,
                              @NonNull FrameLayout layout,
                              @NonNull FrameLayout buttonLayout,
                              @NonNull ScreenInfo screenInfo) {

        placeCircleButton(context, layout, buttonLayout,
                screenInfo.getWidth() / 2, screenInfo.getHeight() / 2,
                R.drawable.round_power_settings_new_black_24);
    }

    public ACRemoteControl(@NonNull GeneralDevice device,
                           @NonNull IRControl irControl) {
        super(TYPE_AIR_CONDITIONER);
        mIRControl = irControl;
    }
}

I expect to get this:

Expected result

But in practice I get this:

Obtained result

h.nodehi
  • 1,036
  • 1
  • 17
  • 32

0 Answers0