Thursday 31 May 2012

Google Map API key genaration in ubuntu

To the generation of the Google Map API,we first find out where is our java files are stored in ubuntu.In most cases the java files should be in the File System.

Here giving commands to get the Google API using terminal.

:~$ cd /
:~$ cd user
:~$ cd lib
:~$ cd jvm
:~$ cd java-6-openjdk
:~$ cd bin
then we want to get the keystore.To get the android default debug keystore:

eclipse->windows->preferences->Android ->Build, then copy the  default debug keystore.

eg: /home/anna/.android/debug.keystore \

:~$ keytool -list -alias androiddebugkey \-keystore /home/anna/.android/debug.keystore \
> -storepass android -keypass android


Then we will get an MD5 encrypted key.Copy that key,example(36:EF:BD:0C:1B:7F:7B:06:F9:34:90:A7:8E:83:34:28).
Then take the following link: https://developers.google.com/android/maps-api-signup
Then agree the teams and conditions to generate key.



then copy the given example xml layout to your application.

<com.google.android.maps.MapView
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:apiKey="0jG4xjFXTdrDqmRbIPau0v1DZnjdpNQ4Lu07qqg"
                 />

SplashScreen

package com.ann;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.MotionEvent;
import android.widget.TextView;

public class splashSereen extends Activity {
    protected boolean _active = true;
    protected int _splashTime = 10000;
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
      
        SharedPreferences app_preferences =
                PreferenceManager.getDefaultSharedPreferences(this);
          
            // Get the value for the run counter
            int counter = app_preferences.getInt("counter", 0);
            if(counter==0)
            {
                 TextView txt=(TextView)findViewById(R.id.textView1);
               //  txt.setText(read());
               
                 // thread for displaying the SplashScreen
                 Thread splashTread = new Thread() {
                     @Override
                     public void run() {
                         try {
                             int waited = 0;
                             while(_active && (waited < _splashTime)) {
                                 sleep(100);
                                 if(_active) {
                                     waited += 100;
                                 }
                             }
                         } catch(InterruptedException e) {
                             // do nothing
                         } finally {
                             finish();
                             startActivity(new Intent("com.ann.MyBirthdayReminderActivity"));
                             stop();
                         }
                     }
                 };
                 splashTread.start();
            }
            else
            {
                Intent intent=new Intent(this,MyBirthdayReminderActivity.class);
                startActivity(intent);
            }
            SharedPreferences.Editor editor = app_preferences.edit();
            editor.putInt("counter", ++counter);
            editor.commit(); // Very important
    }
  
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            _active = false;
        }
        return true;
    }
  private String read()
  {
InputStream inputStream = getResources().openRawResource(R.raw.birth);
     
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
      
        int i;
     try {
      i = inputStream.read();
      while (i != -1)
         {
          byteArrayOutputStream.write(i);
          i = inputStream.read();
         }
         inputStream.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
  
        return byteArrayOutputStream.toString();
   }
}


Splash screen using Activity


package com.ann;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;

public class splash extends Activity {
    protected boolean _active = true;
    protected int _splashTime = 5000;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
       
        // thread for displaying the SplashScreen
        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while(_active && (waited < _splashTime)) {
                        sleep(100);
                        if(_active) {
                            waited += 100;
                        }
                    }
                } catch(InterruptedException e) {
                    // do nothing
                } finally {
                    finish();
                    startActivity(new Intent("com.ann.start"));
                    stop();
                }
            }
        };
        splashTread.start();
    }
   
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            _active = false;
        }
        return true;
    }
}

Monday 28 May 2012

Popup in android


Creating one window as PopupWindow to display the data

To create the popupwindow we want to create separate XML layout.
Here giving example 
popup.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:background="@android:color/background_light">
 <LinearLayout
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="vertical"
     android:layout_margin="1dp"
     android:background="@android:color/darker_gray">
     >
     <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:layout_margin="20dp">
      <TextView
          android:layout_width="fill_parent"
          android:textColor="#00FF00"
          android:textStyle="bold"
          android:layout_height="wrap_content"
          android:text="It's a PopupWindow" />
      <ImageView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_launcher" />
      <Button
          android:id="@+id/dismiss"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Dismiss" />
      </LinearLayout>
 </LinearLayout>
</LinearLayout>

 PopupActivity.java


package com.ann;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

public class PopupActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        final Button btnOpenPopup = (Button)findViewById(R.id.openpopup);
        btnOpenPopup.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    LayoutInflater layoutInflater
     = (LayoutInflater)getBaseContext()
      .getSystemService(LAYOUT_INFLATER_SERVICE); 
    View popupView = layoutInflater.inflate(R.layout.popoup, null); 
             final PopupWindow popupWindow = new PopupWindow(
               popupView,
               LayoutParams.WRAP_CONTENT, 
                     LayoutParams.WRAP_CONTENT); 
            
             Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
             btnDismiss.setOnClickListener(new Button.OnClickListener(){

     @Override
     public void onClick(View v) {
      // TODO Auto-generated method stub
      popupWindow.dismiss();
     }});
              
             popupWindow.showAsDropDown(btnOpenPopup, 50, 30);
        
   }});
    }
}

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <Button
        android:id="@+id/openpopup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Open Popup Window" />
   
