1

I want to put a my TextView in front of a Button, i tried this but doesnt worked:

<RelativeLayout>
    <Button
        android:id="@+id/button"
        android:layout_width="209dp"
        android:layout_height="115dp"
        android:layout_centerHorizontal="true"
        />
    <TextView
        android:id="@+id/textview"
        android:layout_width="209dp"
        android:layout_height="115dp"
        android:layout_centerHorizontal="true"
        android:text="HELLO"
        android:textSize="50sp"
        />
</RelativeLayout>


public class MainActivity extends AppCompatActivity {
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView)findViewById(R.id.textview);
        textView.bringToFront();
    }
}

The Button is always in front of the TextView.

A. Angulo
  • 15
  • 1
  • 5

1 Answers1

1

That happens beacuse of the elevation system. Button by default has elevation of 2dp and TextView has 0dp. That's why in your case the Button always floats above the TextView. The easiest fix would be to give the TextView a larger elevation value than 2dp.

<RelativeLayout>
    <Button
        android:id="@+id/button"
        android:layout_width="209dp"
        android:layout_height="115dp"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:id="@+id/textview"
        android:layout_width="209dp"
        android:layout_height="115dp"
        android:layout_centerHorizontal="true"
        android:text="HELLO"
        android:textSize="50sp"
        android:elevation="3dp"/>
</RelativeLayout>
Zielony
  • 16,239
  • 6
  • 34
  • 39