Wednesday, July 18, 2012

How to import webpages in Android?

HtmlImport2Activity.java

package html.import2;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class HtmlImport2Activity extends Activity {
    /** Called when the activity is first created. */
       final Activity activity = this;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     //   setContentView(R.layout.main);
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        WebView webview = new WebView(this);
        setContentView(webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
        activity.setProgress(progress * 1000);
        }
      });

webview.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Toast.makeText(activity, "Application has Stoped Working..! " + description, Toast.LENGTH_SHORT).show();
}
});
        webview.loadUrl("http://www.Google.com/");
    }

}

Tuesday, July 17, 2012

Simple JSON Example

After Adding GSON to your project, you can use it as follows.

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

JSON_structure.java 
package beanmatrix.JSON;
public class JSON_structure {

              public String name;
              public String date;
              public String[] message;
}


JSONexampleActivity.java
package beanmatrix.JSON;
import android.app.Activity;
import com.google.gson.Gson;
import android.os.Bundle;
import android.widget.TextView;


public class JSONexampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
              /* Inflate TextView from the layout */
              TextView text = new TextView (this);
              /* JSON data considered as an example. Generally this data is obtained
              from a web service.*/
              String json = " {" + " \"name\": \"BeanMatrix\", " + " \"message\": [\"My JSON Example\",\"Using GSON For Parsing\"]," + " \"date\": \"17-7-2012\" "+ "}";
              Gson gson = new Gson();
              JSON_structure obj= gson.fromJson(json, JSON_structure.class);
              text.setText("Name: "+ obj.name +"\n\n");
              setContentView(text);
              text.append("Date: "+ obj.date +"\n\n");
              for(int i=0;i<obj.message.length;i++)
              {
                     text.append("Message: "+ obj.message[i] +"\n\n");
              }
              }
              catch(Exception ex){ex.printStackTrace();}
              }
}




Monday, July 16, 2012

Downloading and using external Libraries for JSON

There are many libraries available to parse JSON, be carefull while choosing them. GSON is commonly used because they are quicker than the android classes.

1. Download GSON library from
http://code.google.com/p/google-gson/

2. Now add GSON library to your project, follow the given step.

  1. Right click on your project and select properties. Select Java Build Path.Then select Libraries tab.

             2. Now select the option Add external JARs at the right corner.

            3. Browse the path up to downloaded file and press ok.

The External JSON library is ready to use in project.

Sunday, July 1, 2012

How Do i Work With Android DataBase - Select / Insert / Update / Delete

BeanMatrixDbActivity.java

package beanmatrix.db;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class BeanMatrixDbActivity extends Activity {
       private SQLiteAdapter SQLiteAdapter1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ListView listContent = (ListView)findViewById(R.id.contentlist);         
        SQLiteAdapter1 = new SQLiteAdapter(this);     
        SQLiteAdapter1.openToWrite();     
        SQLiteAdapter1.deleteAll();     
        SQLiteAdapter1.insert("Apple");     
        SQLiteAdapter1.insert("Banana");     
        SQLiteAdapter1.insert("Watermalon");
        SQLiteAdapter1.insert("Mango");
        SQLiteAdapter1.close();           
        SQLiteAdapter1 = new SQLiteAdapter(this);     
        SQLiteAdapter1.openToRead();     
        Cursor cursor = SQLiteAdapter1.queueAll();     
        startManagingCursor(cursor);     
        String[] from = new String[]{SQLiteAdapter.FRUIT_NAME};     
        int[] to = new int[]{R.id.text};     
        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.main, cursor, from, to);     
        listContent.setAdapter(cursorAdapter);         
        SQLiteAdapter1.close();
    }
}
SQLiteAdapter.java

package beanmatrix.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class SQLiteAdapter {
       public static final String DATABASE_NAME = "DATABASE1";
       public static final String TABLE = "FRUITS";
       public static final int MYDATABASE_VERSION = 1;
       public static final String ID = "_id";
       public static final String FRUIT_NAME = "Content";
       private static final String SCRIPT_CREATE_DATABASE ="create table " + TABLE + " ("+ ID + " integer primary key autoincrement, "+ FRUIT_NAME + " text not null);";
       private SQLiteHelper sqLiteHelper;
       private SQLiteDatabase sqLiteDatabase;
       private Context context;
       public SQLiteAdapter(Context c){context = c;}
       public SQLiteAdapter openToRead() throws android.database.SQLException
       {
              sqLiteHelper = new SQLiteHelper(context, DATABASE_NAME, null, MYDATABASE_VERSION);
              sqLiteDatabase = sqLiteHelper.getReadableDatabase();
              return this;
              }
       public SQLiteAdapter openToWrite() throws android.database.SQLException
       {
              sqLiteHelper = new SQLiteHelper(context, DATABASE_NAME, null, MYDATABASE_VERSION);
              sqLiteDatabase = sqLiteHelper.getWritableDatabase();
              return this;
              }
       public void close()
       {
              sqLiteHelper.close();
              }
       public long insert(String content)
       {
              ContentValues contentValues = new ContentValues();
              contentValues.put(FRUIT_NAME, content);
              return sqLiteDatabase.insert(TABLE, null, contentValues);
              }
       public int deleteAll()
       {
              return sqLiteDatabase.delete(TABLE, null, null);
              }
       public Cursor queueAll()
       {
              String[] columns = new String[]{ID, FRUIT_NAME};
              Cursor cursor = sqLiteDatabase.query(TABLE, columns,  null, null, null, null, null);
              return cursor;
              }
       public class SQLiteHelper extends SQLiteOpenHelper
       {
              public SQLiteHelper(Context context, String name,  CursorFactory factory, int version)
              {
                     super(context, name, factory, version);
                     }
              @Override
              public void onCreate(SQLiteDatabase db)
              {
                     db.execSQL(SCRIPT_CREATE_DATABASE);
              }
     }
 }
             
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" >

   <ListView android:id="@+id/contentlist"
                           android:layout_width="fill_parent"
                           android:layout_height="fill_parent"/>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/text"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="10dip"/>
</LinearLayout>