Saturday 24 December 2011

ImageSwitcher View Example in Android

ImageSwicher is an View and it provides smooth switching between 2 images and provide animation between images.
Here Below Example shows how to create ImageSwicher view Example.

UI Layout(imageswitcher_layout.xml)

Create layout in res/layout folder and copy below code

<?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" >

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

    <ImageSwitcher
        android:id="@+id/ImageSwitcher"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ImageSwitcher>

</LinearLayout>

Android Activity(ImageSwitcherView.java)

package com.androidsurya.imageswicherexample;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class ImageSwitcherView extends Activity implements ViewFactory {
Integer pictures[] = { R.drawable.city, R.drawable.citybridge,
R.drawable.school, R.drawable.friends };
ImageSwitcher imageSwitcher;
ImageView imageView;
Gallery gallery;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imageswitcher_layout);
imageSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher);
imageSwitcher.setFactory(this);
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
gallery = (Gallery) findViewById(R.id.Gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
imageSwitcher.setImageResource(pictures[position]);
}
});
}

public class ImageAdapter extends BaseAdapter {

private Context ctxObj;

public ImageAdapter(Context c) {
ctxObj = c;
}

@Override
public int getCount() {

return pictures.length;
}

@Override
public Object getItem(int arg0) {

return arg0;
}

@Override
public long getItemId(int arg0) {

return arg0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
imageView = new ImageView(ctxObj);
imageView.setImageResource(pictures[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 150));
return imageView;
}

}

@Override
public View makeView() {
imageView = new ImageView(this);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
imageView.setBackgroundColor(0xFF000000);
return imageView;
}
}

Register Activity in Android Manifest File

 <activity
            android:name="com.androidsurya.imageswicherexample.ImageSwitcherView"
            android:label="@string/app_name" >

Output Screenshot










For More information about : Android Developers site

Saturday 17 December 2011

Android - Adding contacts programmatically example in Android

Here below method addContact() is used to insert now contact into android contact list.

This method takes parameters of

displayname,
homenumber,
mobilenumber,
worknumber,
homeemail,
workemail,
companyname,
jobtitle,

Note :Before inserting don't forget add permission in Manifest file

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

Example code how we call that method.

addContact(getApplicationContext(),"Surya Bondada","+919999999999","+919962174332","9999999100","Androidsurya","Android developer");


Method:addContact( ) add new contact to contact list

void addContact(Context ctx, String displayname, String homenumber,
String mobilenumber, String worknumber, String homeemail,
String workemail, String companyname,String jobtitle) {
String DisplayName = displayname;
String MobileNumber = homenumber;
String HomeNumber = mobilenumber;
String WorkNumber = worknumber;
String homeemailID = homeemail;
String workemailID = workemail;
String company = companyname;
String jobTitle = jobtitle;
ArrayList<ContentProviderOperation> contentProviderOperation = new
ArrayList<ContentProviderOperation>();

contentProviderOperation.add(ContentProviderOperation
.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());

// ------------------------------------------------------ Names
if (DisplayName != null) {
contentProviderOperation.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
DisplayName).build());
}

// ------------------------------------------------------ Mobile Number
if (MobileNumber != null) {
contentProviderOperation.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
MobileNumber)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.build());
}

// ------------------------------------------------------ Home Numbers
if (HomeNumber != null) {
contentProviderOperation.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
HomeNumber)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_HOME)
.build());
}

// ------------------------------------------------------ Work Numbers
if (WorkNumber != null) {
contentProviderOperation.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
WorkNumber)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
.build());
}

