Friday, 25 November 2016

Find nearest based on GPS


SELECT ( 3959 * acos( cos( radians(28.6219459) ) * cos( radians( Latitude ) ) * 
cos( radians( Longitude ) - radians(77.384108) ) + sin( radians(28.6219459) ) * 
sin( radians( Latitude ) ) ) ) AS distance,DealerName,DealerID 
FROM jcb.tbl_dealer_list HAVING distance < 10 ORDER BY distance LIMIT 0 , 20;

Tuesday, 15 November 2016

Call php page from url


Syntax

http://localhost:90/jcb_api_session/login.php?email=admin@gmail.com&password=admin


OUTPUT

[{"status":1,"msg":"login success..!","Key":"82f36a9bcc570b6250292cc2b1e68376"}]



login.php

<?php
include("include/connection.php");
session_start();

if(isset($_GET['email']) && isset($_GET['password']))
{
    $email=mysqli_real_escape_string($con,$_GET['email']);
    $password=mysqli_real_escape_string($con,$_GET['password']);
   $query="select * from tbl_user_master where Email='$email' AND Password='$password'";
    $res=mysqli_query($con,$query);
    $count=mysqli_num_rows($res);
    if($count == 1)
    {
     $key=$_SESSION['token']=md5(uniqid(mt_rand(),true));
         $json[] = array("status"=> 1, "msg"=>"login success..!" ,"Key"=>$key);
        echo json_encode($json);
    }
    else
    {
         $json[] = array("status"=> 0, "msg"=>"login failed..!" );
        echo json_encode($json);
    }
   
}
else
{
    $json[] = array("status"=> 0, "msg"=>"Not receive required data..!" );
        echo json_encode($json);
   
}

?>


MySQL Database



Friday, 11 November 2016

Showing Gif Image in Android


1. Add below code to app level gradle

dependencies {
    compile 'pl.droidsonroids.gif:android-gif-drawable:1.1.+'}
 

2. activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:gravity="center_horizontal"    >  <pl.droidsonroids.gif.GifImageView      android:id="@+id/test1"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      />  </LinearLayout>

3. MainActivity.java

public class MainActivity extends AppCompatActivity {

    GifImageView gifImageView;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);
        gifImageView = (GifImageView)findViewById(R.id.test1);        gifImageView.setImageResource(R.drawable.icons_animation);
    }
}

Friday, 16 September 2016

Shared Preferences : Getting and Setting Data


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="370dp"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin" >

    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/textView2"        android:layout_centerVertical="true"        android:onClick="Save"        android:text="Save" />

    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:onClick="Get"        android:text="Get" />

    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/etEmail"        android:layout_alignBottom="@+id/etEmail"        android:layout_alignLeft="@+id/textView1"        android:text="Email"        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignRight="@+id/etEmail"        android:layout_centerVertical="true"        android:layout_marginRight="21dp"        android:onClick="clear"        android:text="Clear" />

    <EditText        android:id="@+id/etEmail"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_below="@+id/textView1"        android:layout_marginTop="36dp"        android:ems="10"        android:inputType="textEmailAddress" />

    <EditText        android:id="@+id/etName"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_above="@+id/etEmail"        android:layout_alignLeft="@+id/etEmail"        android:ems="10"        android:inputType="text" />

    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:layout_marginLeft="17dp"        android:layout_marginTop="39dp"        android:text="Name"        android:textAppearance="?android:attr/textAppearanceMedium" />


</RelativeLayout>




MainActivity.java





import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
    SharedPreferences sharedpreferences;
    TextView name;
    TextView email;
    public static final String mypreference = "mypref";
    public static final String Name = "nameKey";
    public static final String Email = "emailKey";

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = (TextView) findViewById(R.id.etName);
        email = (TextView) findViewById(R.id.etEmail);
        sharedpreferences = getSharedPreferences(mypreference,
                Context.MODE_PRIVATE);
        if (sharedpreferences.contains(Name)) {
            name.setText(sharedpreferences.getString(Name, ""));
        }
        if (sharedpreferences.contains(Email)) {
            email.setText(sharedpreferences.getString(Email, ""));

        }

    }

    public void Save(View view) {
        String n = name.getText().toString();
        String e = email.getText().toString();
        Editor editor = sharedpreferences.edit();
        editor.putString(Name, n);
        editor.putString(Email, e);
        editor.commit();
    }

    public void clear(View view) {
        name = (TextView) findViewById(R.id.etName);
        email = (TextView) findViewById(R.id.etEmail);
        name.setText("");
        email.setText("");

    }

    public void Get(View view) {
        name = (TextView) findViewById(R.id.etName);
        email = (TextView) findViewById(R.id.etEmail);
        sharedpreferences = getSharedPreferences(mypreference,
                Context.MODE_PRIVATE);

        if (sharedpreferences.contains(Name)) {
            name.setText(sharedpreferences.getString(Name, ""));
        }
        if (sharedpreferences.contains(Email)) {
            email.setText(sharedpreferences.getString(Email, ""));

        }
    }

}

