Pages

Wednesday, September 21, 2016

Android Example- a Transform Filter (create keyword to serch directly into website)



activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/textview1" />

</RelativeLayout>

MainActivity.java
package in.kantapp.example36;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.os.Bundle;
import android.app.Activity;
import android.text.util.Linkify;
import android.text.util.Linkify.TransformFilter;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TransformFilter myTransformFilter = new TransformFilter() {
            @Override
            public String transformUrl(Matcher match, String url) {
                // TODO Auto-generated method stub
                return url.substring(1).toLowerCase();
            }
        };

        TextView text = (TextView) findViewById(R.id.textview1);
        Pattern pattern3 = Pattern.compile("\\![a-zA-Z]+");
        text
                .setText("press one of these words to search it on google:"
                        + " !kantapp !twitter !facebook");
        Linkify.addLinks(text, pattern3,
                "http://www.google.ie/search?q=", null, myTransformFilter);

    }


}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.example36">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <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