1

I am using a relative layout, and this is a section taken from it:

<ImageView
    android:id="@+id/btnLifeMinus5"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="#2a80b9"
    android:visibility="invisible"
    android:adjustViewBounds="false"
    android:clickable="true"
    android:cropToPadding="false"
    android:padding="0dp"
    android:scaleType="fitStart"
    android:layout_marginLeft="0dp"
    android:layout_marginTop="0dp" />

<ImageView
    android:id="@+id/btnLifePlus5"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="#2a80b9"
    android:visibility="invisible"
    android:adjustViewBounds="false"
    android:clickable="true"
    android:cropToPadding="false"
    android:padding="0dp"
    android:scaleType="fitStart"
    android:layout_marginLeft="0dp"
    android:layout_marginTop="0dp" />

<ImageView
    android:id="@+id/btnLifePlus1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="#2a80b9"
    android:visibility="invisible"
    android:adjustViewBounds="false"
    android:clickable="true"
    android:cropToPadding="false"
    android:padding="0dp"
    android:scaleType="fitStart"
    android:layout_marginLeft="0dp"
    android:layout_marginTop="0dp" />

<ImageView
    android:id="@+id/btnLifeMinus1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="#2a80b9"
    android:visibility="invisible"
    android:adjustViewBounds="false"
    android:clickable="true"
    android:cropToPadding="false"
    android:padding="0dp"
    android:scaleType="fitStart"
    android:layout_marginLeft="0dp"
    android:layout_marginTop="0dp" />

<TextView
    android:id="@+id/txtLifePlus5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible"
    android:adjustViewBounds="false"
    android:clickable="true"
    android:cropToPadding="false"
    android:textColor="#000000"
    android:text="+5"
    android:padding="0dp" />

<TextView
    android:id="@+id/txtLifePlus1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible"
    android:adjustViewBounds="false"
    android:clickable="true"
    android:cropToPadding="false"
    android:textColor="#000000"
    android:text="+1"
    android:padding="0dp" />

<TextView
    android:id="@+id/txtLifeMinus5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible"
    android:adjustViewBounds="false"
    android:clickable="true"
    android:cropToPadding="false"
    android:textColor="#000000"
    android:text="-5"
    android:padding="0dp" />

<TextView
    android:id="@+id/txtLifeMinus1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible"
    android:adjustViewBounds="false"
    android:clickable="true"
    android:cropToPadding="false"
    android:textColor="#000000"
    android:text="-1"
    android:padding="0dp" />

I have the following method in my class that sets these buttons up on the screen. This section places the text onto the buttons:

private void setTextPlacements() {
    final float SCALE = getResources().getDisplayMetrics().density;
    final TextView txtPlus1 = (TextView) findViewById(R.id.txtLifePlus1);
    final TextView txtPlus5 = (TextView) findViewById(R.id.txtLifePlus5);
    final TextView txtMinus1 = (TextView) findViewById(R.id.txtLifeMinus1);
    final TextView txtMinus5 = (TextView) findViewById(R.id.txtLifeMinus5);
    final ImageView btnPlus5 = (ImageView) findViewById(R.id.btnLifePlus5);
    final ImageView btnPlus1 = (ImageView) findViewById(R.id.btnLifePlus1);
    final ImageView btnMinus5 = (ImageView) findViewById(R.id.btnLifeMinus5);
    final ImageView btnMinus1 = (ImageView) findViewById(R.id.btnLifeMinus1);

    RelativeLayout.LayoutParams txtP1 = new RelativeLayout.LayoutParams(txtPlus1.getLayoutParams());
    txtP1.leftMargin = Math.round((btnPlus1.getLeft() + (btnPlus1.getWidth()/2)) - (txtPlus1.getWidth()/2));
    txtP1.topMargin = Math.round((btnPlus1.getTop() + (btnPlus1.getHeight()/2)) - (txtPlus1.getHeight()/2));
    txtPlus1.setLayoutParams(txtP1);

    RelativeLayout.LayoutParams txtP5 = new RelativeLayout.LayoutParams(txtPlus5.getLayoutParams());
    txtP5.leftMargin = Math.round((btnPlus5.getLeft() + (btnPlus5.getWidth()/2)) - (txtPlus5.getWidth()/2));
    txtP5.topMargin = Math.round((btnPlus5.getTop() + (btnPlus5.getHeight()/2)) - (txtPlus5.getHeight()/2));
    txtPlus5.setLayoutParams(txtP5);

    RelativeLayout.LayoutParams txtM1 = new RelativeLayout.LayoutParams(txtMinus1.getLayoutParams());
    txtM1.leftMargin = Math.round((btnMinus1.getLeft() + (btnMinus1.getWidth()/2)) - (txtMinus1.getWidth()/2));
    txtM1.topMargin = Math.round((btnMinus1.getTop() + (btnMinus1.getHeight()/2)) - (txtMinus1.getHeight()/2));
    txtMinus1.setLayoutParams(txtM1);

    RelativeLayout.LayoutParams txtM5 = new RelativeLayout.LayoutParams(txtMinus5.getLayoutParams());
    txtM5.leftMargin = Math.round((btnMinus5.getLeft() + (btnMinus5.getWidth()/2)) - (txtMinus5.getWidth()/2));
    txtM5.topMargin = Math.round((btnMinus5.getLeft() + (btnMinus5.getWidth()/2)) - (txtMinus5.getWidth()/2));
    txtMinus5.setLayoutParams(txtM5);
}

