Add android:clickable="true"
to your TextView
android:onClick="openBrowser" doesn't work in XML
Question
The android:onClick="openBrowser"
doesn't work when I clicked the text, however it work fine for other image such as Facebook, Twitter and Instagram. The link in valid and it can be clickable when I put the text below the image but doesn't when I change the position. Can anyone help me with this?
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/homepagel2"
android:scaleType = "fitXY" />
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_marginTop="60dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logofacebook"
android:tag="http://www.facebook.com"
android:onClick="openBrowser"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/instagramlogo"
android:tag="http://instagram/"
android:onClick="openBrowser"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/twitter"
android:tag="https://twitter.com"
android:onClick="openBrowser"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout5"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="-90dp"
android:textSize="25sp"
android:text="@string/website"
android:tag="http://www..../"
android:onClick="openBrowser"
android:textColor="#ffffff"/>
</LinearLayout>
Solution
OTHER TIPS
By default, TextViews
aren't clickable. I am not sure that you can perform a onClick
event by using this xml attribute on textview without clickable
attribute. Then, you can try to add this attribute:
android:clickable="true"
Or should make a click listener as below:
((TextView) findViewById(R.id.text1)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openBrowser(v);
}
});
with this view:
<TextView
android:id="@id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="-90dp"
android:textSize="25sp"
android:text="@string/website"
android:tag="http://www..../"
android:textColor="#ffffff" />
The click listener will automatically make it clickable, then the previous attribute will be useless.
This works for me
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:autoLink="all"
android:text="@string/link_text_auto"
/>
source :http://android.okhelp.cz/linkify-text-link-url-in-textview-text-android-example/