Android Date Time example

Please follow the 2 steps to implement Date and Time picker in application.

1. create xm like  “main.xml”

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:orientation=”vertical” android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
<TextView android:layout_width=”fill_parent”
android:layout_height=”wrap_content” android:text=”@string/hello” />
<RelativeLayout android:orientation=”vertical”
android:layout_width=”fill_parent” android:layout_height=”fill_parent”>
<Button android:id=”@+id/timepicker” android:layout_width=”121px”
android:layout_height=”wrap_content” android:text=”Time Picker”
android:layout_alignParentTop=”true” android:layout_alignParentRight=”true”>
</Button>
<Button android:id=”@+id/datepicker” android:layout_width=”122px”
android:layout_height=”wrap_content” android:text=”Date Picker”
android:layout_alignParentTop=”true” android:layout_alignParentLeft=”true”>
</Button>
</RelativeLayout>
</LinearLayout>

2. Append an event listner in Activity like below:

package com.pr.testone;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

public class TestOneActivity extends Activity implements Button.OnClickListener{
private Button b1, b2;
static final int DATE_DIALOG_ID = 0;
static final int TIME_DIALOG_ID = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Adding button to the layout
b1 = (Button) findViewById(R.id.datepicker);
b1.setOnClickListener(this);
b2 = (Button) findViewById(R.id.timepicker);
b2.setOnClickListener(this);

}

// Creating dialog
@Override
protected Dialog onCreateDialog(int id) {
Calendar c = Calendar.getInstance();
int cyear = c.get(Calendar.YEAR);
int cmonth = c.get(Calendar.MONTH);
int cday = c.get(Calendar.DAY_OF_MONTH);
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, cyear, cmonth,
cday);
case TIME_DIALOG_ID:
return new TimePickerDialog(this, mTimeSetListener, 0, 0,false);
}
return null;
}

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
// onDateSet method
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
String date_selected = String.valueOf(monthOfYear + 1) + ” /”
+ String.valueOf(dayOfMonth) + ” /” + String.valueOf(year);
Toast.makeText(TestOneActivity.this,
“Selected Date is =” + date_selected, Toast.LENGTH_SHORT)
.show();
}
};

private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(TestOneActivity.this, “Time is=”+hourOfDay+”:”+minute, Toast.LENGTH_SHORT).show();
}
};

@Override
public void onClick(View v) {
if (v == b1){
showDialog(DATE_DIALOG_ID);
}else if (v == b2) {
showDialog(TIME_DIALOG_ID);
}
}
}

The result will be as shown in screen captures.

Leave a comment

Filed under Uncategorized

Android AIDL Example

Steps for aidl implemenation:
1.
Create aidl file in src folder as below.
SaInterface.aidl
**********************************
package com.sample.aidl.ui;

interface SAInterface {
String getDownloadFilename (in String name);
}
**********************************
2.
Implement getDownloadFilename(String) method in Service extended class as below.

**********************************
package com.sample.aidl.ui;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

public class AidlService extends Service {

@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent arg0) {
return mBinder;
}

private final SAInterface.Stub mBinder = new SAInterface.Stub() {
public String getDownloadFilename(String name) throws RemoteException {
return “You are downloading” + name + “file.”;
}
};
}
*********************************
3.
Make a service call in AidlActivity.

