Hi Friends, if you like my blog please give your valuable comments it will help to improve my blog content and enthusiasm to write a lot in android World.

Wednesday, February 29, 2012

Combine two images in android java

 


In this blog we are combine two images and we have two images stored locally on an SD card or drawble folder in android.

Steps:

Read the image from Source
InputStream istream = context.getResources().openRawResource(R.drawable.img1);
try {
image1 = BitmapFactory.decodeStream(istream);
istream = context.getResources().openRawResource(R.drawable.img2);
image2 = BitmapFactory.decodeStream(istream);
} finally {
try {
istream.close();
} catch (IOException e) {
}
}

Define the Image property
int width = 0, height = 0;
if (c.getWidth() > s.getWidth()) {
width = c.getWidth();
height = c.getHeight() + s.getHeight();
} else {
width = s.getWidth();
height = c.getHeight() + s.getHeight();
}

Create your target Bitmap,
Bitmap combinedImages = Bitmap.createBitmap(width * 2, height, Bitmap.Config.ARGB_8888);

Create a Canvas for it,
Canvas comboImage = new Canvas(combinedImages);
Use Canvas.drawBitmap to blit each source bitmap into your target bitmap
comboImage.drawBitmap(image1, 0f, 0f, null);
comboImage.drawBitmap(image2, 0f, image1.getHeight()+1, null);

Example:
public Bitmap combineImages(Context context, int img1, int img2) {
// Bitmap[] mBitmap = new Bitmap[6];
Bitmap image1, image2;

InputStream istream = context.getResources().openRawResource(img1);
try {
image1 = BitmapFactory.decodeStream(istream);
istream = context.getResources().openRawResource(img2);
image2 = BitmapFactory.decodeStream(istream);
} finally {
try {
istream.close();
} catch (IOException e) {
}
}
int width = 0, height = 0;
if (c.getWidth() > s.getWidth()) {
width = c.getWidth();
height = c.getHeight() + s.getHeight();
} else {
width = s.getWidth();
height = c.getHeight() + s.getHeight();
}
Bitmap combinedImages = null;
combinedImages = Bitmap
.createBitmap(width * 2, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(combinedImages);
comboImage.drawBitmap(c, 0f, 0f, null);
comboImage.drawBitmap(s, 0f, c.getHeight()+1, null);
return cs;
}

ARUNKUMAR: PDF Reader in Android

ARUNKUMAR: PDF Reader in Android

PDF Reader in Android


This example shows you how to open a pdf file from your activity.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it PDFReader.
2.) You will see some default code into your main.xml and android manifest file.
3.) Download and install any adobe reader application from android market.
4.) Write following into main.xml file:


android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_height="wrap_content" android:text="PDFReader" />

5.) Copy any pdf file onto your sdcard.
6.) Make sure to change the name and path of your pdf file in PDFReaderActivity class. In this example I have used “/sdcard/sample.pdf” . You can change the name of your pdf instead of sample.pdf.
7.) Run for output.

Steps:

1.) Create a project named PDFReader and set the information as stated in the image.

Build Target: Android 2.2
Application Name: PDFReader
Package Name: com.pdftest
Activity Name: PDFReaderActivity
Min SDK Version: 8



2.) Open PDFReaderActivity.java file and write following code there:

package com.pdftest;

import java.io.File;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;

public class PDFReaderActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File file = new File(“/sdcard/sample.pdf”);
PackageManager packageManager = getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType(“application/pdf”);
List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() > 0 && file.isFile()) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, “application/pdf”);
startActivity(intent);
}
}
}

3.) Compile and build the project.
4.) Make sure you have installed pdf viewer application on your device or simulator.

Output





Thursday, February 23, 2012

calculator in android


















main.xml

