Saturday, 11 February 2017

Android Custom Font Example Tutorial

Android Custom Font Example Tutorial.

1.      Create a new project.
2.      Create a folder called assets and place all your fonts in it.

     


Activity_main.xml
<?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">

    <TextView
        android:id="@+id/ghost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="30dp"
        android:text="MIHIR" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/etd"
        android:textSize="30dp"
        android:gravity="center"
        android:layout_marginTop="50dip"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:id="@+id/btn"
        android:text="Hello"/>

</LinearLayout>
MainActivity.java
package com.cutomfontexample;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {


    @Override

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

        String fontPath = "CHILLER.TTF";
        // Loading Font Face
        Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);

        // text view label
        TextView txtGhost = (TextView) findViewById(R.id.ghost);

        // edittext view
        EditText etd = (EditText)findViewById(R.id.etd);

        // button view
        Button btn = (Button)findViewById(R.id.btn);

        // Applying font
        txtGhost.setTypeface(tf);
        etd.setTypeface(tf);
        btn.setTypeface(tf);

    }

}



Saturday, 28 January 2017

Android Snack Bar Tutorial

Here is code to create snackbar in your application. Here I provide simple, custom and with action call back method snack bar in this code tutorial.
       
  
First of all you have to take design support library in your gradle.
compile 'com.android.support:design:25.1.0'

Use the following code.
MainActivity.java
package com.snackbarexample;



import android.graphics.Color;

import android.os.Bundle;

import android.support.design.widget.CoordinatorLayout;

import android.support.design.widget.FloatingActionButton;

import android.support.design.widget.Snackbar;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;



public class MainActivity extends AppCompatActivity {



    private CoordinatorLayout coordinatorLayout;

    private Button b_simple, b_action, b_custom;

    private FloatingActionButton fab;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);



        b_simple = (Button) findViewById(R.id.btn_simple);

        b_action = (Button) findViewById(R.id.btn_action);

        b_custom = (Button) findViewById(R.id.btn_custom);



        b_simple.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                Snackbar snackbar = Snackbar

                        .make(coordinatorLayout, "Welcome to devilsparadise9", Snackbar.LENGTH_LONG);



                snackbar.show();

            }

        });



        b_action.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                Snackbar snackbar = Snackbar

                        .make(coordinatorLayout, "Message deleted", Snackbar.LENGTH_LONG)

                        .setAction("UNDO", new View.OnClickListener() {

                            @Override

                            public void onClick(View view) {

                                Snackbar snackbar1 = Snackbar.make(coordinatorLayout, "Message restored!", Snackbar.LENGTH_SHORT);

                                snackbar1.show();

                            }

                        });



                snackbar.show();

            }

        });



        b_custom.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                Snackbar snackbar = Snackbar

                        .make(coordinatorLayout, "No internet connection!", Snackbar.LENGTH_LONG)

                        .setAction("RETRY", new View.OnClickListener() {

                            @Override

                            public void onClick(View view) {

                            }

                        });



                // Changing message text color

                snackbar.setActionTextColor(Color.BLUE);



                // Changing action button text color

                View sbView = snackbar.getView();

                TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);

                textView.setTextColor(Color.WHITE);



                snackbar.show();

            }

        });

    }

}

activity_main.xml
<android.support.design.widget.CoordinatorLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/coordinatorLayout"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity">

    

    <LinearLayout

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical"

        android:paddingLeft="20dp"

        android:paddingRight="20dp">



        <Button

            android:id="@+id/btn_simple"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:layout_marginTop="30dp"

            android:text="Simple Snackbar" />



        <Button

            android:id="@+id/btn_action"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:layout_marginTop="10dp"

            android:text="Action Callback Snackbar" />



        <Button

            android:id="@+id/btn_custom"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:layout_marginTop="10dp"

            android:text="Custom Snackbar" />



    </LinearLayout>



</android.support.design.widget.CoordinatorLayout>

By using this code you can get the snack bar in your app.

Thank You..

Friday, 27 January 2017

Tutorial to get current longitude latitude in android application


This in an example to get your current location as toast in your application.



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

    <
uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <
uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION" />
    <
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>

MainActivity.java
package com.getcurrentlocation;



import android.Manifest;

import android.app.Activity;

import android.os.Bundle;

import android.support.v4.app.ActivityCompat;

import android.test.mock.MockPackageManager;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;



public class MainActivity extends Activity {



    Button btnShowLocation;

    private static final int REQUEST_CODE_PERMISSION = 2;

    String mPermission = Manifest.permission.ACCESS_FINE_LOCATION;



    // GPSTracker class

    GPSTracker gps;



    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        try {

            if (ActivityCompat.checkSelfPermission(this, mPermission)

                    != MockPackageManager.PERMISSION_GRANTED) {



                ActivityCompat.requestPermissions(this, new String[]{mPermission},

                        REQUEST_CODE_PERMISSION);



                /*// If any permission above not allowed by user, this condition will

                execute every time, else your else part will work*/

            }

        } catch (Exception e) {

            e.printStackTrace();

        }



        btnShowLocation = (Button) findViewById(R.id.button);