// ------------------------------------------------------ workEmail
if (workemailID != null) {
contentProviderOperation.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA,
workemailID)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.build());
}
// ------------------------------------------------------ homeEmail
if (homeemailID != null) {
contentProviderOperation.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA,
homeemailID)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_HOME)
.build());
}
// ------------------------------------------------------ Organization
if (!company.equals("") && !jobTitle.equals("")) {
contentProviderOperation.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.Organization.COMPANY,
company)
.withValue(
ContactsContract.CommonDataKinds.Organization.TYPE,
ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
.withValue(
ContactsContract.CommonDataKinds.Organization.TITLE,
jobTitle)
.withValue(
ContactsContract.CommonDataKinds.Organization.TYPE,
ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
.build());
}
// Asking the Contact provider to create a new contact
try {
ctx.getContentResolver()
.applyBatch(ContactsContract.AUTHORITY, contentProviderOperation);
} catch (Exception e) {
e.printStackTrace();
//show exception in toast
Toast.makeText(ctx, "Exception: " + e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}

If any queries regarding this method just Contact me.Thanking you

Friday 9 December 2011

Android - How can I check if a view is visible or not in Android ?

By using getVisibility( ) method on View we can get status of view.when we apply getVisibility( ) on view this method return any one of the below state.

View.VISIBLE - The view is visible.

View.INVISIBLE - The view is invisible, but any spacing it would normally take up will still be used.
 Its "invisible"

View.GONE -The view is gone, you can't see it and it doesn't take up the "spot".

Here the below code is for visibility status of view finding.

imageView is an view(ImageView obj)

if (imageView.getVisibility() == View.VISIBLE) {

    // Its visible

} else {

    // Either gone or invisible

}

Tuesday 6 December 2011

Apply Color to ImageView (PNG) file {foreground color}in Andriod.....

For apply color to an image in andriod....i search lot of blogs and different websites.only solution for apply background color. but thier is no solution for apply foreground color.. .but i try and find the solution for that...
Here below code is  apply color to image.. Foreground color..apply .

Main.xml(GUI)
<?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:text="Apply Color to Png image "
  android:layout_height="wrap_content"
  android:id="@+id/select"
  android:layout_width="fill_parent">
  </Button>
 <ImageView android:src="@drawable/icon"
 android:layout_height="wrap_content"
 android:id="@+id/imageView1"
 android:layout_width="wrap_content"
 ></ImageView>

</LinearLayout>







Activity class:
 public class ImageColorexample extends Activity {
    /** Called when the activity is first created. */
      ImageView image;
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
  image=(ImageView)findViewById(R.id.imageView1);
      Button b=(Button)findViewById(R.id.select);
         b.setOnClickListener(new OnClickListener()          
            @Override
            public void onClick(View v) {

//code to apply foregroundcolor to png image...
            image.setColorFilter(Color.rgb(123,73,122),
                          android.graphics.PorterDuff.Mode.MULTIPLY );

       
            }
        });
    }
}


Screen short:
Before applying color



When user click on Button..Foreground color will be apply to image...






These below methods are use full to apply color ..






Monday 5 December 2011

Android Tools for Developers

The Android SDK comes with a robust set of tools to help developers design, develop, test, and publish quality Android applications. In this article, we discuss 10 of the most common tools.

Android Tool 1: Eclipse ADT

Although Eclipse is not the only Java development environment that can be used to develop Android applications, it is by far the most popular. This is partially due to its cost (free!) but mostly due the strong integration of the Android tools with Eclipse. This integration is achieved with the Android Development Tools (ADT) plug-in for Eclipse, which can be downloaded from the Android website.

Eclipse + ADT

Android Tool 2: The SDK and AVD Manager


This tool serves a number of important functions. It manages the different versions of the Android SDKs (build targets) that you can develop for, as well as third-party add-ons, tools, devices drivers, and documentation. Its second function is to manage the Android Virtual Device configurations (AVDs) you use to configure emulator instances.

Android SDK + AVD

Android Tool 3: Android Debug Bridge(ADB)


The Android Debug Bridge (adb) connects other tools with the emulator and devices. Besides being critical for the other tools (most especially the Eclipse ADT plug-in) to function, you can use it yourself from the command line to upload and download files, install and uninstall packages, and access many other features via the shell on the device or emulator.

Android Tool 4: Dalvik Debug Monitor Server(DDMS)


