Browser Content Provider Example

Code to check no. of url visited by user.

/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	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" />
</LinearLayout>

Activity file

package com.androidwork.browsercontentprovider;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Browser;
import android.util.Log;

public class BorwserContentProviderActivity extends Activity {
	/** Called when the activity is first created. */
	private static final String DEBUG_TAG = "VIEW2";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		String[] requestedColumns = { Browser.BookmarkColumns.TITLE,
				Browser.BookmarkColumns.VISITS,
				Browser.BookmarkColumns.BOOKMARK

		};
		Cursor faves = managedQuery(Browser.BOOKMARKS_URI, requestedColumns,
				null, null, null);
		Log.d(DEBUG_TAG, "Bookmarks count: " + faves.getCount());
		int titleIdx = faves.getColumnIndex(Browser.BookmarkColumns.TITLE);
		int visitsIdx = faves.getColumnIndex(Browser.BookmarkColumns.VISITS);
		int bmIdx = faves.getColumnIndex(Browser.BookmarkColumns.BOOKMARK);
		faves.moveToFirst();
		int count = 0;
		while (!faves.isAfterLast()) {
		//	Log.d(DEBUG_TAG, "Inside while --- : " + count);
			Log.d("SimpleBookmarks",
					faves.getString(titleIdx) + " visited "
							+ faves.getInt(visitsIdx) + " times : "
							+ (faves.getInt(bmIdx) != 0 ? "true" : "false"));
			faves.moveToNext();
			count++;
		}
	}
}

Output

Advertisements

Mobile Computing Lecture Notes Handouts

Unit : 1, Chapter :1 Introducing Android Download
Unit : 1, Chapter :3 Writting your first android application Download
Unit : 2, Chapter :4 Understanding the anatomy of android application Download
Unit : 2, Chapter :5 Defining your application using android manifest file Download
Unit : 2, Chapter :6 Managing application resources Download
Unit : 3, Chapter :7 Exploring user interface screen elements Download
Unit : 3, Chapter :8 Designing user interfaces with layouts Download
Unit : 3, Chapter :9 Drawing and working with animation Download
Unit : 4, Chapter :10 Using android data and storage apis Download
Unit : 4, Chapter :11 Sharing data between applications using content providers Download
Unit : 4, Chapter :12 Using android Network APIs Download
Unit : 4, Chapter :13 Using android Web APIs Download
Unit : 4, Chapter :16 Using android Telephony APIs Download
Unit : 5, Chapter :29 Selling your android application Download

UNIT_1_Chap1

Drawing Shapes (Using ShapeDrawable)

Drawing Shapes (Using ShapeDrawable)

Note : Uncomment code to see various shapes
/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<ImageView android:layout_width="fill_parent"
		android:layout_height="fill_parent" android:id="@+id/ImageView1" />
</LinearLayout>

/res/drawable/green_rect.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
	android:shape="rectangle">
	<solid android:color="#0f0" />
</shape>

/res/drawable/green_rect.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
	android:shape="rectangle">
	<solid android:color="#0f0" />
</shape>

/res/drawable/shaperred.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
	android:shape="oval">
	<gradient android:startColor="#f00" android:centerColor="#fffa0f"
		android:endColor="#f0f" android:angle="180" />
	<stroke android:width="3dp" android:color="#00f"
		android:dashWidth="5dp" android:dashGap="3dp" />
</shape>

Activity file

package com.androidwork.shapesdemo;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.ArcShape;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.PathShape;
import android.graphics.drawable.shapes.RectShape;
import android.graphics.drawable.shapes.RoundRectShape;
import android.graphics.drawable.ShapeDrawable;
import android.os.Bundle;
import android.widget.ImageView;

