Sunday, September 27, 2009

Include attribute in Android

Include tag in android is used to add an layout XML file to another file. Here I am showing a simple way, to do this.


<include layout="@layout/newfile"/>

Here, "newfile" is the name of another layout file

Happy Coding...

Thursday, September 24, 2009

Reading Contacts from mobile and calling

Before coming to Contacts, first I will touch a bit on Content provider in Android

Content Provider

ContentProvider in android is mainly used to share data between applications. Android has some built-in ContentProviders. They are


-Contacts
-CallLog
-MediaStore
-Browser
-Settings
-SMS


We store data into contentProvider using contentprovider base class. We will store data in any way, generally, using Sqlite database.

We will retrieve data from contentprovider using a single interface called ContentResolver. We can get a object of contentResolver using getContentResolver().

One main thing we require for retrieving data from content provider is the URI.
To get contact list, to the query(), just pass "People.CONTENT_URI".


Cursor c = getContentResolver().query(People.CONTENT_URI, null, null,
null, null);


We will receive the contact details in a cursor. Its better to use startManagingCursor(c). Because then Android ,itself will take care of the cursor, means when activity is stopped, it will call deActivate() automatically on the given cursor. When activity restarts, it will call requery() and at the time of destroying an activity, all the cursors will be closed automatically.

In this program, I am just showing the name and number of the persons in the contact list. We can display lot of data about contact persons like email, notes, ringtone assigned, photo assigned etc. For displaying data, I am first putting all the data in a listAdapter using,

ListAdapter listAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, c, new String[] {
Phones.NAME, Phones.NUMBER }, new int[] {android.R.id.text1,android.R.id.text2 });

The output will look like this in emulator,





Next part is calling the displayed numbers by just clicking on the list. For that, I am going to use onListItemClick(). To know which item in the list, the user clicked, I am using, listAdapter.getItem(position), will give the item clicked. Intent.ACTION_CALL is used for calling a particular. For dialing a number, we have to
use the Uri "tel:".

Another important thing is, we are going to start a new task(Calling), so we have to add the flag "Intent.FLAG_ACTIVITY_NEW_TASK" to the intent.

When we click on any contact, the dial screen will look like this,



After call ends, when we click back button, it will come back to our application.

The full source code is,


public class AndContacts extends ListActivity {

private ListAdapter listAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Cursor c = getContentResolver().query(People.CONTENT_URI, null, null,
null, null);

startManagingCursor(c);
listAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, c, new String[] {
Phones.NAME, Phones.NUMBER }, new int[] {
android.R.id.text1, android.R.id.text2 });
setListAdapter(listAdapter);
}

protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor c = ((Cursor) listAdapter.getItem(position));
long phoneNumber = c.getLong(c.getColumnIndex(People.NUMBER));
String num = "tel:";
num += Long.toString(phoneNumber);
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(num));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}

Wednesday, September 23, 2009

Gradient In Android



Gradient are drawn in android using GradientDrawable.

The code is,


GradientDrawable grad = new GradientDrawable(Orientation.LEFT_RIGHT,
new int[]{0xffffffff, 0xffff00ff, 0xffffff00,
0xff0000ff, 0xf0f0f0f0, 0xfefefefe});
grad.setBounds(0, 0, 320, 480);


then, in onDraw(), just call

grad.draw(canvas);

The entire source code is,


public class AndGradient extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new ViewGradient(this));
}

class ViewGradient extends View {

GradientDrawable grad;
public ViewGradient(Context context) {
super(context);
grad = new GradientDrawable(Orientation.LEFT_RIGHT,
new int[]{0xffffffff, 0xffff00ff, 0xffffff00,
0xff0000ff, 0xf0f0f0f0, 0xfefefefe});
grad.setBounds(0, 0, 320, 480);
}

public void onDraw(Canvas canvas) {
super.onDraw(canvas);
grad.draw(canvas);
}
}
}

Thursday, September 17, 2009

Creating dashed circle and bulls eye(Marker)





In this tutorial, I am going to explain how to create dashed line or circle.

All we have to do for dashed circle, create a paint object with some special settings. To paint object we have to set some properties. Android have a class called "DashPathEffect".


DashPathEffect dashPath = new DashPathEffect(new float[]{5,5}, 1};


Then to paint object, we have to attach this "dashPath" by,


paint.setPathEffect(dashPath);

then

paint.setStyle(Style.STROKE);
paint.setStrokeWidth(3.0f);

Then in onDraw(), use this paint object in the drawCircle().

The complete source code is,


public class AndDashedLine extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new ViewDashedLine(this));
}