Friday, 9 September 2016

add rows with buttons dynamically into table layout to have equal width




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: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.example.divakar.dynamicbuttoningrid.MainActivity">


    <TableLayout        android:id="@+id/mTlayout"        android:layout_width="match_parent"        android:layout_height="wrap_content"         />
</RelativeLayout>



MainActivity.java



import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;

public class MainActivity extends AppCompatActivity {

    private TableLayout table = null;

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

        TableLayout displayLevels = (TableLayout) findViewById(R.id.mTlayout);
        final int screenWidth = dpToPx(getResources().getConfiguration().screenWidthDp);
        int i = 0;
        int j = 0;
        int levelNum = 8;
        TableRow row = new TableRow(this);
        TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
        row.setLayoutParams(lp);
        row.setGravity(Gravity.CENTER);
        int width = (screenWidth - (levelNum) * 4) / levelNum;//4: I don't remember name of it. It is default value.4dp or 4px... I don't remember exactly
        displayLevels.addView(row);
        while (i < levelNum) {
            Button iBtn = new Button(this);
            iBtn.setGravity(Gravity.CENTER_HORIZONTAL);
            iBtn.setMinimumWidth(100);//I set 100px for minimunWidth.            iBtn.setWidth(width);
            iBtn.setText(Integer.toString(i + 1));
            iBtn.setId(i + 1);
            //iBtn.setOnClickListener(btnclick);            row.addView(iBtn, i);
            i++;
        }
    }
    private int dpToPx(int dp) {
        return (int) (dp * getResources().getDisplayMetrics().density + 0.5f);
    }
}

Android adding dynamic row to table




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: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.example.divakar.dynamicbuttoningrid.MainActivity">

    <Button        android:id="@+id/additembutton"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Add row to table"         />
    <TableLayout        android:id="@+id/mTlayout"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/additembutton"         />
</RelativeLayout>


Mainactivity.java



import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    private TableLayout table = null;
    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        table = (TableLayout)findViewById(R.id.mTlayout);

        Button additembutton = (Button) findViewById(R.id.additembutton);
        additembutton.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {

                update();
            }
        });

    }

    private void update()
    {
        TableRow tr = new TableRow(this);
        tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

        // adding text        TextView tv = new TextView(this);
        tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        tv.setText("New Entry");
        tv.setBackgroundColor(Color.parseColor("#12e7f4"));
        tr.addView(tv);

        // adding button        Button button = new Button(this);
        button.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        button.setText("New Button");
        tr.addView(button);

        table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));


    }}

Android Dynamic Button adding to Table




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: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.example.divakar.dynamicbuttoningrid.MainActivity">

    <TableLayout        android:id="@+id/mTlayout"        android:layout_width="match_parent"        android:layout_height="wrap_content"         />
</RelativeLayout>


MainActivity.java



import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    TableLayout mTlayout;
    TableRow tr;
    String[] mTextofButton = { "D", "E", "I", "J", "L", "M", "G", "R", "N",
            "T", "H", "P", "K", "Y", "V" };

    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTlayout = (TableLayout) findViewById(R.id.mTlayout);

        int i = 0;
        while (i < mTextofButton.length) {
            if (i % 2 == 0) {
                tr = new TableRow(this);
                mTlayout.addView(tr);
            }
            Button btn = new Button(this);
            btn.setText(mTextofButton[i]);
            btn.setId(i);
            btn.setOnClickListener(new View.OnClickListener() {

                @Override                public void onClick(View v) {
                    // TODO Auto-generated method stub                    System.out.println("v.getid is:- " + v.getId());
                    Toast.makeText(getApplicationContext(),"v.getid is:- " + v.getId(),Toast.LENGTH_LONG).show();
                }
            });
            tr.addView(btn);
            i++;
        }
    }
}