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();

Wednesday, 23 May 2012

Sample Sax Handler

public class CarDetailsHandler extends DefaultHandler {

private Company company = null;
private Category category = null;
private Model model = null;
private ModelInfo modelInfo = null;
private EngineInfo engineInfo = null;
private BreakInfo breakInfo = null;
private StringBuffer buffer = null;
private List<Model> models = null;
private List<Category> categories = null;
private StandardFeatures standardFeatures;
private static CarDetailsHandler carDetailsHandler = null;

private CarDetailsHandler() {

}

public static CarDetailsHandler getInstance() {
if (carDetailsHandler == null) {
carDetailsHandler = new CarDetailsHandler();
}

return carDetailsHandler;
}

public Company getDetails() {

return company;
}

@Override
public void startDocument() throws SAXException {
company = new Company();
categories = new ArrayList<Category>();
}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
buffer = new StringBuffer();
System.out.println("now it's in start menthd");
if (localName.equalsIgnoreCase("category")) {
category = new Category();
models = new ArrayList<Model>();

} else if (localName.equalsIgnoreCase("model")) {
model = new Model();

} else if (localName.equalsIgnoreCase("info")) {
modelInfo = new ModelInfo();

} else if (localName.equalsIgnoreCase("engine")) {
engineInfo = new EngineInfo();

} else if (localName.equalsIgnoreCase("break")) {
breakInfo = new BreakInfo();

} else if (localName.equalsIgnoreCase("standardfeatures")) {
 standardFeatures = new StandardFeatures();

}

// Get the number of attribute
int length = attributes.getLength();

// Process each attribute
for (int i = 0; i < length; i++) {
// Get names and values for each attribute
String name = attributes.getQName(i);
String value = attributes.getValue(i);

if (localName.equalsIgnoreCase("company")
&& name.equalsIgnoreCase("name")) {
company.setName(value);

} else if (localName.equalsIgnoreCase("category")
&& name.equalsIgnoreCase("name")) {
category.setName(value);

} else if (localName.equalsIgnoreCase("model")
&& name.equalsIgnoreCase("name")) {
model.setName(value);

} else if (localName.equalsIgnoreCase("engine")) {
if (name.equalsIgnoreCase("type")) {
engineInfo.setEngineType(value);

} else if (name.equalsIgnoreCase("cc")) {
engineInfo.setEngineCC(value);

} else if (name.equalsIgnoreCase("fuelefficiency")) {
engineInfo.setFuelEfficiency(value);

}
} else if (localName.equalsIgnoreCase("break")) {
if (name.equalsIgnoreCase("front")) {
breakInfo.setFront(value);

} else if (name.equalsIgnoreCase("rear")) {
breakInfo.setRear(value);

}
}

}
}

@Override
public void characters(char[] ch, int start, int length)
throws SAXException {

if (buffer != null) {
buffer.append(ch, start, length);

}
}

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (localName.equalsIgnoreCase("company")) {
company.setCategories(categories);
} else if (localName.equalsIgnoreCase("category")) {
category.setModels(models);
models = null;
categories.add(category);
category = null;

} else if (localName.equalsIgnoreCase("model")) {
System.out.println("model going to end");
models.add(model);
model = null;

} else if (localName.equalsIgnoreCase("description")) {
category.setDescription(buffer.toString());
} else if (localName.equalsIgnoreCase("image")) {
model.setImage(buffer.toString());

} else if (localName.equalsIgnoreCase("price")) {
model.setPrice(buffer.toString());

} else if (localName.equalsIgnoreCase("info")) {
model.setModelInfo(modelInfo);
modelInfo = null;

} else if (localName.equalsIgnoreCase("engine")) {
modelInfo.setEngineInfo(engineInfo);
engineInfo = null;

} else if (localName.equalsIgnoreCase("transmission")) {
modelInfo.setTransmission(buffer.toString());

} else if (localName.equalsIgnoreCase("steering")) {
modelInfo.setSteering(buffer.toString());

} else if (localName.equalsIgnoreCase("doors")) {
modelInfo.setDoors(buffer.toString());

} else if (localName.equalsIgnoreCase("break")) {
modelInfo.setBreakesInfo(breakInfo);

} else if (localName.equalsIgnoreCase("highlights")) {
model.setHighlights(buffer.toString());
} else if (localName.equalsIgnoreCase("standardfeatures")) {
model.setStandardFeatures(standardFeatures);
standardFeatures = null;


} else if (localName.equalsIgnoreCase("exterior")) {
standardFeatures.setExterior(buffer.toString());
} else if (localName.equalsIgnoreCase("interior")) {
standardFeatures.setInterior(buffer.toString());
} else if (localName.equalsIgnoreCase("comfort")) {
standardFeatures.setComfort(buffer.toString());
} else if (localName.equalsIgnoreCase("safety")) {
standardFeatures.setSafety(buffer.toString());
}

}
}