Thursday 21 May 2015

Android example of Deep Linking

Hi friends'

Here i am posting an example of android DEEP LINKING.

In this simple application two activities are created.
MainActivity and
DeepLinking Activity

In manifest file you need to write which activity you want to open.
In this example DeepLinking Activity is set to open.

Manifest file code:

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

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".DeepLinking"
            android:label="@string/title_activity_deep_linking" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <data android:scheme="http"
                      android:host="www.example.in"
                      android:pathPrefix="/deep" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity class code:

package example.deeplinking;

import android.app.Activity;
import android.os.Bundle;


public class MainActivity extends Activity {

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

activity_main layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center">

    <TextView
        android:text="Main Activity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"/>

</LinearLayout>

DeepLinking Activity code:

package example.deeplinking;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;


public class DeepLinking extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deep_linking);
        Intent intent = getIntent();
        String action = intent.getAction();
        Uri data = intent.getData();

        TextView deepLinkTV = (TextView) findViewById(R.id.deepLinkTV);
        deepLinkTV.setText("Action: "+action+"\n URI: "+data.toString());
    }
}


activity_deep_linking layout file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center">

    <TextView
        android:id="@+id/deepLinkTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="20sp"/>

</LinearLayout>


In order to test type the following in command line:
//your uri and package name
adb shell am start -a android.intent.action.VIEW -d "http://www.example.in/deep" example.deeplinking

The sample output:























Monday 18 May 2015

Android Example of showing or hiding password

Hello friends'

Here is a simple example of showing or hiding PASSWORD field.

Here i am setting an image icon on the right side of an edit text which is of type password.
When click or touch on this image icon the password field will be visible and when it touch or click again the password will hide again.

Here is the Layout file

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

//Note android:inputType="textPassword"
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="Enter Password"
        android:drawableRight="@mipmap/eye_icon"
        android:id="@+id/passwordET"/>

</LinearLayout>

Here is the Activity class

package example.hideorviewpassword;

import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;


public class MainActivity extends Activity {

    private EditText passwordET;
    private boolean isPasswordShown;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        passwordET = (EditText) findViewById(R.id.passwordET);
        passwordET.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int DRAWABLE_RIGHT = 2;
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (event.getRawX() >= (passwordET.getRight() - passwordET.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                        if(isPasswordShown) {
                            isPasswordShown = false;
                            passwordET.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.eye_icon, 0);
                            passwordET.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                        }
                        else {
                            isPasswordShown = true;
                            passwordET.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.eye_visible_icon, 0);
                            passwordET.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                        }
                        passwordET.setSelection(passwordET.getText().length());
                    }
                }
                return false;
            }
        });
    }
}

The output will be like:

          


For source code click here