Monday, 9 December 2013

How to link a web address in Android's TextView and ImageView (Updated with androidx package)

Once you have developed an application, you will want people contact you regarding feedback or queries about the application. So there has to be some easiest way through which people will reach you. 

For example when the user just clicks a website link, it should take the user to the desired address. This functionality can be achieved with the help of Android's autoLink. Lets see how...



The autoLink will work only for TextViews. What if you want an image to work as a link. In that case Intents are used.

---------------Your xml file will contain the following-----------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#E91E63">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="30sp"
android:textColor="#ffffff"
android:gravity="center"
android:background="#F06292"
android:text="Hit this Url"/>

<TextView
android:layout_gravity="center"
android:id="@+id/la_linkTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
android:textSize="30sp"
android:text="whatanandroid.blogspot.com"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:weightSum="2"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:background="#F06292"
android:textColor="#ffffff"
android:gravity="center"
android:text="Click this Image"/>

<ImageView
android:id="@+id/la_linkIV"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/andro"
android:autoLink="all"
android:text="whatanandroid.blogspot.com" />

</LinearLayout>

</LinearLayout>
----------------------------------------------------------------------------

---------------------------No need to write anything extra in your Java code for TextView linking----------
package com.app.yourappname;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class LinkActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.link_activity);

ImageView imageView = findViewById(R.id.la_linkIV);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent imgLink = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.android.com"));
startActivity(imgLink);
}
});
}
}
------------------------------------------------------------------------------------------

---------------------------- Declare the Activity name in your Manifest file --------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.yourappname">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LinkActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

No comments:

Post a Comment