android:layout_width="fill_parent"
android:layout_height="fill_parent" >
android:id="@+id/txtResultId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:editable="false"
android:gravity="right" />
android:id="@+id/tableId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txtResultId" android:layout_marginTop="60dp">
android:id="@+id/btnNum7Id"
android:text="7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
android:id="@+id/btnNum8Id"
android:text="8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
android:id="@+id/btnNum9Id"
android:text="9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
android:id="@+id/btnDivId"
android:text="÷"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
android:id="@+id/btnNum4Id"
android:text="4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
android:id="@+id/btnNum5Id"
android:text="5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
android:id="@+id/btnNum6Id"
android:text="6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
android:id="@+id/btnMulId"
android:text="×"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
android:id="@+id/btnNum1Id"
android:text="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
android:id="@+id/btnNum2Id"
android:text="2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
android:id="@+id/btnNum3Id"
android:text="3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
android:id="@+id/btnSubId"
android:text="-"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
android:id="@+id/btnNum0Id"
android:text="0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
android:id="@+id/btnClearId"
android:text="C"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
android:id="@+id/btnEqualId"
android:text="="
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
android:id="@+id/btnAddId"
android:text="+"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
calc.java
package com.exam;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class calc extends Activity implements OnClickListener {
private EditText txtResult; // Reference to EditText of result
private int result = 0; // Result of computation
private String inStr = "0"; // Current input string
// Previous operator: '+', '-', '*', '/', '=' or ' ' (no operator)
private char lastOperator = ' ';
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Retrieve a reference to the EditText field for displaying the result.
txtResult = (EditText)findViewById(R.id.txtResultId);
txtResult.setText("0");
// Register listener (this class) for all the buttons
((Button)findViewById(R.id.btnNum0Id)).setOnClickListener(this);
((Button)findViewById(R.id.btnNum1Id)).setOnClickListener(this);
((Button)findViewById(R.id.btnNum2Id)).setOnClickListener(this);
((Button)findViewById(R.id.btnNum3Id)).setOnClickListener(this);
((Button)findViewById(R.id.btnNum4Id)).setOnClickListener(this);
((Button)findViewById(R.id.btnNum5Id)).setOnClickListener(this);
((Button)findViewById(R.id.btnNum6Id)).setOnClickListener(this);
((Button)findViewById(R.id.btnNum7Id)).setOnClickListener(this);
((Button)findViewById(R.id.btnNum8Id)).setOnClickListener(this);
((Button)findViewById(R.id.btnNum9Id)).setOnClickListener(this);
((Button)findViewById(R.id.btnAddId)).setOnClickListener(this);
((Button)findViewById(R.id.btnSubId)).setOnClickListener(this);
((Button)findViewById(R.id.btnMulId)).setOnClickListener(this);
((Button)findViewById(R.id.btnDivId)).setOnClickListener(this);
((Button)findViewById(R.id.btnClearId)).setOnClickListener(this);
((Button)findViewById(R.id.btnEqualId)).setOnClickListener(this);
}
// On-click event handler for all the buttons
@Override
public void onClick(View view) {
switch (view.getId()) {
// Number buttons: '0' to '9'
case R.id.btnNum0Id:
case R.id.btnNum1Id:
case R.id.btnNum2Id:
case R.id.btnNum3Id:
case R.id.btnNum4Id:
case R.id.btnNum5Id:
case R.id.btnNum6Id:
case R.id.btnNum7Id:
case R.id.btnNum8Id:
case R.id.btnNum9Id:
String inDigit = ((Button)view).getText().toString();
if (inStr.equals("0")) {
inStr = inDigit; // no leading zero
} else {
inStr += inDigit; // accumulate input digit
}
txtResult.setText(inStr);
// Clear buffer if last operator is '='
if (lastOperator == '=') {
result = 0;
lastOperator = ' ';
}
break;
// Operator buttons: '+', '-', '*', '/' and '='
case R.id.btnAddId:
compute();
lastOperator = '+';
break;
case R.id.btnSubId:
compute();
lastOperator = '-';
break;
case R.id.btnMulId:
compute();
lastOperator = '*';
break;
case R.id.btnDivId:
compute();
lastOperator = '/';
break;
case R.id.btnEqualId:
compute();
lastOperator = '=';
break;
// Clear button
case R.id.btnClearId:
result = 0;
inStr = "0";
lastOperator = ' ';
txtResult.setText("0");
break;
}
}
// User pushes '+', '-', '*', '/' or '=' button.
// Perform computation on the previous result and the current input number,
// based on the previous operator.
private void compute() {
int inNum = Integer.parseInt(inStr);
inStr = "0";
if (lastOperator == ' ') {
result = inNum;
} else if (lastOperator == '+') {
result += inNum;
} else if (lastOperator == '-') {
result -= inNum;
} else if (lastOperator == '*') {
result *= inNum;
} else if (lastOperator == '/') {
result /= inNum;
} else if (lastOperator == '=') {
// Keep the result for the next operation
}
txtResult.setText(String.valueOf(result));
}
}

Wednesday, February 22, 2012

Web Service in Android

In this post I'm going to illustrate how we can access web service in Android using ksoap2-android project that provides a lightweight and efficient SOAP library for the Android platform.

You can download the jar file from following link;
http://ksoap2-android.googlecode.com/svn/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/2.5.8/ksoap2-android-assembly-2.5.8-jar-with-dependencies.jar

Here is the sample code that illustrate SOAP web service call. I have access a live web service ConvertWeight from http://www.webserviceX.NET/ which convert weight from one unit to another. I have set "envelope.dotNet = true;" in Line 53 since I'm accessing .NET web service. You can comment that line if your web service is not .NET one.
package com.sencide;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class AndroidWebService extends Activity {

private final String NAMESPACE = "http://www.webserviceX.NET/";
private final String URL = "http://www.webservicex.net/ConvertWeight.asmx";
private final String SOAP_ACTION = "http://www.webserviceX.NET/ConvertWeight";
private final String METHOD_NAME = "ConvertWeight";

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

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

String weight = "3700";
String fromUnit = "Grams";
String toUnit = "Kilograms";

PropertyInfo weightProp =new PropertyInfo();
weightProp.setName("Weight");
weightProp.setValue(weight);
weightProp.setType(double.class);
request.addProperty(weightProp);

PropertyInfo fromProp =new PropertyInfo();
fromProp.setName("FromUnit");
fromProp.setValue(fromUnit);
fromProp.setType(String.class);
request.addProperty(fromProp);

PropertyInfo toProp =new PropertyInfo();
toProp.setName("ToUnit");
toProp.setValue(toUnit);
toProp.setType(String.class);
request.addProperty(toProp);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
Log.i("myApp", response.toString());

TextView tv = new TextView(this);
tv.setText(weight+" "+fromUnit+" equal "+response.toString()+ " "+toUnit);
setContentView(tv);

} catch (Exception e) {
e.printStackTrace();
}
}
}
You have to add INTERNET permission to AndroidManifest.xml as follows;


<?xml version="1.0" encoding="utf-8"?>
      package="com.sencide"
      android:versionCode="1"
      android:versionName="1.0">
 
    <application android:icon="@drawable/icon" android:label="@string/app_nam">
        <activity android:name=".AndroidWebService"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
    </application>
     
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>



You can download the source code of above project (Password:sara). 

I have updated my previous code in the post Android Login Screen Using HttpClientto authenticate users from web service. There I have illustrate how you can start new activity if the login is successful

You can download the updated source code with sample .NET web service application (Password:sara).