class ViewDashedLine extends View {

Context mContext;
Paint linePaint;
Paint dotPaint;
Paint linePaint1;
DashPathEffect dashPath;
public ViewDashedLine(Context context) {
super(context);
mContext = context;
dashPath = new DashPathEffect(new float[]{5, 5}, 1);

linePaint = new Paint();
linePaint.setAntiAlias(true);
linePaint.setARGB(255, 255, 0, 0);
linePaint.setPathEffect(dashPath);
linePaint.setStyle(Style.STROKE);
linePaint.setStrokeWidth(3.0f);

dotPaint = new Paint();
dotPaint.setAntiAlias(true);
dotPaint.setARGB(255, 255, 0, 0);
dotPaint.setStyle(Style.FILL);

linePaint1 = new Paint();
linePaint1.setAntiAlias(true);
linePaint1.setARGB(255, 255, 0, 0);
linePaint1.setStrokeWidth(3.0f);
}

public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(160, 200, 40, linePaint);
canvas.drawCircle(160, 200, 20, linePaint);
canvas.drawCircle(160, 200, 4, dotPaint);

canvas.drawLine(160, 160, 160, 130, linePaint1);
canvas.drawLine(160, 240, 160, 270, linePaint1);
canvas.drawLine(200, 200, 230, 200, linePaint1);
canvas.drawLine(120, 200, 90, 200, linePaint1);
}
}
}

Creating Static Label




In this tutorial, I am going to explain custom label.

For drawing label, we have to draw two same round rectangle with two differnt paint objects. First paint object will fill the inside color


Paint textDialog = new Paint();
textDialog.setARGB(255, 220, 220, 220);
textDialog.setAntiAlias(true);


and second paint object for the border color, here I am giving green color for border,


Paint borderDialog = new Paint();
borderDialog.setARGB(255, 0, 255, 0);
borderDialog.setStyle(Style.STROKE);
borderDialog.setStrokeWidth(3.5f);


Now, for drawing in onDraw(), we have to draw two round rectangle as,


canvas.drawRoundRect(rectDialog, 7, 7, textDialog);
canvas.drawRoundRect(rectDialog, 7, 7, borderDialog);

rectDialog, is the area on which, I am going to draw, here its

RectF rectDialog = new RectF(100, 100, 200, 130);

and 7 is the radius for the round rectangle.

For drawing inside the label, I am creating another Paint with some other funcionalities.

Paint textPaint = new Paint();
textPaint.setARGB(255, 0, 0, 255);
textPaint.setTypeface(Typeface.DEFAULT_BOLD);
textPaint.setTextSize(15);
textPaint.setAntiAlias(true);

Then, in onDraw, I am writing the text as,

canvas.drawText("Androidians", 105, 120, textPaint);

The complete source code is,


public class AndLabel extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new ViewLabel(this));
}

class ViewLabel extends View {

Paint textDialog;
Paint borderDialog;
Paint textPaint;
RectF rectDialog;
public ViewLabel(Context context) {
super(context);
textDialog = new Paint();
textDialog.setARGB(255, 220, 220, 220);
textDialog.setAntiAlias(true);

textPaint = new Paint();
textPaint.setARGB(255, 255, 0, 0);
textPaint.setTypeface(Typeface.DEFAULT_BOLD);
textPaint.setTextSize(15);
textPaint.setAntiAlias(true);

borderDialog = new Paint();
borderDialog.setARGB(255, 0, 255, 0);
borderDialog.setStyle(Style.STROKE);
borderDialog.setStrokeWidth(3.5f);

rectDialog = new RectF(100, 100, 200, 130);
}

public void onDraw(Canvas canvas) {
super.onDraw(canvas);

canvas.drawRoundRect(rectDialog, 7, 7, textDialog);
canvas.drawText("Androidians", 105, 120, textPaint);
canvas.drawRoundRect(rectDialog, 7, 7, borderDialog);
}
}
}

Tuesday, September 15, 2009

Creating View from XML layout

We can create a view from the XML layout file. the way to do this is using,


LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflater(R.layout.main, true);

Wednesday, September 9, 2009

Drawing customized shapes and basic shapes in Android



By extending the View Class, we can draw lot of shapes like circle, triangle, rectangle, rounded rectangle, bitmap, gradientdrawable, customized shape etc. Here I am explaining a few things about that.

Drawing circle, rectangle are straight forward, triangle we can draw using three lines. For customized shapes, we can use "Path" class in Android.

Example :


Path path = new Path();
path.moveTo(160.0f, 240.0f);
path.lineTo(140.0f, 200.0f);
path.addArc(new RectF(140, 180, 180, 220), -180, 180);
path.lineTo(160.0f, 240.0f);
path.close();

This path, we will draw in canvas using,

canvas.drawPath(path, paint);

Androidians