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>

Android Example- Match 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="hello_world"
        android:id="@+id/textview1" />

</RelativeLayout>

MainActivity.java
package in.kantapp.example35;

import java.util.regex.Pattern;

import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.text.util.Linkify;
import android.text.util.Linkify.MatchFilter;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
{

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

        MatchFilter myMatchFilter = new MatchFilter()
        {
            @Override
            public boolean acceptMatch(CharSequence cs, int start, int end)
            {
                return ((start == 0 || cs.charAt(start - 1) == '!') && !(cs
                        .charAt(start) == 'p'));
            }
        };

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

    }



}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.example35">
    <uses-permission android:name="android.permission.INTERNET" />
    <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>

Android Example- Linkify class in android


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:id="@+id/textview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="95dp"
        android:text="hello"
        android:textSize="30dp" />

    <TextView
        android:id="@+id/textview2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="186dp"
        android:text="kantapp.in"
        android:textSize="30dp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textview2"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textview2"
        android:layout_marginTop="88dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

MainActivity.java
package in.kantapp.example34;

import java.util.regex.Pattern;

import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.text.util.Linkify;
import android.text.util.Linkify.MatchFilter;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

        TextView WebSiteone = (TextView) findViewById(R.id.textview1);
        WebSiteone.setText("Go to : google.com");
        Linkify.addLinks(WebSiteone, Linkify.WEB_URLS);

        TextView WebSitetwo = (TextView) findViewById(R.id.textview2);
        WebSitetwo.setText("Go to : www.kantapp.in");
        Linkify.addLinks(WebSitetwo, Linkify.WEB_URLS);

        TextView myCustomLink = (TextView) findViewById(R.id.textView3);
        Pattern p1 = Pattern.compile("\\bAndroid+\\b");
        myCustomLink.setText("Click on Android " +
                "to search it on google");
        Linkify.addLinks(myCustomLink, p1, "http://www.google.ie/search?q=");

    }



}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.example34">
    <uses-permission android:name="android.permission.INTERNET"/>
    <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>

Tuesday, September 20, 2016

Android Example- Implicit Intent


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

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="14dp"
        android:background="#69d0ff" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/spinner1"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="26dp"
        android:text="Button" />

</RelativeLayout>

MainActivity.java
package in.kantapp.example33;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    private Spinner spinner_object;

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

        spinner_object = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
                R.array.intents, android.R.layout.simple_spinner_dropdown_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner_object.setAdapter(adapter);
        Button but = (Button) findViewById(R.id.button1);
        but.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int position = spinner_object.getSelectedItemPosition();
        Intent intent = null;
        switch (position) {
            case 0:
                intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("http://www.google.com"));
                break;
            case 1:
                // dials the call directly to the number specified
                intent = new Intent(Intent.ACTION_CALL,
                        Uri.parse("tel:(+91)8888888888"));
                break;
            case 2:
                // opens up the to - dial but does not dials the number
                intent = new Intent(Intent.ACTION_DIAL,
                        Uri.parse("tel:(+91)8888888888"));

                break;
            case 3:
            /*
             * opens up the specified location in the google map by its
             * longitude and latitude
             */
                intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("geo:28.632323175362604,77.22595474023433?z=19"));

                break;
            case 4:
                // to search for india gate on google map
                intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("geo:0,0?q=india gate"));
                break;
            case 5:
                // opens up the camera to capture an image
                intent = new Intent("android.media.action.IMAGE_CAPTURE");
                break;
            case 6:
                // opens up contacts menu of your phone
                intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("content://contacts/people/"));
                break;
            case 7:
                // edits the first contact on your contact list
                intent = new Intent(Intent.ACTION_EDIT,
                        Uri.parse("content://contacts/people/1"));
                break;

        }
        if (intent != null) {
            startActivity(intent);
        }
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == 0) {
            String result = data.toURI();
            Toast.makeText(this, result, Toast.LENGTH_LONG);
        }
    }

}

Intent.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="intents">
        <item>Open Browser</item>
        <item>Dial a Call </item>
        <item>Dial</item>
        <item>Show Map</item>
        <item>Search on Map</item>
        <item>Take picture</item>
        <item>Show contacts</item>
        <item>Edit first contact</item>
    </string-array>

</resources>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.example33">
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <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>