How to handle textview link click to open new activity?
Step 1: Here is how the Textview looks like.
Activity 1:
TextView tv = (TextView) findViewById(R.id.buy_tc_label);
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setText(Html.fromHtml(getString(R.string.terms_and_condition_start) + " <a href='com.package.name://tc/terms'>" + getString(R.string.terms_and_conditions) + "</a>"));
Strings.xml
<string name="terms_and_condition_start">I accept the</string>
<string name="terms_and_conditions">Terms & Conditions</string>
Step 2: manifest.xml
<activity
android:name=".TCsActivity"
android:theme="@style/Theme" >
<intent-filter>
<data
android:scheme="com.package.name" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
This means that links starting with com.package.name:// will be handled by TCsActivity.
So all I have to do is construct a URL that contains the information I want to convey:
com.package.name://action-to-be-needed/abc
In my TCsActivity, I can retrieve this.
TCsActivity:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.terms_conds);
WebView webView = (WebView) findViewById(R.id.terms_and_conditions);
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.GINGERBREAD)
{
MyWebViewClient webViewClient = new MyWebViewClient(this);
webView.setWebViewClient(webViewClient);
}
webView.setWebChromeClient(new WebChromeClient());
String tc = "blah blah blah terms and condition content goes here";
webView.loadDataWithBaseURL (null, tc, "text/html", "utf-8", "about:blank");
}
output Xml layout: terms_conds.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/terms_and_conditions"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Step 1: Here is how the Textview looks like.
Activity 1:
TextView tv = (TextView) findViewById(R.id.buy_tc_label);
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setText(Html.fromHtml(getString(R.string.terms_and_condition_start) + " <a href='com.package.name://tc/terms'>" + getString(R.string.terms_and_conditions) + "</a>"));
Strings.xml
<string name="terms_and_condition_start">I accept the</string>
<string name="terms_and_conditions">Terms & Conditions</string>
Step 2: manifest.xml
<activity
android:name=".TCsActivity"
android:theme="@style/Theme" >
<intent-filter>
<data
android:scheme="com.package.name" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
This means that links starting with com.package.name:// will be handled by TCsActivity.
So all I have to do is construct a URL that contains the information I want to convey:
com.package.name://action-to-be-needed/abc
In my TCsActivity, I can retrieve this.
TCsActivity:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.terms_conds);
WebView webView = (WebView) findViewById(R.id.terms_and_conditions);
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.GINGERBREAD)
{
MyWebViewClient webViewClient = new MyWebViewClient(this);
webView.setWebViewClient(webViewClient);
}
webView.setWebChromeClient(new WebChromeClient());
String tc = "blah blah blah terms and condition content goes here";
webView.loadDataWithBaseURL (null, tc, "text/html", "utf-8", "about:blank");
}
output Xml layout: terms_conds.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/terms_and_conditions"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.
ReplyDeletedata science training