Posts

Showing posts from September, 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...

Reading Contacts from mobile and calling

Image
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 wi…

Gradient In Android

Image
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);
}
}
}

Creating dashed circle and bulls eye(Marker)

Image
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(…

Creating Static Label

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

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

Drawing customized shapes and basic shapes in Android

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