Fatemeh
Fatemeh

Reputation: 177

Text View on click listener

I have three text view on page and each of them has on click method. when I click on each of them,Text view3 is called. what is wrong?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.card.admin.nfcapp.ReadCertificateActivity"
android:id="@+id/Test">

<TextView
    android:id="@+id/dipcert1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textSize="30dp"
    android:text="@string/cert1"
    android:gravity="center"
    android:paddingBottom="200dp"
    android:onClick="btnClickListener1 />

<TextView
    android:id="@+id/dipcert2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textSize="30dp"
    android:text="@string/cert2"
    android:gravity="center"
    android:paddingBottom="100dp"
    android:onClick="btnClickListener2 "/>

<TextView
    android:id="@+id/dipcert3"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textSize="30dp"
    android:text="@string/cert3"
    android:gravity="center"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:paddingBottom="10dp"
    android:onClick="btnClickListener3"/>

</RelativeLayout>

and when I click aon each of them "btnClickListener3" is called.

Upvotes: 0

Views: 149

Answers (2)

Akash Patel
Akash Patel

Reputation: 2767

All three Textview has same layout_width="fill_parent" and layout_height="fill_parent" in RelativeLayout so it detect click on only textview3.

Use LinearLayout with orientation="vertical".

And set layout_width="wrap_content" and layout_height="wrap_content" to all Textview.

Also as answered by @aleksandrbel Remove last space from the name of functions in textview 1 and 2.

Upvotes: 1

aleksandrbel
aleksandrbel

Reputation: 1490

Take away space after the name of functions 1 and 2

 android:onClick="btnClickListener1"

and

 android:onClick="btnClickListener2"

But for the purposes if the TextView should do the functionality of the Button, so the user need to know that that he can click it, Material Design suggest to use of cause Buttons.

https://material.google.com/components/buttons.html#

You can just take away the background, if you want to have transparent background

android:background="@android:color/transparent"

Upvotes: 1

Related Questions