        // show location button click event

        btnShowLocation.setOnClickListener(new View.OnClickListener() {



            @Override

            public void onClick(View arg0) {

                // create class object

                gps = new GPSTracker(MainActivity.this);



                // check if GPS enabled

                if(gps.canGetLocation()){



                    double latitude = gps.getLatitude();

                    double longitude = gps.getLongitude();



                    // \n is for new line

                    Toast.makeText(getApplicationContext(), "Your Location is - \nLat: "+ latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();

                }else{

                    // can't get location

                    // GPS or Network is not enabled

                    // Ask user to enable GPS/network in settings

                    gps.showSettingsAlert();

                }

            }

        });

    }

}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/activity_main"

    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="com.getcurrentlocation.MainActivity">



    <Button

        android:id = "@+id/button"

        android:layout_width = "fill_parent"

        android:layout_height = "wrap_content"

        android:text = "getlocation"/>

</RelativeLayout>

GPSTracker.java
package com.getcurrentlocation;



import android.app.AlertDialog;

import android.app.Service;

import android.content.Context;

import android.content.DialogInterface;

import android.content.Intent;

import android.location.Location;

import android.location.LocationListener;

import android.location.LocationManager;

import android.os.Bundle;

import android.os.IBinder;

import android.provider.Settings;

import android.util.Log;



public class GPSTracker extends Service implements LocationListener {



    private final Context mContext;



    // flag for GPS status

    boolean isGPSEnabled = false;



    // flag for network status

    boolean isNetworkEnabled = false;



    // flag for GPS status

    boolean canGetLocation = false;



    Location location; // location

    double latitude; // latitude

    double longitude; // longitude



    // The minimum distance to change Updates in meters

    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters



    // The minimum time between updates in milliseconds

    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute



    // Declaring a Location Manager

    protected LocationManager locationManager;



    public GPSTracker(Context context) {

        this.mContext = context;

        getLocation();

    }



    public Location getLocation() {

        try {

            locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);



            // getting GPS status

            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);



            // getting network status

            isNetworkEnabled = locationManager

                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);



            if (!isGPSEnabled && !isNetworkEnabled) {

                // no network provider is enabled

            } else {

                this.canGetLocation = true;

                // First get location from Network Provider

                if (isNetworkEnabled) {

                    locationManager.requestLocationUpdates(

                            LocationManager.NETWORK_PROVIDER,

                            MIN_TIME_BW_UPDATES,

                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);



                    Log.d("Network", "Network");

                    if (locationManager != null) {

                        location = locationManager

                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);



                        if (location != null) {

                            latitude = location.getLatitude();

                            longitude = location.getLongitude();

                        }

                    }

                }



                // if GPS Enabled get lat/long using GPS Services

                if (isGPSEnabled) {

                    if (location == null) {

                        locationManager.requestLocationUpdates(

                                LocationManager.GPS_PROVIDER,

                                MIN_TIME_BW_UPDATES,

                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);



                        Log.d("GPS Enabled", "GPS Enabled");

                        if (locationManager != null) {

                            location = locationManager

                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);



                            if (location != null) {

                                latitude = location.getLatitude();

                                longitude = location.getLongitude();

                            }

                        }

                    }

                }

            }



        } catch (Exception e) {

            e.printStackTrace();

        }



        return location;

    }



    /**

     * Stop using GPS listener

     * Calling this function will stop using GPS in your app

     * */



    public void stopUsingGPS(){

        if(locationManager != null){

            locationManager.removeUpdates(GPSTracker.this);

        }

    }



    /**

     * Function to get latitude

     * */



    public double getLatitude(){

        if(location != null){

            latitude = location.getLatitude();

        }



        // return latitude

        return latitude;

    }



    /**

     * Function to get longitude

     * */



    public double getLongitude(){

        if(location != null){

            longitude = location.getLongitude();

        }



        // return longitude

        return longitude;

    }



    /**

     * Function to check GPS/wifi enabled

     * @return boolean

     * */



    public boolean canGetLocation() {

        return this.canGetLocation;

    }



    /**

     * Function to show settings alert dialog

     * On pressing Settings button will lauch Settings Options

     * */



    public void showSettingsAlert(){

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);



        // Setting Dialog Title

        alertDialog.setTitle("GPS is settings");



        // Setting Dialog Message

        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");



        // On pressing Settings button

        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog,int which) {

                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);

                mContext.startActivity(intent);

            }

        });



        // on pressing cancel button

        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {

                dialog.cancel();

            }

        });



        // Showing Alert Message

        alertDialog.show();

    }



    @Override

    public void onLocationChanged(Location location) {

    }



    @Override

    public void onProviderDisabled(String provider) {

    }



    @Override

    public void onProviderEnabled(String provider) {

    }



    @Override

    public void onStatusChanged(String provider, int status, Bundle extras) {

    }



    @Override

    public IBinder onBind(Intent arg0) {

        return null;

    }

}


CCAvenue Payment Gateway Android Integration

CCAvenue Payment Gateway Android Integration CCAvenue payment gateway android integration using PHP RSA and Response Handling. An...