Saturday, July 28, 2012

Splash Screen Demo in android | Splash Page Example in Android


Hello friends,

Today I am going to share very simple code for splash page in android. Splash screen is the first screen in any application. We can add progress bar also on splash page. This is a smple demo for splash page navigation from splash page to main activity page. Hope it will help you..

Screen shot:


1-mainifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.splash.screen.demo"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="15" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".SplashScreenDemo" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".NextActivity"/>
    </application>
</manifest>

2-SplshScreenDemo.java file
package com.splash.screen.demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;

public class SplashScreenDemo extends Activity {
       private Thread splashThread;

       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
             
              // The thread to wait for splash screen events
              splashThread = new Thread() {
                     @Override
                     public void run() {
                           try {
                            synchronized (this) {
                             // Wait given period of time or exit on touch
                             wait(3500);
                                  }
                           } catch (InterruptedException ex) {
                           }

                           finish();

                           // Run next activity
                           Intent intent = new Intent();
                           intent.setClass(SplashScreenDemo.this,NextActivity.class);
                           startActivity(intent);
                     }
              };

              splashThread.start();
       }

       /**
        * Processes splash screen touch events
        */
       @Override
       public boolean onTouchEvent(MotionEvent evt) {
              if (evt.getAction() == MotionEvent.ACTION_DOWN) {
                     synchronized (splashThread) {
                           splashThread.notifyAll();
                     }
              }
              return true;
       }
}
4- NextActivity.java file
package com.splash.screen.demo;

import android.app.Activity;
import android.os.Bundle;

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

}
3-main.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_gravity="center"
        android:layout_marginTop="50sp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Loading..."
       android:layout_gravity="center"/>
</LinearLayout>

4-screen_next.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>


Auto complete demo in android | Searching in Edit text in Android


1-manifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="auto.complete"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="15" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".AutoCompleteDemoActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


2- AutoCompleteDemoActivity.java file
package auto.complete;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

public class AutoCompleteDemoActivity extends Activity implements TextWatcher {
       TextView textViewSelection;
       AutoCompleteTextView autoComplete;
       String[] fruits = { "apple", "banana", "graps", "orrange", "berry" };

       @Override
       public void onCreate(Bundle icicle) {
              super.onCreate(icicle);
              setContentView(R.layout.main);
              textViewSelection = (TextView) findViewById(R.id.selection);
              autoComplete = (AutoCompleteTextView) findViewById(R.id.edit);
              autoComplete.addTextChangedListener(this);
              autoComplete.setAdapter(new ArrayAdapter<String>(this,
                           android.R.layout.simple_list_item_1, fruits));
       }

       public void onTextChanged(CharSequence s, int start, int before, int count) {
              textViewSelection.setText(autoComplete.getText());
       }

       public void beforeTextChanged(CharSequence s, int start, int count,
                     int after) {
              // needed for interface, but not used
       }

       @Override
       public void afterTextChanged(Editable s) {
              // TODO Auto-generated method stub
             
       }      }


3- main.xml file
<?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:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<AutoCompleteTextView android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="3"/>

</LinearLayout>

How to capture image from camera in android | Camera Capture code in android


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

    <uses-sdk android:minSdkVersion="8" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".CameraDemoActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

2- CameraDemoActivity.java

package com.camera.demo;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class CameraDemoActivity extends Activity {
       protected static final int CAMERA_REQUEST = 1;
       ImageView imgView;
       Button btnCamera;

       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
              imgView = (ImageView) findViewById(R.id.imageView1);
              btnCamera = (Button) findViewById(R.id.btn_camera);
              btnCamera.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View v) {
                           // TODO Auto-generated method stub
                           // ******** code for take image

                           Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                           intent.putExtra(MediaStore.EXTRA_OUTPUT,
                                         MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
                           try {

                                  intent.putExtra("return-data", true);
                                  startActivityForResult(intent, CAMERA_REQUEST);

                           } catch (ActivityNotFoundException e) {
                                  // Do nothing for now
                           }

                     }
              });
       }

       protected void onActivityResult(int requestCode, int resultCode, Intent data) {
              if (requestCode == CAMERA_REQUEST) {

                     Bundle extras = data.getExtras();
                     if (extras != null) {
                           Bitmap photo = extras.getParcelable("data");
                           // display image in ImageView.
                           imgView.setImageBitmap(photo);
                           // saveBitmapToFile("/sdcard/crop/cropped_img.jpg", photo);
                     }
              }

       }

}


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

    <Button
        android:id="@+id/btn_camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Take image" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

</LinearLayout>