Tuesday 15 December 2015

coustom-list-view-in-android-example

Custom List View In Android Example

Hi Guys,
I am going to create android custom list view in which I show text with image. This one is very simple android list view example.
Create Project:
Project Name: CoustomizeListView
packageName: com.apsmind.coustomizelistview

file : activity_main.xml ( Here we put list view on this file ).


    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    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" >

   
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
   


file: row.xml(create row.xml in res/layout/ folder ).




    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

   
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

       
            android:id="@+id/imageView1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

       
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

       


file: MainActivity.java ( Main Activity file created within src  folder)

package com.apsmind.coustomizelistview;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;

public class MainActivity extends Activity {
ListView lv;
String arr[]={"About"," Call ", " Carrer ", " course ", " Email "};
int imgarr[]={R.drawable.aboutus,R.drawable.call1,R.drawable.career,R.drawable.courses,R.drawable.email};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.listView1);
CoustomListAdapter clp = new CoustomListAdapter(MainActivity.this,R.layout.row,arr,imgarr);
lv.setAdapter(clp);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

file: CoustomListAdapter.java ( Adapter class create with in src folder)

package com.apsmind.coustomizelistview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class CoustomListAdapter extends ArrayAdapter {
Context c;
int layout;
String arr[];
int imgid[];
public CoustomListAdapter(Context mainActivity, int row, String[] arr,
int[] imgarr) {
// TODO Auto-generated constructor stub
super(mainActivity,row,arr);
c = mainActivity;
layout=row;
this.arr= arr;
imgid = imgarr;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater lif = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = lif.inflate(layout, null);
ImageView iv = (ImageView)convertView.findViewById(R.id.imageView1);
TextView tv = (TextView) convertView.findViewById(R.id.textView1);
Button btn = (Button)convertView.findViewById(R.id.button1);
iv.setImageResource(imgid[position]);
tv.setText(arr[position]);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(c, "hello", Toast.LENGTH_LONG).show();
}
});
return convertView;
}
}
To learn more interesting android mobile application development join apsmind technology pitampura delhi contact 9953535779.


Sunday 26 July 2015

Android Spy Camera Example using android service

Image Clicked in background using android camera service


hi friends,
Today i am going to taught how to create spy camera, basically if you want your smart phone camera clicked image in background with out using any fore ground graphics then you use this concept.
this idea you can use to create security application locker. This is also an example of android service that help to click picture in background.

Requirement to Create Spy Camera:-



1. we have to create an service that run camera application in background.
2.we have to give external storage write and  using camera permission in android manifest file.



Complete Code:
Android Activity code:




package com.apsmind.spycamera;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity1 extends Activity {


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

startService(new Intent(MainActivity.this, CameraService.class) );
 
 }

android Service Class : create a class name CameraService.


package com.apsmind.spycamera;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;

import android.os.IBinder;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

public class CameraService extends Service
{
 static int x=0;
      //Camera variables
      //a surface holder
      private SurfaceHolder sHolder;
      //a variable to control the camera
      private Camera mCamera;
      //the camera parameters
      private Parameters parameters;
      /** Called when the activity is first created. */
    @Override
    public void onCreate()
    {
        super.onCreate();
       
    }
    @Override
    public void onStart(Intent intent, int startId) {
      // TODO Auto-generated method stub
      super.onStart(intent, startId);
   
       mCamera = Camera.open();
       SurfaceView sv = new SurfaceView(getApplicationContext());
 

       try {
                  mCamera.setPreviewDisplay(sv.getHolder());
                  parameters = mCamera.getParameters();
                 
                   //set camera parameters
                 mCamera.setParameters(parameters);
                 mCamera.startPreview();
                 mCamera.takePicture(null, null, mCall);
           
            } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
     
     
       //Get a surface
         sHolder = sv.getHolder();
        //tells Android that this surface will have its data constantly replaced
         sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

   
   
    Camera.PictureCallback mCall = new Camera.PictureCallback()
    {
 
       public void onPictureTaken(byte[] data, Camera camera)
       {
             //decode the data obtained by the camera into a Bitmap
   
             FileOutputStream outStream = null;
                  try{
                   x++;
                      outStream = new FileOutputStream("/sdcard/Image"+x+".jpg");
                      outStream.write(data);
                      outStream.close();
                      mCamera.release();
                      Toast.makeText(getApplicationContext(), "picture clicked", Toast.LENGTH_LONG).show();
                  } catch (FileNotFoundException e){
                      Log.d("CAMERA", e.getMessage());
                  } catch (IOException e){
                      Log.d("CAMERA", e.getMessage());
                  }
   
       }
    };


      @Override
      public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
      }
}