public class ShapesDemoActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// ////////Getting shapes from xml file //////////////
		// ImageView iv = (ImageView) findViewById(R.id.ImageView1);
		// // iv.setImageResource(R.drawable.green_rect);
		// iv.setImageResource(R.drawable.shaperred);
		// ///////////////////////////////////

		// //////////////Drawing Rectangle Programmatically////////////////////

		// ShapeDrawable rect = new ShapeDrawable(new RectShape());
		// rect.setIntrinsicHeight(20);
		// rect.setIntrinsicWidth(100);
		// rect.getPaint().setColor(Color.MAGENTA);
		//
		// ImageView iView = (ImageView)findViewById(R.id.ImageView1);
		// iView.setImageDrawable(rect);

		// ///////// Rounded rectangle Programmatically/////////////////

		// ShapeDrawable rndrect = new ShapeDrawable(
		// new RoundRectShape( new float[] { 5, 15, 5, 15, 5, 15, 5, 15 },
		// null, null));
		// rndrect.setIntrinsicHeight(50);
		// rndrect.setIntrinsicWidth(100);
		// rndrect.getPaint().setColor(Color.CYAN);
		// ImageView iView = (ImageView)findViewById(R.id.ImageView1);
		// iView.setImageDrawable(rndrect);

		// ////////////Testing RoundedRect //////////////
		//
		// float[] outerRadii = new float[]{ 16,16, 6, 6, 16,16, 6,6 };
		// RectF insetRectangle = new RectF(8, 8, 8, 8);
		// float[] innerRadii = new float[]{ 16, 16, 6, 6, 16, 16, 6, 6 };
		//
		// //
		// //// float[] outerRadii = new float[]{ 6,6, 6, 6, 6,6, 6,6 };
		// //// RectF insetRectangle = new RectF(10, 10, 10, 10);
		// //// float[] innerRadii = new float[]{ 16, 16, 16, 16, 16, 16, 16, 16
		// };
		// ShapeDrawable rndrect = new ShapeDrawable(
		// new RoundRectShape(
		// outerRadii,insetRectangle , innerRadii));
		// rndrect.setIntrinsicHeight(50);
		// rndrect.setIntrinsicWidth(100);
		// rndrect.getPaint().setColor(Color.WHITE);
		// ImageView iView = (ImageView)findViewById(R.id.ImageView1);
		// iView.setImageDrawable(rndrect);

		// //////Drawing oval & Circle programmatically /////////////
		// ShapeDrawable oval = new ShapeDrawable(new OvalShape());
		// oval.setIntrinsicHeight(10);
		// oval.setIntrinsicWidth(50);
		// oval.getPaint().setColor(Color.CYAN);
		// ImageView iView = (ImageView)findViewById(R.id.ImageView1);
		// iView.setImageDrawable(oval);

		// //////////Drawing Arc///////////////////

		ShapeDrawable pacMan = new ShapeDrawable(new ArcShape(0, 340));
		pacMan.setIntrinsicHeight(100);
		pacMan.setIntrinsicWidth(100);
		pacMan.getPaint().setColor(Color.MAGENTA);
		ImageView iView = (ImageView) findViewById(R.id.ImageView1);
		iView.setImageDrawable(pacMan);

		// ////////////Drawing Path///////////////////

		// Path p = new Path();
		// // p.mo
		// p.moveTo(50, 0);
		// p.lineTo(25,100);
		// p.lineTo(100,50);
		// p.lineTo(0,50);
		// p.lineTo(75,100);
		// p.lineTo(50,0);
	
		// ShapeDrawable star =
		// new ShapeDrawable(new PathShape(p, 100, 100));
		// star.setIntrinsicHeight(100);
		// star.setIntrinsicWidth(100);
		// star.getPaint().setColor(Color.YELLOW);
	
		// star.getPaint().setStyle(Paint.Style.STROKE);
		// ImageView iView = (ImageView)findViewById(R.id.ImageView1);
		// iView.setImageDrawable(star);
	}
}

Output

Bitmap Transformations

Bitmap Transformations (Scaling, Rotation)

Note : uncomment the code to see various effects.

/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	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" />
</LinearLayout>

/res/drawable/test.bmp

Activity file

package com.androidwork.bmpdemo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;

public class BMPDemoActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new loadbitmap(this));

	}
}

class loadbitmap extends View {

	public loadbitmap(Context context) { 
		super(context);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onDraw(Canvas canvas) {

		Bitmap pic = BitmapFactory.decodeResource(getResources(),
				R.drawable.test);
		// for scaling down ///1

		// Bitmap sm = Bitmap.createScaledBitmap(pic, 50, 100, false);
		// ////////////////////////

		// //transforming bmp ////
		// Matrix mirrorMatrix = new Matrix();
		// //mirrorMatrix.preScale(-1, -1);
		// //mirrorMatrix.preScale(1, -1);
		// mirrorMatrix.preScale(2, -1);
		// //mirrorMatrix.preScale(1, -2);

		// Bitmap mirrorPic = Bitmap.createBitmap(pic, 0, 0,
		// pic.getWidth(), pic.getHeight(), mirrorMatrix, false);
		// /////////////////

		// /////30 degree rotation ////////////

		Matrix mirrorAndTilt30 = new Matrix();

		mirrorAndTilt30.preRotate(180);
		// mirrorAndTilt30.preScale(-1, 1);
		Bitmap mirrorPic = Bitmap.createBitmap(pic, 0, 0, pic.getWidth(),
				pic.getHeight(), mirrorAndTilt30, false);
		// ////////////////////////

		canvas.drawBitmap(mirrorPic, 0, 0, null);

	}

}

Output

Tweened Animation

Tweened Animation

/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<Button android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/hello"
		android:id="@+id/button1" />
	<ImageView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:id="@+id/imageview1"
		android:src="@drawable/a" />
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:id="@+id/TextView1"
		android:text="Hello" android:textSize="40dp" />

</LinearLayout>

/res/anim/alphaeffect.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
	android:shareInterpolator="false"
	android:interpolator="@android:anim/accelerate_decelerate_interpolator">
	<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
		android:duration="5000">
	</alpha>
</set>

/res/anim/grow.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
	android:shareInterpolator="false">
	<scale android:pivotX="50%" android:pivotY="50%"
		android:fromXScale="1.0" android:fromYScale="1.0" android:toXScale="4.0"
		android:toYScale="4.0" android:duration="2500" />
	<scale android:startOffset="2500" android:duration="2500"
		android:pivotX="50%" android:pivotY="50%" android:fromXScale="1.0"
		android:fromYScale="1.0" android:toXScale="0.5" android:toYScale="0.5" />
</set>

/res/anim/hyperscpace_jump.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
	android:shareInterpolator="false">
	<scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"
		android:fromXScale="1.0" android:toXScale="1.4" android:fromYScale="1.0"
		android:toYScale="0.6" android:pivotX="50%" android:pivotY="50%"
		android:fillAfter="false" android:duration="700" />
	<set android:interpolator="@android:anim/accelerate_interpolator"
		android:startOffset="700">
		<scale android:fromXScale="1.4" android:toXScale="0.0"
			android:fromYScale="0.6" android:toYScale="0.0" android:pivotX="50%"
			android:pivotY="50%" android:duration="400" />
		<rotate android:fromDegrees="0" android:toDegrees="-45"
			android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%"
			android:duration="400" />
	</set>
</set>

/res/anim/spin.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
	android:shareInterpolator="false">
	<rotate android:fromDegrees="0" android:toDegrees="360"
		android:pivotX="100%" android:pivotY="100%" android:duration="5000" />
</set>

/res/anim/spin.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
	android:shareInterpolator="false">
	<translate android:toXDelta="100" android:fillAfter="false"
		android:duration="2500" />
</set>

Activity file

package com.androidwork.tweenedanimationdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class TweenedAnimationDemoActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		ImageView spaceshipImage = (ImageView) findViewById(R.id.imageview1);