The Dalvik Debug Monitor Server (DDMS), whether it's accessed through the standalone application or the Eclipse perspective with the same name, provides handy features for inspecting, debugging, and interacting with emulator and device instances. You can use DDMS to inspect running processes and threads, explore the file system, gather heap and other memory information, attach debuggers, and even take screenshots. For emulators, you can also simulate mock location data, send SMS messages, and initiate incoming phone calls.
Dalvik Debug Monitor Server

Android Tool 5: The Android Emulator and Real Devices


Once you have begun to develop an app, it's important to test it on the appropriate device targets. The emulator can be used in conjunction with AVDs to simulate device targets. That said, testing on real physical devices is essential for complete test coverage. The emulator, while powerful, cannot emulate the idiosyncrasies of individual devices. Therefore, a solid test plan must incorporate both emulators and real devices. After all, your users won't be running your app on an emulator, will they?

Android Emulator

Android Tool 6: LogCat


LogCat is the name of the Android logging system. LogCat data is accessible from within Eclipse, as well as through adb, and provides helpful diagnostic information about events on the system. As a developer, you can enable your applications to log debugging and diagnostic information to LogCat as well. Logging from within an application is about as easy as a printf() statement.

Android Tool 7: The Hierarchy Viewer

The Hierarchy Viewer, whether it's access through the standalone application or the relatively new Eclipse perspective, is used to see how your layouts and screens resolve at runtime. It provides a graphical representation of the layout and view hierarchy of your application and can be used to diagnose layout problems.
Android Hierarchy Viewer

Android Tool 8: Draw 9-Patch

When it comes to graphics design, the Draw 9-patch tool comes in handy. This tool allows you to convert traditional PNG graphic files into stretchable graphics that are more flexible and efficient for mobile development use. The tool simplifies the creation of NinePatch files in an environment that instantly displays the results.


Draw 9-patch

Android Tool 9: The Monkey Test Tools


The Monkey Test Tools, including the Monkey exerciser tool and the monkeyrunner tool, are a pair of applications that can be used to automate application testing. The Monkey exerciser randomly sends events to your application for stress testing purposes. The monkeyrunner tool is a scripting library that can be used for automated testing and checking of the results via screenshots using Python scripts.

Android Tool 10: ProGuard

ProGuard, which is now part of the typical Android build process, provides developers with a straightforward way to increase protection of their intellectual property after publication. The ProGuard toolcan be configured to obfuscate the resulting binaries to make them difficult to reverse engineer. The ProGuard tool can also be used to optimize the size of the resulting binary, reducing the overall package size and speeding delivery to your users.

Sunday 30 October 2011

Android Notification Example

Here below example show notification and cancel notification.

First i created application with 2 buttons one is for show and other one is for cancel.
when user click one show button notification will be appear and when user click on
cancel button cancel the notification.

Android UI layout(main.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/show"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Show Notification" />

    <Button
        android:id="@+id/cancel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Cancel Notification" />

</LinearLayout>

Android Activity(MainActivity.java)

package com.androidsurya.notification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
NotificationManager notificationManager;
Notification notificationObj;
PendingIntent pendingIntent;
String notificationTitle = "Notification Titile!";
String notificationText = "Notification text display";

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

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
notificationManager = (NotificationManager)                    getSystemService(Context.NOTIFICATION_SERVICE);
notificationObj = new Notification(R.drawable.ic_launcher,
"Show Notification !", System.currentTimeMillis());

pendingIntent = PendingIntent.getActivity(MainActivity.this, 0,
intent, Intent.FLAG_ACTIVITY_NEW_TASK);
notificationObj.defaults |= Notification.DEFAULT_SOUND;
notificationObj.setLatestEventInfo(getApplicationContext(),
notificationTitle, notificationText, pendingIntent);
notificationManager.notify(0, notificationObj);

}
});
Button cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
notificationManager.cancel(0);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

Register Activity in Android Manifest file

 <activity
            android:name="com.androidsurya.notification.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

Output Screenshot:






































Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...