Android Manifest File :




    package="com.apsmind.spycamera"
    android:versionCode="1"
    android:versionName="1.0" >

            android:minSdkVersion="8"
        android:targetSdkVersion="18" />
   
   
   

            android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
                    android:name="com.apsmind.spycamera.MainActivity"
            android:label="@string/app_name" >
           
               

               
           
       
       
   




To Learn Interesting and Live project Base Android Application Development join Apsmind Technology Pvt Ltd.

http://www.apsmind.com (Training)
http://www.apsmindtech.com  (Development)

Wednesday 4 February 2015

android-client-server-database-with-php

Android Application Development With PHP and MySql DataBase

Hi Reader, 
Welcome Back in my new Post with very exiting an useful topic in android application development, when we going to create an Web Base Android Application, or such application that  data base required to share with several user then we have to create our application on live server.

Today  i am going to explain android client server architecture.
In This Scenario our application store in PHONE ie is client and our database store on server, and client have to send a reqest to server  and on behalf of that request server return response.  if response is null then we not connetct to server or some internet issue.
   try{
 HttpClient httpclient = new DefaultHttpClient();
           HttpPost httppost = new HttpPost("http://www.apsmind.com/foodcorner/fetchfeedback.php");
                
           
            HttpResponse response = httpclient.execute(httppost);
            if(response!=null)
            System.out.println("Connection created");
            HttpEntity entity = response.getEntity();
            InputStream isr = entity.getContent();
}
catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
    }
            
By writing these code you got your response in your InputStream Object. Now you have to convert in to text.
 //convert response to string
   try{
    InputStreamReader isre= new InputStreamReader(isr,"iso-8859-1");
           BufferedReader reader = new BufferedReader(isre,8);
           StringBuilder sb = new StringBuilder();
           String line = null;
           while ((line = reader.readLine()) != null) {
                   sb.append(line + "\n");
           }
           isr.close();
        String    result=sb.toString();
         //  tv.setText(result);
   }catch(Exception e){
           Log.e("log_tag", "Error converting result "+e.toString());
   }
By writing these code you got your response in your String object. But if your data in json format like this

[{"comment":"good","name":"ravikant","Rating":"3"},{"comment":"Nice app","name":"Akshu","Rating":"5"},{"comment":"nice","name":"Ravi","Rating":"5"},{"comment":"interesting","name":"amit","Rating":"5"},{"comment":"Awesum App","name":"Shobha","Rating":"5"}]

then you have to parse it.
try{
    String record1 = " ";
        JSONArray    jArray = new JSONArray(result);
        for(int i=0;i
        {
        JSONObject json = jArray.getJSONObject(i);
        record1 = record1+"\n" +" Name : "+json.getString("name")+
        "Rating: "+json.getInt("Rating")+"\n" +" Comment : "+json.getString("comment");
        tv.setText(record1);
        }
   }catch(JSONException e){
           Log.e("log_tag", "Error parsing data "+e.toString());
   }
    
    }

How To Create Complete Project:-

1. Install XAMPP Server if not install yet.

2.start apache and mysql.



3. click on Mysql Admin Button :

  1.        create an database name APSMINDTECHNOLOGY


   2.   create an table name courses with these field 

C_IDC_NameC_DurC_Fee
   3. then enter some record:


C_IDC_NameC_DurC_Fee
EditDelete1Java3 Month5000
EditDelete2Android3 Month7500
EditDelete3php++48500
EditDelete5Phonegap3 Month12000
EditDelete6Webdesigning3 Month8500
EditDelete7ccna3 Month9000
EditDelete8CLOUD3 Month10000
EditDelete9CLOUD3 Month10000
EditDelete10c12000
EditDelete11Java2.5 month5000
EditDelete15Oractle3month8000
EditDelete13Oractle3month8000
EditDelete14Oractle3month8000

4.create an php file and save within c://XAMPP/htdocs/project/fetch.php

