Live Folder Example : Getting Saved Contacts to livefolder

Following Example Demonstrates how to create Live Folder and getting contacts to this live foler.

Live Folder is useful in many ways you can bring live RSSFeed and any Builtin / custom content provider
data to live folder

activity_main.xml

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

LiveFolderActivity.java

package com.example.livefolder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.provider.ContactsContract;
import android.provider.LiveFolders;
import android.view.Menu;

public class MainActivity extends Activity {

	//public static final Uri CONTENT_URI = Uri.parse("content://contacts/");
	public static final Uri CONTENT_URI = People.CONTENT_URI;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final Intent intent = getIntent();
        final String action = intent.getAction();

        if (LiveFolders.ACTION_CREATE_LIVE_FOLDER.equals(action)) {
            setResult(RESULT_OK, createLiveFolder(getApplicationContext(), CONTENT_URI,
                    "Contacts", R.drawable.ic_launcher));
        } else {
            setResult(RESULT_CANCELED);
        }

        finish();
    }

    private static Intent createLiveFolder(Context context, Uri uri, String name, int icon) {
        final Intent intent = new Intent();

        intent.setData(uri);
        intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME, name);
        intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON,
                Intent.ShortcutIconResource.fromContext(context, icon));
        intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE, LiveFolders.DISPLAY_MODE_LIST);

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

AndroidManifest.xml file

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />
    <uses-permission android:name="android.permission.READ_CONTACTS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.livefolder.MainActivity"
            android:label="@string/app_name" >
                <intent-filter>
             <action android:name="android.intent.action.CREATE_LIVE_FOLDER"/>
             <category android:name="android.intent.category.DEFAULT" />
       </intent-filter>
            
                    </activity>
    </application>

</manifest>

LiveFolder1

LiveFolder2

LiveFolder3

LiveFolder4

Insert, Update and Delete Contact Using ContentProvider

Following Code demonstrates insert, update and delete contact using Content provider

activity_main.xml

<RelativeLayout 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"
    tools:context=".MainActivity" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imageView1"
        android:layout_toLeftOf="@+id/imageView1"
        android:text="Button" />

</RelativeLayout>

ContactIUD.java

package com.example.contactscp5a;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

	Cursor c;
	int count=0;
	Uri u,pu;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		createContact("mca5a","1111");
		
		Button b = (Button)findViewById(R.id.button1);
		b.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
                                  createContact("ASDFG");
				//deleteContact("ASDFG");
				//updateContact("pqr");
			}
		});
		
	}
	private void updateContact(String nm)
	{
		ContentValues cv = new ContentValues();
		//cv.put(People.NAME, "mca5a");
		cv.put(People.Phones.TYPE, People.TYPE_HOME);
		cv.put(People.NUMBER, "89898");
	//	Cursor c = managedQuery(People.CONTENT_URI, new String[]{People.NAME}, People.NAME+"=?", new String[]{nm}, null);
		//Toast.makeText(getApplicationContext(), c.getCount()+"", 50).show();
		getContentResolver().update(pu, cv, 
				null,null);
	}
	private void createContact(String nm,String phno)
	{
		ContentValues cv = new ContentValues();
		cv.put(People.NAME, nm);
		u = getContentResolver().insert(People.CONTENT_URI, cv);
		pu = Uri.withAppendedPath(u, People.Phones.CONTENT_DIRECTORY);
		cv.clear();
		cv.put(People.Phones.TYPE, People.TYPE_MOBILE);
		cv.put(People.NUMBER,phno);
		u = getContentResolver().insert(pu, cv);
	}
	private void deleteContact(String nm)
	{
		getContentResolver().delete(People.CONTENT_URI, 
				People.NAME+"=?", new String[]{nm});
		////getContentResolver().delete(u, null, null);
	}

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

}