		Animation a = AnimationUtils.loadAnimation(getApplicationContext(),
				R.anim.translateanim);
		spaceshipImage.startAnimation(a);
		// spaceshipImage.startAnimation(hyperspaceJumpAnimation);
		TextView tv = (TextView) findViewById(R.id.TextView1);
		tv.startAnimation(a);

		// AlphaAnimation aa = new AlphaAnimation(context, attrs)

	}
}

Output

Animation

Animation (Frame by Frame Demo)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<Button android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:id="@+id/Button1"
		android:text="Start" />
	<ImageView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:id="@+id/ImageView1" />
</LinearLayout>

/res/drwable/spin_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/a" android:duration="500"/>
    <item android:drawable="@drawable/b" android:duration="500"/>
    <item android:drawable="@drawable/c" android:duration="500"/>
</animation-list>

/res/drawalbe/a.png
/res/drawalbe/b.png
/res/drawalbe/c.png

Activity file

package com.androidwork.animationdemo;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class AnimationDemoActivity extends Activity {
    /** Called when the activity is first created. */
	 AnimationDrawable animation;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button b = (Button) findViewById(R.id.Button1);
        b.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ImageView iv = (ImageView) findViewById(R.id.ImageView1);
				iv.setBackgroundResource(R.drawable.spin_animation);
				AnimationDrawable ad = (AnimationDrawable) iv.getBackground();
				ad.start();
			}
		});
    }
  }

Output

Gradient

Gradient (Linear, Sweep Gradient)

/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	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" />
</LinearLayout>

Activity file

package com.androidwork.gradientdemo;

import android.graphics.Shader;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.os.Bundle;
import android.view.View;

public class GradientDemoActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main);
		setContentView(new ViewWithRedDot(this));

	}
}

class ViewWithRedDot extends View {
	public ViewWithRedDot(Context context) {
		super(context);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(Color.BLACK);
		Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		LinearGradient linGrad = new LinearGradient(0, 0, 25, 25,

		Color.RED, Color.GREEN, Shader.TileMode.REPEAT);
		circlePaint.setShader(linGrad);
		canvas.drawCircle(100, 100, 50, circlePaint);

		RadialGradient rg = new RadialGradient(50, 50, 10, Color.GREEN,
				Color.WHITE, Shader.TileMode.REPEAT);
		circlePaint.setShader(rg);
		canvas.drawCircle(150, 150, 50, circlePaint);
		// Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);

		SweepGradient sweepGrad = new SweepGradient(canvas.getWidth() - 175,
				canvas.getHeight() - 175, new int[] { Color.RED, Color.YELLOW,
						Color.GREEN, Color.BLUE, Color.MAGENTA }, null);

		circlePaint.setShader(sweepGrad);
		canvas.drawCircle(canvas.getWidth() - 175, canvas.getHeight() - 175,
				100, circlePaint);

		// Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		// SweepGradient sweepGrad = new
		// SweepGradient(canvas.getWidth()-175,
		// canvas.getHeight()-175,
		// new int[] { Color.RED, Color.YELLOW, Color.GREEN,
		// Color.BLUE, Color.MAGENTA }, null);
		// circlePaint.setShader(sweepGrad);
		//
		// canvas.drawCircle(100, 100, 50, circlePaint);
		//
		// circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		// SweepGradient sweepGrad = new
		// SweepGradient(canvas.getWidth()-175,
		// canvas.getHeight()-175,
		// new int[] { Color.RED, Color.YELLOW, Color.GREEN,
		// Color.BLUE, Color.MAGENTA }, null);
		// circlePaint.setShader(sweepGrad);
		// canvas.drawCircle(canvas.getWidth()-175,
		// canvas.getHeight()-175, 100,
		// circlePaint);
	}
}

Output