Pages

Thursday, 20 December 2012

open calender to add event from android application


here are two ways to pass the start time and end time  values:::

long startTime = System.currentTimeMillis() -  24*1000 * 60 * 60;
  long endTime = System.currentTimeMillis() + 48*1000 * 60 * 60 * 2;
              (or)

Date date=(new SimpleDateFormat("MM/dd/yyyy hh:mm aa")).parse("12/23/2012 07:00 am");
                                      long startTime=date.getTime();
       Date date2=(new SimpleDateFormat("MM/dd/yyyy hh:mm aa")).parse("12/22/2012 08:30 am");
                                       long endTime=date2.getTime();



Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("title", "Some title");
intent.putExtra("description", "Some description");
intent.putExtra("beginTime", startTime);
intent.putExtra("endTime", endTime);
startActivity(intent);

Monday, 25 June 2012

To get the android device dimentions


public class DeviceDimentions {

Context context;
Display display;

public DeviceDimentions(Context context) {
this.context = context;
display = ((Activity) context).getWindowManager().getDefaultDisplay();
}

public int getWidth() {
return display.getWidth();
}

public int getHeight() {
return display.getHeight();
}
}

Tuesday, 12 June 2012

Dynamic radio buttons and checkbox sample code


LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(btn);

TextView view = new TextView(this);
view.setText("1. Select the correct statement in the following options :");
layout.addView(view);
int n = 5;
for (int i = 0; i < n; i++) {
  CheckBox checkedTextView=new CheckBox(this);
checkedTextView.setText(String.valueOf(i));
checkedTextView.setId(i);
checkedTextView.setOnCheckedChangeListener(this);
            layout.addView(checkedTextView);
}

LinearLayout layout2 = (LinearLayout) findViewById(R.id.ll);
layout2.addView(layout);
TextView view1 = new TextView(this);
view1.setText("2. Select one correct statement in the following options :");
layout.addView(view1);

RadioButton[] radioButtons=new RadioButton[5];
RadioGroup group=new RadioGroup(this);
group.setOrientation(RadioGroup.HORIZONTAL);
for (int i = 0; i < n; i++) {
 
radioButtons[i]=new RadioButton(this);
group.addView(radioButtons[i]);
radioButtons[i].setText(String.valueOf(i));
radioButtons[i].setId(i);
}
   layout.addView(group);
   group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

System.out.println("the checked radio button id is :"+checkedId);

}
});

}

@Override
public void onCheckedChanged(CompoundButton cb,boolean isChecked) {

Toast.makeText(getApplicationContext(),String.valueOf(cb.getId()) , Toast.LENGTH_LONG).show();
}

Thursday, 7 June 2012

Date comparision


DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");

Date d1 = df.parse("2012-01-24 12:30:00 PM");
Date d2 = df.parse("2012-01-24 02:30:00 PM");

int hoursDifference = (int)((d2.getTime() - d1.getTime()) / 3600000L);
System.out.println("Difference in hours: " + hoursDifference);

Tuesday, 29 May 2012

getting the numeric with - . from a string


//getting the only numeric values with . -
String strs = priceString.replaceAll("[^\\d-.]", "");
example: if priceString is Rs 25.73 L - 27.75 L
it returns 25.73-27.75

sqlite Database util class


public class DatabaseUtil {

private static final String TAG = "DatabaseUtil";

/**
* Database Name
*/
private static final String DATABASE_NAME = "sales_database";

/**
* Database Version
*/
private static final int DATABASE_VERSION = 1;

/**
* Table Name
*/
private static final String DATABASE_TABLE = "SalesTb";

/**
* Table columns
*/
public static final String ROWID = "_id";
public static final String SNAME = "salesman_name";
public static final String CNAME = "customer_name";
public static final String CPHONENO = "phone_numebr";
public static final String CARMODEL = "car_model";

/**
* Database creation sql statement
*/
private static final String CREATE_SALES_TABLE = "create table  "
+ DATABASE_TABLE + " (" + ROWID
+ " integer primary key autoincrement, " + SNAME
+ " text not null, " + CNAME + " text not null, " + CPHONENO
+ " text not null, " + CARMODEL + " text not null);";

/**
* Context
*/
private final Context mCtx;
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;

/**
* Inner private class. Database Helper class for creating and updating
* database.
*/

private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

/**
* onCreate method is called for the 1st time when database doesn't
* exists.
*/
@Override
public void onCreate(SQLiteDatabase db) {
Log.i(TAG, "Creating DataBase: " + CREATE_SALES_TABLE);
db.execSQL(CREATE_SALES_TABLE);
}

/**
* onUpgrade method is called when database version changes.
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion);

db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}

/**
* Constructor - takes the context to allow the database to be
* opened/created
*
* @param ctx
*            the Context within which to work
*/
public DatabaseUtil(Context ctx) {
this.mCtx = ctx;
}

/**
* This method is used for creating/opening connection
*
* @return instance of DatabaseUtil
* @throws SQLException
*/
public DatabaseUtil open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}

/**
* This method is used for closing the connection.
*/
public void close() {
mDbHelper.close();
}

/**
* This method is used to create/insert new sales record .
*/
public long createsales(ContentValues initialValues) {
return mDb.insert(DATABASE_TABLE, null, initialValues);
}

/**
* This method will delete sales record.
*
* @param rowId
* @return boolean
*/
public boolean deletesales(long rowId) {
return mDb.delete(DATABASE_TABLE, ROWID + "=" + rowId, null) > 0;
}

/**
* This method will return Cursor holding all the Sales records.
*
* @return Cursor
*/
public Cursor fetchAllsales() {
return mDb.query(DATABASE_TABLE, new String[] { ROWID, SNAME, CNAME,
CPHONENO, CARMODEL }, null, null, null, null, null);
}

/**
* This method will return Cursor holding the specific Sales person record.
* @return Cursor
*/
public Cursor fetchsales(String name) throws SQLException {
Cursor mCursor = mDb.query(DATABASE_TABLE, new String[] { ROWID,
SNAME, CNAME, CPHONENO, CARMODEL },SNAME +"=? ", new String[]{name},
null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}

}

Monday, 28 May 2012

ListView using CursorAdapter


listView = (ListView) findViewById(R.id.reviewList);
DatabaseUtil util = new DatabaseUtil(getApplicationContext());
util.open();
Cursor cursor = util.fetchAllsales();
String columns[] = { DatabaseUtil.SNAME, DatabaseUtil.CNAME,
DatabaseUtil.CPHONENO, DatabaseUtil.CARMODEL };
int to[] = { R.id.salemannameView, R.id.cusomernameview,
R.id.phonenumberview, R.id.carmodelnameview };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.reviewrow, cursor, columns, to);
listView.setAdapter(adapter);
util.close();