Mariusel
Mariusel

Reputation: 13

TextView over button disappears when the button is pressed

I have a problem with the app (android studio, java).

  1. I added a textview over a button to style it.
  2. Initially, I set android:elevation="2" for the textview in XML, otherwise it doesn't appear at all.
  3. Now, when you press the button, the textview disappears.
  4. I tried to make a setOnTouchListener that sets the elevation for the button to 1, so the text appears after I stop pressing the button
  5. After i added the listener, the textview does not appear only when the button is held down.

I want the textview to remain visible at all times. Any tips? <3

learnButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                learnButton.setElevation(1);
                return false;
            }
        });
<Button
        android:id="@+id/buttonLearn"
        android:layout_width="match_parent"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_height="100dp"
        android:background="@drawable/style_bg_light"
        android:layout_marginTop="125dp"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:id="@+id/textViewLearn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:fontFamily="@font/dosis"
        android:text="Button Continue"
        android:elevation="2dp"
        android:layout_marginStart="12dp"
        android:layout_marginTop="5dp"
        android:textColor="@color/black"
        android:layout_alignTop="@+id/buttonLearn"
        android:layout_alignStart="@+id/buttonLearn"/>

Upvotes: 1

Views: 200

Answers (1)

Khalid Taha
Khalid Taha

Reputation: 3313

If you want the best practice, you need either to style the button itself or to style the text and put the click listener on the TextView instead of the Button

for example:

<TextView
    android:id="@+id/learn"
    android:width="200pd"
    android:height="48dp"
    android:background="@drawable/button_bg"
    android:gravity="center"
    android:textColor="@color/white"
    android:text="Click To Learn" />

And the file button_bg.xml to be added to the drawable folder, will contain:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <ripple android:color="@color/lightGreyText">
            <item android:drawable="@drawable/static_button_bg" />
        </ripple>
    </item>
</layer-list>

The file button_bg.xml handles the ripple effect on click, and the file static_button_bg.xml contains the style of the button, and it is:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#66EFEFF4" />
    <corners android:radius="8dp" />
</shape>

Upvotes: 1

Related Questions