*********************************
package com.sample.aidl.ui;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class AidlActivity extends Activity {

private SAInterface saInterface;
private Context mContext = this;

private String TAG = AidlActivity.class.getSimpleName();
private static final int PROGRESS = 0xDEADBEEF;

private Handler mHandler = new Handler(){

public void handleMessage(android.os.Message msg) {
if (msg.what == PROGRESS) {
try {
String DownloadfileName = saInterface.getDownloadFilename(“AIDL Checking!”);
Log.d(TAG, “DownloadfileName: “+DownloadfileName);
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
};

private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
saInterface = SAInterface.Stub.asInterface(service);
}

public void onServiceDisconnected(ComponentName className) {
saInterface = null;
}
};

/** Called with the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
//bind the ServiceConnection object to activity.
mContext.bindService(new Intent(AidlActivity .this,
AidlService.class), mConnection, Context.BIND_AUTO_CREATE);
mHandler.sendMessageDelayed(mHandler.obtainMessage(PROGRESS), 500);
}

@Override
protected void onDestroy() {
// unbind the ServiceConnection object from activity to avoid ServiceConnectionLeaked error.
mContext.unbindService(mConnection);
super.onDestroy();
}
}
*******************************

now check the logcat.

Leave a comment

Filed under Uncategorized

Phone number in Blackberry Webworks

To get the Phone number in Blackberry for Webworks call the below mentioned function:

/*
userPhoneNumber() fetches the user mobile number.
*/
function userPhoneNumber(){
var lineIds;
var phoneNumber = null;
var mobileType = 1;

try {
lineIds = blackberry.identity.phone.getLineIds();
for( var  i=0; i < lineIds.length; i++ ) {
if(blackberry.identity.phone.getLineType(lineIds[i]) == mobileType) {
//found the user’s cell number
phoneNumber = blackberry.identity.phone.getLineNumber(lineIds[i]);
}
}
}
catch(err) {

}

if(phoneNumber !=null){
document.getElementById(‘phone’).value = phoneNumber;
return true;
}else{
return false;
}

}

Leave a comment

Filed under Uncategorized

custom dialog box in Android

To customize the dialog box, you can follow the below steps.

call the below method in any listener:

code to keep in class:

protected AlertDialog onClickLogin() {
// This example shows how to add a custom layout to an AlertDialog
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
return new AlertDialog.Builder(CustomDialogOneActivity.this)
// .setIconAttribute(android.R.attr.alertDialogIcon)
.setTitle(“Snapshot”)
.setView(textEntryView)
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

/* User clicked OK so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

/* User clicked cancel so do some stuff */
}
})
.show();
}

/********************************************/

layout code :

alert_dialog_text_entry.xml

<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”match_parent” android:layout_height=”wrap_content”
android:orientation=”vertical”>
<LinearLayout android:layout_width=”fill_parent”
android:layout_height=”wrap_content” android:layout_gravity=”top|center”
android:paddingTop=”10dip” android:paddingRight=”40dip”>

<TableLayout android:layout_width=”fill_parent”
android:layout_height=”wrap_content” android:stretchColumns=”1″
android:layout_gravity=”center”>
<TableRow>
<TextView android:layout_width=”wrap_content”
android:layout_height=”wrap_content” android:text=”TextView”
android:gravity=”center” android:paddingLeft=”25dip” />

<EditText android:id=”@+id/exception_reason_button”
android:layout_width=”wrap_content” android:layout_height=”wrap_content”
android:text=”” android:onClick=”onClickReason” />
</TableRow>
<TableRow>
<TextView android:layout_width=”wrap_content”
android:layout_height=”wrap_content” android:text=”Button”
android:gravity=”center” android:paddingLeft=”25dip” />

<Button android:id=”@+id/exception_reason_button”
android:layout_width=”wrap_content” android:layout_height=”wrap_content”
android:text=”Button” android:onClick=”onClickReason” />
</TableRow>
</TableLayout>

</LinearLayout>
<LinearLayout android:layout_width=”wrap_content”
android:orientation=”horizontal”
android:layout_height=”wrap_content” android:layout_gravity=”center|bottom” android:gravity=”center”>

<ImageView android:id=”@+id/camera_button” android:src=”@drawable/android”
android:layout_width=”wrap_content” android:layout_weight=”1″
android:layout_height=”wrap_content” android:onClick=”onClickImageOne”
android:layout_gravity=”center” android:gravity=”center”
android:focusable=”true” android:padding=”30dip” />

<ImageView android:id=”@+id/camera_button” android:src=”@drawable/android”
android:layout_width=”wrap_content” android:layout_weight=”1″
android:layout_height=”wrap_content” android:onClick=”onClickImageTwo”
android:layout_gravity=”center” android:gravity=”center”
android:focusable=”true” android:padding=”30dip” />

</LinearLayout>
</LinearLayout>

1 Comment

Filed under Uncategorized

Android EditText validations

Android EditText can be validated much easily see the below example:

EditText loginText = (EditText)this.findViewById(R.id.login_name);

Integer nameLen = 8;

int len = loginText.length();
if (len<=0) {
loginText.setError(getText(R.string.error_required));
}
else if (len< nameLen) {
loginText.setError(getString(R.string.error_character_length, nameLen));
}
else {
nameId = loginText.getText().toString();
}

8 Comments

Filed under Uncategorized

How to get dip value in place of pixels, through Android programming

By default int values will be considered as pixels in programming. To use dip value instead of pixels we have to use “TypedValue.applyDimension()".

For Example:

Resources resources = getResources();
float dipValue = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 75, resources.getDisplayMetrics());
Log.d(“LobActivity”, “dipValue : “+dipValue );

If you run above code in different density emulators, you can identify the different values are being  generated for dipValue.

1 Comment

Filed under Uncategorized