Tuesday 16 April 2013

SetError Example In Android



res/layout/seterror.xml


Step 1: seterror.xml :




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

    <TextView
        android:id="@+id/textViewPhoneNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="To"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextTo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textViewSubject"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Subject"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextSubject"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </EditText>

    <TextView
        android:id="@+id/textViewMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Message"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextMessage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:inputType="textMultiLine"
        android:lines="5" />

    <Button
        android:id="@+id/buttonSend"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Send" />
</LinearLayout>



Step 2: SetError.java :



public class SetError extends Activity implements OnClickListener{


Button buttonSend;
    
EditText txtTo;

    
EditText txtSubject;

    
EditText txtMessage;




@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.contactus);
 
buttonSend = (Button) findViewById(R.id.buttonSend);


txtTo = (EditText) findViewById(R.id.editTextTo);




txtSubject = (EditText) findViewById(R.id.editTextSubject);

txtMessage = (EditText) findViewById(R.id.editTextMessage);
        

buttonSend.setOnClickListener(new OnClickListener() {

           @Override
           public void onClick(View v) {


String to = txtTo.getText().toString();
                 

String subject = txtSubject.getText().toString();


             

String message = txtMessage.getText().toString();


                  

if (to.equals("")) {

txtTo.setError("You forgot to enter the To");

}
else if (subject.equals("")){



 txtTo.setError(null);

                
                txtSubject.setError("You forgot to enter the Subject");
                





}

else if (message.equals("")) {

txtSubject.setError(null);
                

               txtMessage.setError("You forgot to enter the Message");


}

 else{


Toast.makeText(getApplicationContext(),"
Message Send Succusefully",

Toast.LENGTH_SHORT).show();

}
}

});
}


@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}



}

Screen Shot:









    Happy Programming ....

Cheers









Sunday 14 April 2013

WebView Example in Android

A View that displays the web pages

res/layout/webview.xml


Step 1: webview.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" >


  <WebView
    android:id="@+id/web"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>
</LinearLayout>


Step 2: WebView.java :




public class WebView extends Activity {
WebView browser;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        browser=(WebView)findViewById(R.id.web);     
        browser.getSettings().setJavaScriptEnabled(true);
        browser.getSettings().setUseWideViewPort(true);
        browser.getSettings().setDefaultFontSize(20);
      // browser.loadUrl("http://www.google.com");
       
       browser.loadUrl("file:///android_asset/hello.html");



}
}


Step 3: Place the hello.html in assets


Step 4: Add internet permission to manifest file :


<uses-permission android:name="android.permission.INTERNET" />









Sending E-mail in Android

Composing a mail and send a mail with the default email client on device.


contactus.xml :


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

    <TextView
        android:id="@+id/textViewTo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="To"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextTo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textViewSubject"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Subject"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextSubject"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </EditText>

    <TextView
        android:id="@+id/textViewMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Message"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextMessage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:inputType="textMultiLine"
        android:lines="5" />

    <Button
        android:id="@+id/buttonSend"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Send" />
</LinearLayout>

ContactUs.java:


package com.apc.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ContactUs extends Activity implements OnClickListener{


Button buttonSend;
     EditText txtTo;
     EditText txtSubject;
     EditText txtMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.contactus);
 
buttonSend = (Button) findViewById(R.id.buttonSend);
       txtTo = (EditText) findViewById(R.id.editTextTo);
       txtSubject = (EditText) findViewById(R.id.editTextSubject);
       txtMessage = (EditText) findViewById(R.id.editTextMessage);
       
       buttonSend.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
           
           
            String to = txtTo.getText().toString();
           
            String subject = txtSubject.getText().toString();
           
            String message = txtMessage.getText().toString();
               Intent email = new Intent(Intent.ACTION_SEND);
               email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });                
               email.putExtra(Intent.EXTRA_SUBJECT, subject);
               email.putExtra(Intent.EXTRA_TEXT, message);                
               email.setType("message/rfc822");
               startActivity(Intent.createChooser(email,
                           "Choose an Email client :"));
             
                    
                }
       });

}

boolean isEmailValid(CharSequence email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
       
@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}



}


Manifest file :

Place the internet permission in manifest file

 <uses-permission android:name="android.permission.INTERNET"/>


Cheers :-)





















Tuesday 2 April 2013

Integrating ZXing-BarCode Scanner in your Android Application



Intigrating ZXing-BarCodeScanner In to your Android Application


Step One: Obtain the zxing src code

The src can be found at http://code.google.com/p/zxing/source/browse/trunk
Specifically you only need the android/ and the core/ projects. Use svn to checkout these to your local hard-drive.

Step Two: Build zxing core using Apache Ant

