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>
<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;
}
}
Add widget class will looks as follows
public class SamplewidgtActivity extends AppWidgetProvider {
/** Called when the activity is first created. */
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
// TODO Auto-generated method stub
try {
SqliteDataBaseHelper sqlite = new SqliteDataBaseHelper(context);
// method to insert static values
sqlite.populateData();
//method to fetch values from database
fetch_TO_Widget(context, appWidgetManager);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void fetch_TO_Widget(Context context, AppWidgetManager Manager) {
SqliteDataBaseHelper sqlite = new SqliteDataBaseHelper(context);
JSONArray jsonArray = new JSONArray();
jsonArray = sqlite.fetchData();
try {
/** Called when the activity is first created. */
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
// TODO Auto-generated method stub
try {
SqliteDataBaseHelper sqlite = new SqliteDataBaseHelper(context);
// method to insert static values
sqlite.populateData();
//method to fetch values from database
fetch_TO_Widget(context, appWidgetManager);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void fetch_TO_Widget(Context context, AppWidgetManager Manager) {
SqliteDataBaseHelper sqlite = new SqliteDataBaseHelper(context);
JSONArray jsonArray = new JSONArray();
jsonArray = sqlite.fetchData();
try {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.main);
String name[] = new String[jsonArray.length()];
String Dob[] = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
name[i] = jsonArray.getJSONObject(i).getString(SqliteDataBaseHelper.NAME);
Dob[i] = jsonArray.getJSONObject(i).getString(SqliteDataBaseHelper.AGE);
remoteViews.setTextViewText(R.id.textViewdate, name[i]);
remoteViews.setTextViewText(R.id.textViewtime, Dob[i]);
}
Intent launchAppIntent = new Intent(context,
SamplewidgtActivity.class);
PendingIntent launchAppPendingIntent = PendingIntent.getActivity(
context, 0, launchAppIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.main_layot_id,launchAppPendingIntent);
ComponentName tutListWidget = new ComponentName(context,SamplewidgtActivity.class);
Manager.updateAppWidget(tutListWidget, remoteViews);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
How to run the application
-- Right click on the app
--Go To Run As
--Select Android Application
-- Note: If you r not finding any exception on logcat
--You can find your application by long clicking on the home screen you will find these things
--select widgets so that you will get
--select your app name so that you can get your widget on the screenimage19 |
No comments:
Post a Comment