Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

progressbar on top of Button in relative layout issue in Android Studio

Ok this is a weird one I hope someone can explain to me.

I have a custom button layout which creates a button with a circular progress bar in the middle of the button. My XML code is below. What I can't work out however is that the ProgressBar seems to be appearing behind the button. If I set the button background to anything other than transparent the progressbar cannot be seen. With the button background as transparent I can then see the ProgressBar but it still appears behind the button text. I was under the understanding that views appeared in the order they are added. I have even tried setting the view to be on top (view.bringToFront();) and I've tried removing the view and recreating it.

Why does the progressbar appear behind the button and what can I do to solve it?

Many thanks

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:background="@android:color/holo_blue_bright"
    android:padding="2dp">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:text="Button"
        android:gravity="center"
        android:textColor="@android:color/white"
        android:singleLine="true"
        android:clickable="false">
    </Button>

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:layout_centerInParent="true"
        android:visibility="visible"
        />

</RelativeLayout> 

Code using the above layout

 private void setupTableLayout(int NumberOfRows, int NumberOfButtons){
    TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT);
    TableRow.LayoutParams rowParams = new TableRow.LayoutParams(0, android.widget.TableRow.LayoutParams.MATCH_PARENT, 3f);
    TableLayout tableLayout = (TableLayout) findViewById(R.id.thetablelayout);
    tableLayout.removeAllViews();

    for (int i = 0; i < NumberOfRows; i++) {
        TableRow tableRow = new TableRow(this);
        tableRow.setLayoutParams(tableParams);

        RelativeLayout btnOneLayout = (RelativeLayout)getLayoutInflater().inflate(R.layout.custom_button, null);
        RelativeLayout btnTwoLayout = (RelativeLayout)getLayoutInflater().inflate(R.layout.custom_button, null);

        ProgressBar btnOneProgressBar = (ProgressBar)btnOneLayout.findViewById(R.id.progressBar);
        ProgressBar btnTwoProgressBar = (ProgressBar)btnTwoLayout.findViewById(R.id.progressBar);

        btnOneLayout.setLayoutParams(rowParams);
        btnTwoLayout.setLayoutParams(rowParams);

        Button btnOne = (Button)btnOneLayout.findViewById(R.id.button);
        btnOne.setText("Btn 1, Row " + i);
        btnOne.setId(1001 + i);
        Button btnTwo = (Button)btnTwoLayout.findViewById(R.id.button);
        btnTwo.setText("Btn 2, Row " + i);
        btnTwo.setId(2001 + i);

        setButtonClickListener(btnOneLayout, btnOneProgressBar);
        setButtonLongClickListener(btnOneLayout, btnOneProgressBar);

        tableRow.addView(btnOneLayout); //Add layout, instead of just Button

        View adivider = new View(this);
        adivider.setLayoutParams(new TableRow.LayoutParams(20, TableRow.LayoutParams.MATCH_PARENT));
        adivider.setBackgroundColor(Color.TRANSPARENT);

        // This bit of code deals with odd/even numbers of buttons.
        if (((i + 1) * 2) < NumberOfButtons + 1) {
            tableRow.addView(adivider);
            tableRow.addView(btnTwoLayout);
        } else {
            tableRow.addView(adivider);

            btnTwoLayout.setBackgroundResource(android.R.color.transparent); 
            tableRow.addView(btnTwoLayout);
        }


        tableLayout.addView(tableRow);

    }

}
like image 803
Regnodulous Avatar asked Nov 28 '22 16:11

Regnodulous


1 Answers

You are propably running this on android >= 5.0. In 5.0 they added elevation field for views. Elevation defines z-order of views in ViewGroup.

In that case button have non-zero elevation value and progress bar have zero value elevation.

Set elevation of progress bar to e.g. 10dp

<ProgressBar
    ...
    android:elevation="10dp"/>
like image 194
pjanecze Avatar answered Dec 15 '22 14:12

pjanecze