Fatemeh Fatemeh - 1 month ago 22
Android Question

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"/>




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

Answer

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.