Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Thursday, February 14, 2013

Android Custom TimePickerDialog

Yesterday we are facing issues regarding the Custom TimePickerDialog,our main requirement was adjustment in minute field of TimePickerDialog as per user requirement.And here is our solution.



import java.util.Calendar;

import android.app.Activity;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TimePicker;

public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 }

 public void timeAction(View v) {
  CustomTimePickerDialog timePickerDialog = new CustomTimePickerDialog(
    this, timeSetListener, Calendar.getInstance()
      .get(Calendar.HOUR),
    CustomTimePickerDialog.getRoundedMinute(Calendar.getInstance()
      .get(Calendar.MINUTE)
      + CustomTimePickerDialog.TIME_PICKER_INTERVAL), false);
  timePickerDialog.setTitle("2. Select Time");
  timePickerDialog.show();
 }

 private CustomTimePickerDialog.OnTimeSetListener timeSetListener = new CustomTimePickerDialog.OnTimeSetListener() {
  @Override
  public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

  }
 };// using CustomTimePickerDialog
}

class CustomTimePickerDialog extends TimePickerDialog {
 public static final int TIME_PICKER_INTERVAL = 15;
 private boolean mIgnoreEvent = false;

 public CustomTimePickerDialog(Context context, OnTimeSetListener callBack,
   int hourOfDay, int minute, boolean is24HourView) {
  super(context, callBack, hourOfDay, minute, is24HourView);
 }
 @Override
 public void onTimeChanged(TimePicker timePicker, int hourOfDay, int minute) {
  super.onTimeChanged(timePicker, hourOfDay, minute);
  this.setTitle("2. Select Time");
  if (!mIgnoreEvent) {
   minute = getRoundedMinute(minute);
   mIgnoreEvent = true;
   timePicker.setCurrentMinute(minute);
   mIgnoreEvent = false;
  }
 }

 public static int getRoundedMinute(int minute) {
  if (minute % TIME_PICKER_INTERVAL != 0) {
   int minuteFloor = minute - (minute % TIME_PICKER_INTERVAL);
   minute = minuteFloor
     + (minute == minuteFloor + 1 ? TIME_PICKER_INTERVAL : 0);
   if (minute == 60)
    minute = 0;
  }
  return minute;
 }
}

Tuesday, April 17, 2012

When not to use Base64 encoding

It has been my long voyage to find where we have use base64 encoding. Recently I was developing an android app where I have get encoded (base64 encoded) from server and displays them in ListView.
The app performance was annoying I have make my app behave properly. After long brain storming I have found some interesting result which I want to share with you guys.

Here are some of my findings
  • base64 encoding makes file size roughly 33% larger than their original binary representation.
  • base64 encoded data may possible take more resources(memory + CPU).
I will update my result later.

Download Progress in Android

I was trying to integrate the progress with download in android finally I got it.
public class DownloadExampleActivity extends Activity {
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private Button startBtn;
private ProgressDialog mProgressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startBtn = (Button) findViewById(R.id.startBtn);
startBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startDownload();
}
});
}

private void startDownload() {
String url = "http://www.kleenspeed.com/blog/wp-content/uploads/2011/10/DSCN1590.jpg";
new DownloadFileAsync().execute(url);
}

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading file..");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}

class DownloadFileAsync extends AsyncTask {

@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}

@Override
protected String doInBackground(String... aurl) {
int count;

try {

URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();

int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

InputStream input = new BufferedInputStream(url.openStream());
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
}
} catch (Exception e) {
Toast.makeText(DownloadExampleActivity.this, e.getMessage(),
Toast.LENGTH_SHORT).show();
}
return null;

}

protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC", progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}

@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}
}

Friday, February 17, 2012

World of Titanium

Just explored few aspects of Titanium appcelerator in my office. I am impressed with some aspect of the titanium SDK and hate some features as well.