$server= mysql_connect("localhost","root","") or die("could not connect");
$db = mysql_select_db("APSMINDTECHNOLOGY",$server); 
                         $result = mysql_query("select * from courses");
                        while($row= mysql_fetch_assoc($result))
                {
                             $output[]=$row;
                }
                         print(json_encode($output));
                         mysql_close($db);
5. Check your PhP code by this way : 
open web browser and open this url http://localhost/project/fetch.php
you got result like this:

[{"C_ID":"1","C_Name":"Java","C_Dur":"3 Month","C_Fee":"5000"},{"C_ID":"2","C_Name":"Android","C_Dur":"3 Month","C_Fee":"7500"},{"C_ID":"3","C_Name":"php++","C_Dur":"4","C_Fee":"8500"},{"C_ID":"5","C_Name":"Phonegap","C_Dur":"3 Month","C_Fee":"12000"},{"C_ID":"6","C_Name":"Webdesigning","C_Dur":"3 Month","C_Fee":"8500"},{"C_ID":"7","C_Name":"ccna","C_Dur":"3 Month","C_Fee":"9000"},{"C_ID":"8","C_Name":"CLOUD","C_Dur":"3 Month","C_Fee":"10000"},{"C_ID":"9","C_Name":"CLOUD","C_Dur":"3 Month","C_Fee":"10000"},{"C_ID":"10","C_Name":"c","C_Dur":"1","C_Fee":"2000"},{"C_ID":"11","C_Name":"Java","C_Dur":"2.5 month","C_Fee":"5000"},{"C_ID":"15","C_Name":"Oractle","C_Dur":"3month","C_Fee":"8000"},{"C_ID":"13","C_Name":"Oractle","C_Dur":"3month","C_Fee":"8000"},{"C_ID":"14","C_Name":"Oractle","C_Dur":"3month","C_Fee":"8000"}]

6. Now you have to create an Android Application name ExternalDataBaseTest
            i.   open MainActivity.java file

package com.example.externaldatabasetest;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;


import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView tv;
String record1 = " ";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView)findViewById(R.id.textView2);
       
   new Asy().execute();
    }
    class Asy extends AsyncTask
    {
    @Override
    protected Void doInBackground(Void... params) {
    // TODO Auto-generated method stub
    getData();
    return null;
    }
    @Override
    protected void onPostExecute(Void result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
    tv.setText(record1);
    }
    }
    public void getData()
    {
    String result = " ";
    InputStream isr = null;
    // To getting Data
    try
    {
     HttpClient httpclient = new DefaultHttpClient();
           HttpPost httppost = new HttpPost("http://10.0.2.2/androidtrainer.netne.net/courselist.php");
                
           
            HttpResponse response = httpclient.execute(httppost);
            if(response!=null)
            System.out.println("Connection created");
            HttpEntity entity = response.getEntity();
            isr = entity.getContent();
           

    }
    catch(Exception e)
    {
    Log.i("Error",e.toString());
    // tv.setText("Connection not created");
    }
     //convert response to string
   try{
    InputStreamReader isre= new InputStreamReader(isr,"iso-8859-1");
           BufferedReader reader = new BufferedReader(isre,8);
           StringBuilder sb = new StringBuilder();
           String line = null;
           while ((line = reader.readLine()) != null) {
                   sb.append(line + "\n");
           }
           isr.close();
           result=sb.toString();
           System.out.println(sb);
         //  tv.setText(result);
   }catch(Exception e){
           Log.e("log_tag", "Error converting result "+e.toString());
   }
  try{
   
        JSONArray    jArray = new JSONArray(result);
        for(int i=0;i
        {
        JSONObject json = jArray.getJSONObject(i);
        record1 = record1+"\n" +i+" Name : "+json.getString("C_Name")+
        " Fee: "+json.getString("C_Fee");
        // tv.setText(record1);
        }
        System.out.println(record1);
   }catch(JSONException e){
           Log.e("log_tag", "Error parsing data "+e.toString());
   }
    
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}


ii. one activity_main.xml file


    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

   
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

       
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

           
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge" />

           
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Large Text"
                android:textAppearance="?android:attr/textAppearanceLarge" />

       
   


iii. add internet permission in AndroidManifest.xml
uses-permission android:name="android.permission.INTERNET"
iv. 
Now you test your application in emulator or bluestack.






Today's Pageviews