Thursday, February 21, 2013

Android Review



1/ Create android app:
   - On eclipse go to File->New->Android Application project
   - Fill Application name "HelloWorld", Project Name, and Package Name then choose Build SDK version ->Next in order to Finish.

2/ To configure android on eclipse:
   - On eclipse go to Help ->Install New Software
   - Add new repository and android web site->Install 
   - After finish installation go to Window->Preferences->Android then browse to android sdk path
   - Click on icon AVD (Android Virtual Device) Manager -> Add new Device
   - Fill Name, Target, and Size-> Create AVD

3/ To use services on android:
  - open file AndroidManifest.xml
  - Click on Permission tab->Add ->Uses permission
 - Choose service name->Save.

or we can type:
 e.g: to use Internet
<use-permission android:name="android.permission.INTERNET" />

4/ Input data and retrieve with putExtras
 - input value 
      Intent in;
      in = new Intent(this, Activity2.class);
      in.putExtra("name", "bunthan"); 
      in.putExtra("age", "23");
      startActivity(in);


- to retrieve data in Activity2.java
 
        Bundle extras = getIntent().getExtras();
        if(extras == null) {
            return;
        }
        String name = extras.getString("name");
        int age = extras.getInt("age");


5/ use click
     ->android:onClick
     -in the xml file add android:onClick="showMe"
     -in the java file type 
     public void showMe(View v) {
          Toast.makeText(this, "This event clicked from XML.", Toast.LENGTH_SHORT).show();
     }      

   
   -> Anonymous class
     Button btnHelp = (Button) findViewById(R.id.btn_help);
     btnHelp.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent i = new Intent(Activity1.this, Activity2.class);  
                startActivity(i);
            }
        });     


6/ Database
    declare global variable: 
  private SQLiteDatabase db; 
  private String db_name = "person";


    -> Create database or open if exists
    private void openDatabase(String dbname){
        db = openOrCreateDatabase(dbname, MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS tbl_person(name VARCHAR, phone VARCHAR, email VARCHAR);");
    }


   ->Close Database
    private void closeDatabase(){
        db.close();
    }


 ->Insert new record
     private void insert(String name, String phone, String email){
        db.execSQL("INSERT INTO tbl_person VALUES('" + name + "', '" + phone + "', '" + email + "');");
    }

    
->Delete record
    private void delete(String name){
        db.execSQL("DELETE FROM tbl_person WHERE name='" + name + "';");
    }
  

->Update record  
    private void update(String name){
        db.execSQL("UPDATE tbl_person SET name='" + name + "';");
    }



-> retrieve all data containing name with String Search
   private void select(String search) {
        Cursor result = db.rawQuery("SELECT * FROM tbl_person WHERE name LIKE '%" + search + "%'", null);
        result.moveToFirst();
        
        while(!result.isAfterLast()){
            String name = result.getString(0);
            String phone = result.getString(1);
            String email = result.getString(2);
            
            //do something here
            TextView tv = new TextView(this);
            tv.setText("name: "+ name + ", phone: " + phone + ", E-mail: " + email);
            
            LinearLayout linlay = (LinearLayout)findViewById(R.id.LinearLayout1);
            linlay.addView(tv);
            //end of do            
            result.moveToNext();
        }
    }


Author: Bunthan Prak

No comments:

Post a Comment