</LinearLayout>










Friday 25 May 2012

Data Base Application(add,view,update,delete)

 DatabaseapplicationActivity.java


package com.databaseapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class DatabaseapplicationActivity extends Activity {
    /** Called when the activity is first created. */
    EditText name,age,qual,place;
    String name1,age1,qual1,place1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
       
        name=(EditText)findViewById(R.id.editText1);
        age=(EditText)findViewById(R.id.editText2);
        qual=(EditText)findViewById(R.id.editText3);
        place=(EditText)findViewById(R.id.editText4); 
     
       
    }
    public void submit(View v)
    {
        name1=name.getText().toString();
        age1=age.getText().toString();
        qual1=qual.getText().toString();
        place1=place.getText().toString();
        details d=new details();
        d.setName(name1);
        d.setAge(age1);
        d.setQual(qual1);
        d.setPlace(place1);
       
       
        databasehandler dbh=new databasehandler(this);
        dbh.insert(d);
        Toast.makeText(getApplicationContext(),"Details Submitted Successfully", Toast.LENGTH_LONG).show();
    }
    public void viewDetails(View v)
    {
        Intent i=new Intent(this,second.class);
        startActivity(i);
    }
   
public void editDetails(View v)
{
Intent k=new Intent(this,third.class);
startActivity(k);
}
public void deletedata(View v)
{
    Intent l=new Intent(this,fourth.class);
    startActivity(l);
}
}




Database Handler


package com.databaseapp;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

public class databasehandler  extends SQLiteOpenHelper{
   
    private static final String MYDB="mydb";
    private static final String MYTB="USER";
    public String deleted;
    SQLiteDatabase db;
    Context context;
   
    public databasehandler(Context context) {
        super(context,MYDB, null, 32);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("create table if not exists "+MYTB+" (id integer primary key autoincrement,name text,age text,qualification text,place text)");
        //db.close();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("drop table if exists "+MYTB);
        onCreate(db);
    }
public void insert(details d) {
    Log.v("Errror","here");
    SQLiteDatabase db=getWritableDatabase();
   
    ContentValues cv=new ContentValues();
    cv.put("name",d.getName());
    Log.v("name",d.getName());
    cv.put("age",d.getAge());
    Log.v("age",d.getAge());
    cv.put("qualification",d.getQual());
    Log.v("qualification",d.getQual());
    cv.put("place",d.getPlace());
    Log.v("place",d.getPlace());
   
    db.insert(MYTB,"id",cv);
    Log.v("data","inserted");
}

public ArrayList<String> resultdata() {
    ArrayList<String> results=new ArrayList<String>();
    SQLiteDatabase db = this.getReadableDatabase();
    try {   
        Cursor c=null;
         c = db.rawQuery("select * from "+MYTB, null);
       
        if (c!= null) {
        Log.v("cursor","notnull");
            if (c.moveToFirst());
            {
            do {
                    int cc=c.getCount();
                    Log.v("cursor count",""+cc);
                   
                    String Name = c.getString(c.getColumnIndex("name"));
                    Log.v("Name",Name);

                    String Age = c.getString(c.getColumnIndex("age"));
                    Log.v("Age",Age);
                   
                    String Qualification=c.getString(c.getColumnIndex("qualification"));
                    Log.v("Qualification",Qualification);
                   
                    String Place=c.getString(c.getColumnIndex("place"));
                    Log.v("Place",Place);
                   
                    String id=c.getString(c.getColumnIndex("id"));
                    Log.v("id",id);
                   
                    results.add( "\n Id: "+id+"\n Name: " +Name+"\n Age : " +Age+"\n Qualification : "+Qualification+"\n Place :"+Place);
                    //results.add("dfrty");
                    Log.v("Array",""+results);
                }
            while (c.moveToNext());
            }}
        else
        {
            Toast.makeText(context, "results empty",Toast.LENGTH_LONG).show();
            results.equals(null);
        }
    } catch (SQLiteException se) {
        Log.e(getClass().getSimpleName(),
                "Could not create or Open the database");
    }
    return results;
}


public Cursor update(String i){
    Cursor c;
    SQLiteDatabase db=this.getWritableDatabase();
    String[] columns={"name","age","qualification","place"};
    String selection="id=?";
    String[] selectionArgs={i};
    Log.v("selection",selection);
    Log.v("selectionargs",i);
    c=db.query(MYTB, columns, selection, selectionArgs, null, null, null);
    //db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy)
    return c;
   
   
}
public void updateDetails(details d,String id1) {
    //db.close();
    Log.v("db","notclosed");
    db=getWritableDatabase();
   
    ContentValues cv=new ContentValues();
    Log.v("update","query");
    cv.put("name",d.getName());
    Log.v("name",d.getName());
    cv.put("age",d.getAge());
    Log.v("age",d.getAge());
    cv.put("qualification",d.getQual());
    Log.v("qualification",d.getQual());
    cv.put("place",d.getPlace());
    Log.v("place",d.getPlace());
    String where="id=?";
    String [] whereArgs={id1};
    db.update(MYTB, cv, where,whereArgs);

}

public void deletedetails(String idd)
{
    Cursor c=null;
    Log.v("ivalue", ""+idd);
    SQLiteDatabase db=getWritableDatabase();
    deleted="false";
    String[] columns={"name","age","qualification","place"};
    String selection="id=?";
    String[] selectionArgs={idd};
    Log.v("selection",selection);
    Log.v("selectionargs",idd);
    c=db.query(MYTB, columns, selection, selectionArgs, null, null, null);
    //db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy)
    if(c!=null)
    {
        if( c.moveToFirst())
        {
            String where="id=?";
            String[] args={idd};
            Log.v("value",""+args);
            db.delete(MYTB, where, args);
            deleted="true";
                   
        }
        else
        {
            deleted="false";
        }
       
    }
    else
    {
        deleted="false";
    }
   
}

}
   
