Tuesday, 17 July 2012

Widget  

Creating a sample Widget and getting update values from Sqlite 

--Create Application,
--Create xml for widget (newwidget.xml)
--Crete a layout which will become content view to widget (main.xml)

--newwidget.xml  

ppwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/main"
    android:minHeight="200dp"
    android:minWidth="200dip"
    android:updatePeriodMillis="86400000"

 

 --main.xml 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layot_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/shape"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="13dp"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Employee"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#E3170D" />

        <TextView
            android:id="@+id/textViewdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#FFFFFF" />

        <TextView
            android:id="@+id/textViewtime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#FFFFFF" />
    </LinearLayout>

</LinearLayout>

graphical layout 

image 18
 

SqliteHelper class for widget is 

 public class SqliteDataBaseHelper extends SQLiteOpenHelper {

    static final String DATABASENAME = "DATABASENAME";
    static final String EMP_TABLE = "EMP_TABLE";
    static final String NAME = "NAME";
    static final String EMP_ID = "EMP_ID";
    static final String AGE = "AGE";
    static final String GENDER = "GENDER";
    static final String OCCU = "OCCU";
    String Names[] = { "PRIYA", "SAIKUMAR", "KARNATE", "PINKU", "SAINATH" };
    String Dob[] = { "3rd,nov", "20th,Mar", "4th,nov", "5th,nov", "3rd,nov" };
    String Gender[] = { "MALE", "FEMALE", "MALE", "MALE", "Male" };
    String Occu[] = { "S/E", "H/M", "S/E", "M/E", "IPS" };
    public SqliteDataBaseHelper(Context context) {
        super(context, DATABASENAME, null, 1);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + EMP_TABLE + " (" + NAME + " TEXT, "
                + EMP_ID + " TEXT, " + AGE + " TEXT, " + GENDER + " TEXT, "
                + OCCU + " TEXT)");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + EMP_TABLE);
        onCreate(db);
    }
    public int populateData() {
        SQLiteDatabase db = null;
        int a = 0;
        for (int i = 0; i < Names.length; i++) {
            try {
                db = this.getWritableDatabase();
                ContentValues cv = new ContentValues();
                cv.put(NAME, Names[i]);
                cv.put(EMP_ID, "" + i);
                cv.put(AGE, Dob[i]);
                cv.put(GENDER, Gender[i]);
                cv.put(OCCU, Occu[i]);
                a = (int) db.insert(EMP_TABLE, null, cv);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                db.close();
            }
        }
        return a;
    }
    public JSONArray fetchData() {
        SQLiteDatabase db = null;
        Cursor cur = null;
        JSONArray jsonArray = new JSONArray();
        try {
            db = this.getReadableDatabase();
            String query = "SELECT * FROM " + EMP_TABLE;
            cur = db.rawQuery(query, null);
            while (cur.moveToNext()) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put(NAME, cur.getString(0));
                jsonObject.put(EMP_ID, cur.getString(1));
                jsonObject.put(AGE, cur.getString(2));
                jsonObject.put(GENDER, cur.getString(3));
                jsonObject.put(OCCU, cur.getString(4));
                jsonArray.put(jsonObject);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.close();
            cur.close();
        }
        return jsonArray;
    }
}

 



 

Thursday, 12 July 2012

Get Start With Android

Start with Android  
  •  This post consist of how to get start with android application 
  • Steps to configure Android SDK with IDE
  • Open Eclipse 
  • Go to Windows 
  • Selects Preferences  
  • Image 12
  • You will get preference Window there u need select android Android
  • image 13
  •  give the SDK location so that it will give you all the version which that SDK deals with
  • Apply
  •  say OK 
       Q: How to create a sample Android application 
        
       Steps to create a sample application 
  • go to file
  • select new 
  • select android project 
  • Note: if android project is not visible in the list go to others 
  • give project name 
  • select version
  • give package name 
  • and Give Activity name(java)
  • Minimum SDK (depends on version API level for example if the version is 2.3.3 then the api level is 10)
  • Finally the application will be look like as fallows
  • image 14

 Get Video Tutorial For Android Sample App click Here