There is a lot going on in this app, and everything is working perfectly except for one thing: txtLifeMinus5 seems to be sitting UNDERNEATH btnLifeMinus5 and I just cannot figure out why. Everything is set up identically to everything else I have done in layouts throughout the entire app, but this one just doesn't behave properly.

I am working on the assumption that items listed higher up in the layout XML document sit UNDERNEATH all the items below it... so what the heckcheese is going on here?

It's not an issue with the text not being visible, as I have experimented displaying it in different places on the screen. As soon as it sits over the btnLifeMinus5 button, it vanishes underneath.

Argh! Any thoughts?

Bisclavret
  • 1,327
  • 9
  • 37
  • 65
  • You issue is that Z-order of the views defined in `RelativeLayout` is different from the order of their definition in XML file, right? I suggest you add "Z-order" to the title of the question to draw more attention. Anyhow, this answer (http://stackoverflow.com/questions/2614393/defining-z-order-of-views-of-relativelayout-in-android) implies that Z-order should follow the ordering in XML. Please share some screenshots of your issue, as well as a full example of code that can be tested by us. – Vasiliy Jun 13 '15 at 12:59

1 Answers1

1

Oh my god, I am an idiot. Sorry everyone. The answer was simple, and there is no bug with layout order at all. This code:

RelativeLayout.LayoutParams txtM5 = new RelativeLayout.LayoutParams(txtMinus5.getLayoutParams());
txtM5.leftMargin = Math.round((btnMinus5.getLeft() + (btnMinus5.getWidth()/2)) - (txtMinus5.getWidth()/2));
txtM5.topMargin = Math.round((btnMinus5.getLeft() + (btnMinus5.getWidth()/2)) - (txtMinus5.getWidth()/2));
txtMinus5.setLayoutParams(txtM5);

Is different to all of this code:

RelativeLayout.LayoutParams txtP1 = new RelativeLayout.LayoutParams(txtPlus1.getLayoutParams());
txtP1.leftMargin = Math.round((btnPlus1.getLeft() + (btnPlus1.getWidth()/2)) - (txtPlus1.getWidth()/2));
txtP1.topMargin = Math.round((btnPlus1.getTop() + (btnPlus1.getHeight()/2)) - (txtPlus1.getHeight()/2));
txtPlus1.setLayoutParams(txtP1);

RelativeLayout.LayoutParams txtP5 = new RelativeLayout.LayoutParams(txtPlus5.getLayoutParams());
txtP5.leftMargin = Math.round((btnPlus5.getLeft() + (btnPlus5.getWidth()/2)) - (txtPlus5.getWidth()/2));
txtP5.topMargin = Math.round((btnPlus5.getTop() + (btnPlus5.getHeight()/2)) - (txtPlus5.getHeight()/2));
txtPlus5.setLayoutParams(txtP5);

RelativeLayout.LayoutParams txtM1 = new RelativeLayout.LayoutParams(txtMinus1.getLayoutParams());
txtM1.leftMargin = Math.round((btnMinus1.getLeft() + (btnMinus1.getWidth()/2)) - (txtMinus1.getWidth()/2));
txtM1.topMargin = Math.round((btnMinus1.getTop() + (btnMinus1.getHeight()/2)) - (txtMinus1.getHeight()/2));
txtMinus1.setLayoutParams(txtM1);

See it? The failing section references width where it should be referencing height. This sends the text object miles off the screen. Epic logic fail. Sorry for the waste of time :(

Bisclavret
  • 1,327
  • 9
  • 37
  • 65