get and set
package com.databaseapp;

public class details {
    String name,age,qual,place;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getQual() {
        return qual;
    }

    public void setQual(String qual) {
        this.qual = qual;
    }

    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        this.place = place;
    }

}
Listview
package com.databaseapp;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class second extends Activity {
   
ListView list;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        list=(ListView)findViewById(R.id.listView1);

        databasehandler dbh=new databasehandler(getApplicationContext());
       
        ArrayList<String > result=new ArrayList<String>();
        result=dbh.resultdata();
        Log.v("reached",""+result);
       
        if (result.isEmpty()==true)
        //Log.v("after clear",""+result);
       
        {
            Toast.makeText(getApplicationContext(),"Database empty",Toast.LENGTH_LONG).show();
           
        }
        else
        {
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, result);
            list.setAdapter(adapter);
        }

}
}
update
package com.databaseapp;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class third  extends Activity{
    EditText t1,t2,t3,t4,t5;
    String name1,age1,qual1,place1,id1;
    SQLiteDatabase db;
    @Override
   
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.third);
        t1=(EditText)findViewById(R.id.id1);
        t2=(EditText)findViewById(R.id.txt1);
        t3=(EditText)findViewById(R.id.txt2);
        t4=(EditText)findViewById(R.id.txt3);
        t5=(EditText)findViewById(R.id.txt4);
    }
    public void editdetails(View v)
    {
        String id1=t1.getText().toString();
        databasehandler dbh=new databasehandler(this);
       
        Cursor c=null;
         c=dbh.update(id1);
       
        if(c!=null)
        {
            Log.v("db","null");
           
            Log.v("inside","cursor");
            if( c.moveToFirst())
            {
            String s=c.getString(c.getColumnIndex("name"));
            Log.v("name",s);
            String p=c.getString(c.getColumnIndex("age"));
            String q=c.getString(c.getColumnIndex("qualification"));
            String r=c.getString(c.getColumnIndex("place"));
            t2.setText(s);
            t3.setText(p);
            t4.setText(q);
            t5.setText(r);
            c.close();
            // dbh.db.close();
           
        }
        else
        {
            Toast.makeText(getApplicationContext(), "no data found", Toast.LENGTH_LONG).show();
        }
        }
        else
        {
            Toast.makeText(getApplicationContext(), "No data found", Toast.LENGTH_LONG).show();
        }   
    //db.close();   
    }
    public void updatedata(View v)
    {
        Log.v("ivide","ethi");
        databasehandler dbh=new databasehandler(this);
        name1=t2.getText().toString();
        Log.v("updatedname",name1);
        age1=t3.getText().toString();
        Log.v("updateage",age1);
        qual1=t4.getText().toString();
        Log.v("updatedqual",qual1);
        place1=t5.getText().toString();
        Log.v("updatedplace",place1);
        id1=t1.getText().toString();
        details d=new details();
        d.setName(name1);
        d.setAge(age1);
        d.setQual(qual1);
        d.setPlace(place1);
        Log.v("going to","update query");
        dbh.updateDetails(d,id1);
        Toast.makeText(getApplicationContext(),"Updated",Toast.LENGTH_LONG).show();
    }
    public void viewdata1(View v)
    {
        Intent i =new Intent(third.this,second.class);
        startActivity(i);
    }
}
delete
package com.databaseapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class fourth extends Activity{
    EditText t1;
    @Override
   
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fourth);
        t1=(EditText)findViewById(R.id.etxt1);
    }
    public void deleteDetails(View v)   
    {
        String idd=t1.getText().toString();
        databasehandler dbh=new databasehandler(this);
        dbh.deletedetails(idd);
        String s=dbh.deleted;
        if( s=="true")
        {
        Toast.makeText(getApplicationContext(), "deleted", Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(), "no data found", Toast.LENGTH_LONG).show();
        }
    }
   

}