Tuesday, April 29, 2014

Unable to get system library for project

You may not able to build your android project because android adt cannot find the system library for project.

And following compile errors may occur due to that problem.

The type java.lang.Object cannot be resolved.

To solve the problem open build path for the project, and in Android tab select different project build target.


Thursday, April 24, 2014

SQLite For Android Source Codes

public class SQLiteHelper extends SQLiteOpenHelper {
 private static final String DATABASE_NAME = "my_database.db";
 // TOGGLE THIS NUMBER FOR UPDATING TABLES AND DATABASE

 private static final int DATABASE_VERSION = 1;
 // NAME OF TABLE YOU WISH TO CREATE
 public static final String TABLE_NAME = "my_table";
 // SOME SAMPLE FIELDS
 public static final String UID = "_id";
 public static final String NAME = "name";
 
 SQLiteHelper(Context context) {
  super(context, DATABASE_NAME, null, DATABASE_VERSION);
 }
 @Override
 public void onCreate(SQLiteDatabase db) {
  db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + UID + "
  INTEGER PRIMARY KEY AUTOINCREMENT," + NAME
  + " VARCHAR(255));");
 }
 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion,
   int newVersion) {
  Log.w("LOG_TAG", "Upgrading database from version " +
   oldVersion + " to " + newVersion + ", which will destroy all old data");
  // KILL PREVIOUS TABLE IF UPGRADED
  db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
  // CREATE NEW INSTANCE OF TABLE
  onCreate(db);
 }
}


Implementing the database helper class.

public class SQLiteExample extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  // INIT OUR SQLITE HELPER
  SQLiteHelper sqh = new SQLiteHelper(this);
  // RETRIEVE A READABLE AND WRITEABLE DATABASE
  SQLiteDatabase sqdb = sqh.getWritableDatabase();
  
  
  // METHOD #1: INSERT USING CONTENTVALUE CLASS
  ContentValues cv = new ContentValues();
  cv.put(SQLiteHelper.NAME, "jason wei");
  // CALL INSERT METHOD
  sqdb.insert(SQLiteHelper.TABLE_NAME, SQLiteHelper.NAME, cv);
  
  
  // METHOD #2: INSERT USING SQL QUERY
  String insertQuery = "INSERT INTO " +
  SQLiteHelper.TABLE_NAME + " (" + SQLiteHelper.NAME + ") VALUES ('jwei')";
  sqdb.execSQL(insertQuery);

  // METHOD #1: QUERY USING WRAPPER METHOD
  Cursor c = sqdb.query(SQLiteHelper.TABLE_NAME,
   new String[] { SQLiteHelper.UID, SQLiteHelper.NAME },
    null, null, null, null, null);
  while (c.moveToNext()) {
  // GET COLUMN INDICES + VALUES OF THOSE COLUMNS
  int id = c.getInt(c.getColumnIndex(SQLiteHelper.UID));
  String name = c.getString(c.getColumnIndex(SQLiteHelper.NAME));
  Log.i("LOG_TAG", "ROW " + id + " HAS NAME " + name);
  }
  c.close();

  // METHOD #2: QUERY USING SQL SELECT QUERY
  String query = "SELECT " + SQLiteHelper.UID + ", " +
   SQLiteHelper.NAME + " FROM " + SQLiteHelper.TABLE_NAME;
  Cursor c2 = sqdb.rawQuery(query, null);
  while (c2.moveToNext()) {
  int id = c2.getInt(c2.getColumnIndex(SQLiteHelper.UID));
  String name = c2.getString(c2.getColumnIndex(SQLiteHelper.NAME));
  Log.i("LOG_TAG", "ROW " + id + " HAS NAME " + name);
  }
  c2.close();
  
  // CLOSE DATABASE CONNECTIONS
  sqdb.close();
  sqh.close();
 }
}

Monday, February 17, 2014

[Android] Make android button transparent.

You can make android button transparent by inserting following code in layout xml file.

android:background="@android:color/transparent"

For example, I used it in one of my button as following.

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Who is computer Geek?"
        android:background="@android:color/transparent" />

Sunday, February 16, 2014

CSS Basics

Making bold the whole paragraph.

<p style="font-weight: bold;">This is a bold paragraph.</p>