You will need to build the core project into a jar file using apache ant (download from here http://ant.apache.org/ivy/download.cgi).
Using a shell or cmd prompt navigate to the root directory of the downloaded zxing src and execute ”ant -f core/build.xml”.
This will produce a file core/core.jar which we will use in the next step.

Step Three: Build ZXing Android using Eclipse

Create a New Android Project (File –> New –> Android Project).
Set the project name to ZXing (or similar).
Select the “Create project from existing source” radio button
Click “Browse” and navigate to the android project that you downloaded from zxing and click “OK”
Select “Finish”


The project will not currently build. We need to add the core.jar file (that we produced in the previous step) into our project.
Right-click on ZXing project –> properties –> Java Build Path –> Add External Jars –> Navigate to and select core.jar –> Open –> OK.

Actually, while we’re here we should do one more very important thing!
Right-click on ZXing project –> properties –> Android –> Scroll down and check/tick the “Is Library” checkbox –> OK.

Step 4: Include ZXing Android into your project.

Within Eclipse,  Right-click on YOURPROJECTNAMEHERE project –> properties –>Android –> Scroll down to Libraries section –> Click Add –> Select ZXing (which should appear as an option as a result of completing previous step).

Next, in some trigger function e.g. button press within your code you should add:

Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
In the same activity you’ll need the following to retrieve the results:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

  if (requestCode == 0) {
      if (resultCode == RESULT_OK) {
         String contents = intent.getStringExtra("SCAN_RESULT");
         String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
         // Handle successful scan
      } else if (resultCode == RESULT_CANCELED) {
         // Handle cancel
      }
   }
}

Almost there! One of the current limitations of Android Library projects is that it will not pull anything from AndroidManifest.xml into your project.

So if we try to invoke the above code we will receive a runtime exception because your Android app has no idea how to handle the scan intent.
To fix this you just need to copy the following into your AndroidManifest.xml:

<activity android:name="com.google.zxing.client.android.CaptureActivity"
   android:screenOrientation="landscape"
   android:configChanges="orientation|keyboardHidden"
   android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
   android:windowSoftInputMode="stateAlwaysHidden">
   <intent-filter>
      <action android:name="android.intent.action.MAIN"/>
      <category android:name="android.intent.category.DEFAULT"/>
   </intent-filter>
   <intent-filter>
      <action android:name="com.google.zxing.client.android.SCAN"/>
      <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>
And as Columbo would say, “Just one more thing!”.
 Add this permission to the top of your AndroidManifest.xml:

<uses-permission android:name="android.permission.CAMERA"/>

EDIT: You need to do yet one more thing!
You need to add the core.jar (produced in Step two) to your new project
(Right-click your project –> Properties –> Java Build Path –> Add External JARS… –> Select core.jar –> OK).


Thanks

Wednesday 27 March 2013

Integrating ZXing-QR in your Android Application


Follow the steps to integrating ZXing-QRSCan in your app:


ZXing is one of the most popular barcode scanning applications on the market.
They make it very easy for you to integrate into your application
via an intent but this means that your users must manually install the application from the market.


Step 1:  Obtain the zxing source code:
      
      Also you can find the full source code at this url

     
Step 2 :  Build zxing core using Apache Ant:
 
      You will need to build the core project into a jar file using apache ant
    (download from here http://ant.apache.org/ivy/download.cgi).
      Using a shell or cmd prompt navigate to the root directory of
      the downloaded zxing src and execute “ant -f core/build.xml”.
      This will produce a file core/core.jar which
      we will use in the next step.  

Step 3:     Build ZXing Android using Eclipse:
     
      Create a New Android Project (File –> New –> Android Project).
      Set the project name to ZXing (or similar).
      Select the “Create project from existing source” radio button
      Click “Browse” and navigate to the android project that you downloaded from
      zxing  and click “OK” Select “Finish”
     
       The project will not currently build. We need to add the core.jar file (that we
  produced    in the previous step) into our project. Right-click on ZXing
  project –> properties –> Java Build Path –> Add External Jars –> Navigate to
  and select core.jar –> Open –> OK.

  Actually, while we’re here we should do one more very important thing!
  Right-click on ZXing project –> properties –> Android –> Scroll down and
  check/tick the “Is Library” checkbox –> OK

Step 4:     Include ZXing Android into your project.
 
      Within Eclipse,  Right-click on YOURPROJECTNAMEHERE project –>
      properties –>Android –> Scroll down to Libraries section –> Click Add –>
      Select ZXing (which should appear as an option as a result of completing
      previous step).

Source Code:
     
     
1. AndroidManifest.xml

   <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidpeoplesconnect.app"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.CAMERA" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".QRCodeActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.google.zxing.client.android.CaptureActivity"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:windowSoftInputMode="stateAlwaysHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.google.zxing.client.android.SCAN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

  </manifest>
 
 
 
2. QRCodeActivity.java

 package com.androidpeoplesconnect.app;

import com.google.zxing.client.android.CaptureActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class QRCodeActivity extends Activity {
 /** Called when the activity is first created. */
 Button b1;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  b1 = (Button) findViewById(R.id.submit);
  b1.setOnClickListener(new OnClickListener() {

   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(TestQRCodeActivity.this,
      CaptureActivity.class);
    // Intent intent = new
    // Intent("com.google.zxing.client.android.SCAN");
    intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
    startActivityForResult(intent, 0);
   }
  });

 }

 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
  if (requestCode == 0) {
   if (resultCode == 1) {
    // Handle successful scan
    String capturedQrValue = intent.getStringExtra("RESULT");
    //String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
    Toast.makeText(TestQRCodeActivity.this,
      "Scan Result:" + capturedQrValue, Toast.LENGTH_SHORT)
      .show();

   } else if (resultCode == RESULT_CANCELED) {
    // Handle cancel
   }
  } else {

  }
 }
}