Posts

Showing posts from February, 2010

Creating a custom widget Calendar

In android, there is DatePicker and DatePickerDialog. But these two have lot of restrictions. So I decided to create a Calendar widget for one of my Android application.


The features of this calendar are,
- Set the startDate and endDate.
- Set a different color for dates that is coming outside the range of startDate and endDate.


Create two Spinners, one for Month and one for year. Create a TableLayout and add the TextView array, you can use buttons too, its up to you.

Creating spinner and adding values to it is straight forward. For year, from startDate and endDate, create the String array for year. MonthSpinner will be updated when you click on the startYear and endYear.

For creating spinner I created a function as,


public Spinner createSpinner(String[] results) {
Spinner spinner = new Spinner(ctContext);
ArrayAdapter adapter = new ArrayAdapter(ctContext,
android.R.layout.simple_spinner_item, results);
spinner.setAdapter(adapter);
return spinner;
}

Creating the spinner and add values to i…

Animation through Bitmap In Android

In Android, we can do frame by frame animation on Bitmaps. For this, first convert the bitmap into drawable objects.

Create a class called "BitmapDrawable" which extends Drawable and pass the bitmap to its constructor. In the draw(), draw the bitmap.


class BitmapDrawable extends Drawable {
private Bitmap mBitmap;

BitmapDrawable(Bitmap b) {
mBitmap = b;
}

@Override
public void draw(Canvas canvas) {
canvas.drawBitmap(mBitmap, 0.0f, 0.0f, null);
}

@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}

@Override
public void setAlpha(int alpha) {
}

@Override
public void setColorFilter(ColorFilter cf) {
}

@Override
public int getIntrinsicWidth() {
return mBitmap.getWidth();
}

@Override
public int getIntrinsicHeight() {
return mBitmap.getHeight();
}

@Override
public int getMinimumWidth() {
return mBitmap.getWidth();
}

@Override
public int getMinimumH…

Soft KeyBoard in Android

If in a screen, we have more than one view or we have a tab activity, then we may need to hide the virtual keyboard, if it is open. We can do this using


InputMethodManager imm = context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(this.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);


Always hide soft key board

To always hide the soft keyboprad, use one of the two methods.


InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);


or

getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Soft Keyboard in Landscape mode

In landscape mode soft keyboard will show in full screen. So edittext of password type won't work. For this, just add this API,


editText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);

This will resize the soft keyboard and will show the edit text.

To get the events of soft keyboard, we can use


editText.addTextChang…