Making a word italic.

This is the <span style="font-style:italic;">greatest</span> web page I’ve made yet.

Using an internal style sheet.

<!doctype html>
<html>
<head>
<title>Using an internal style sheet.</title>
<style type=”text/css”>
div {
      font-weight: bold;
}
span {
      font-style: italic;
}
</style>
</head>
<body>

            <div>This is my web page.</div>
</body>
</html>

Using an external style sheet.

<link rel="stylesheet" type="text/css" href="path_to_style_sheet/style.css">

HTML Basics

Basic HTML Page


<!doctype html>
<html>
    <head>
           <title>Basic HTML Web Page Template</title>
    </head>
    <body>
           <div>Web Page Content</div>
    </body>
</html>

Creating unordered list.

<ul>
       <li>Pine</li>
       <li>Oak</li>
       <li>Elm</li>

</ul>

Creating ordered list.

<ol>
       <li>Pine</li>
       <li>Oak</li>
       <li>Elm</li>

</ol>

Creating a table.

<table>
    <tr>
          <th>Airport Code</th>
          <th>Common Name/City</th>

    </tr>
    <tr>
          <td>CWA</td>
          <td>Central Wisconsin Airport</td>
    </tr>
    <tr>
          <td>ORD</td>
          <td>Chicago O’Hare</td>
    </tr>
    <tr>
          <td>LHR</td>
          <td>London Heathrow</td>
    </tr>
</table>

Linking to another page.

<p>Link to my <a href="http://37.139.31.138/">server home page.</a></p>   

Adding Images to webpage.

<img src="path_to_image.png" alt="image description">


Friday, January 17, 2014

Threading and Asynchronous Processing In Android OS - Part 1

Some tasks in application development require threading and asynchronous processing due to complexity of the task. For example networking, complex calculations, querying data over the network, parsing data, accessing remote database require threading. Unless Android Operating system decide that application is not responding and throw Application Not Responding (ANR) event.

There are three ways to manage complex and time consuming processes. They are,
  • By using AsynTask helper class.
  • By using standard Thread class.
  • Or using the Loader class.
AsyncTask Helper Class

Create subclass of AsyncTask and implement the appropriate callback method from below list.

  • onPreExecute() - This method runs on the UI thread before beginning of background process.
  • doInBackground() - This method runs where real task is taken place. For example if you want to perform complex task, you can do inside this method.
  • publishProgress() - This method called from the doInBackground() method. This will inform UI thread about the background process progress. This method sends information to the UI process.
  • onProgressUpdate() - This method runs on the UI thread whenever the doInBackground() method calls publishProgress(). Use this method to update the progressBar.
  • onPostExecute() - Runs on the UI thread after doInBackground() method completes its operation.
Sample class,

public class SimpleAsyncActivity extends Activity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.main);
            CountingTask tsk = new CountingTask();
            tsk.execute();
       }

       private class CountingTask extends AsyncTask<Void, Integer, Integer> {
             CountingTask() {}
            @Override
            protected Integer doInBackground(Void... unused) {
                    int i = 0;                    while (i < 100) {
                            SystemClock.sleep(250);
                            i++;
                            if (i % 5 == 0) {
                                   // update UI with progress every 5%
                                   publishProgress(i);
                            }
                   }
                   return i;
          }

          protected void onProgressUpdate(Integer... progress) {
                   TextView tv = (TextView) findViewById(R.id.counter);
                    tv.setText(progress[0] + "% Complete!");
          }

          protected void onPostExecute(Integer result) {
                   TextView tv = (TextView) findViewById(R.id.counter);
                    tv.setText("Count Complete! Counted to " + result.toString());
           }
     }
}

Sunday, January 5, 2014

[Android] Display Yes/No Alert box using java.

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Do this action");
builder.setMessage("do you want confirm this action?");

builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int which) {
        // Do do my action here

        dialog.dismiss();
    }

});

builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // I do not need any action here you might
        dialog.dismiss();
    }
});

AlertDialog alert = builder.create();
alert.show();

In the following line, remember to specify your class.
AlertDialog.Builder builder = new AlertDialog.Builder(this);

For example it should change like this.
AlertDialog.Builder builder = new AlertDialog.Builder(ClassName.this);