Douglas Fornaro
Douglas Fornaro

Reputation: 2037

Android: How to put a textview in front of a button

I want to put a textview in front of a button, I couldn't do it, what I've tried:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <TextView
            android:id="@+id/text_timer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:text="10"
            android:textColor="@android:color/white" />

        <Button
            android:id="@+id/button_alert"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/alert" />
    </RelativeLayout>

I'd like to do it because I want the ripple effect when I click in the button, in the same time I want that show a counter in textview

Can anybody help me? Thanks!

Upvotes: 1

Views: 5127

Answers (3)

Valentin Nasraty
Valentin Nasraty

Reputation: 299

Use android:elevation on the TextView to put it in front of the Button. In this way you keep the effect when button is clicked.

    <Button
            android:id="@+id/button"
            android:layout_width="@dimen/button_width"
            android:layout_height="@dimen/button_height"
            android:background="@color/colorButton"/>

    <TextView
            android:layout_width="@dimen/button_width"
            android:layout_height="@dimen/button_height"
            android:gravity="start|center"
            android:text="This is a button"
            android:elevation="10dp"/>

Upvotes: 3

Hoang Nguyen
Hoang Nguyen

Reputation: 183

You can change to FrameLayout

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">

<Button
    android:id="@+id/button_alert"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/alert" />

<TextView
    android:id="@+id/text_timer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:layout_gravity="right|end|center_vertical"
    android:text="10"
    android:textColor="@android:color/white" />
</FrameLayout>

Or easy move TextView after insert Button ^^

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
xmlns:android="http://schemas.android.com/apk/res/android">

<Button
    android:id="@+id/button_alert"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Aleart" />

<TextView
    android:id="@+id/text_timer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:text="10"
    android:textColor="@android:color/white" />
</RelativeLayout>

Upvotes: 0

Buddy
Buddy

Reputation: 11028

Add this line to your Button's XML:

android:layout_toRightOf="@id/text_timer"

Upvotes: 0

Related Questions