Monday, July 16, 2012

To Get Your phone contact and display in list



Click Here To download Source code

Package Name  :  selva.contact

Project Name    Contact

Version             :  1.5 ( Supports 1.5 and above versions )

main.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">
    <ListView
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:stackFromBottom="false"
    android:transcriptMode="normal"/>
    <TextView
    android:id="@+id/contactName"
    android:textStyle="bold"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
    <TextView
    android:id="@+id/contactID"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>




ContactActivity.java



package selva.contact;

import android.os.Bundle;
import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.widget.SimpleCursorAdapter;
public class ContactActivity extends ListActivity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Uri allContacts = Uri.parse("content://contacts/people");
        Cursor c = managedQuery(allContacts, null, null, null, null);
        String[] columns = new String[] { ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.Contacts._ID };
        int[] views = new int[] {R.id.contactName, R.id.contactID};
        SimpleCursorAdapter adapter =new SimpleCursorAdapter(this, R.layout.main, c, columns, views);
        this.setListAdapter(adapter);
}
}




AndroidManifest.xml


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

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

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

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

</manifest>



OUTPUT:

click call button in emulator











click contacts tab
















































select New contact














































Fill the Details in New contact Form













































Click Done Button














































Contacts are appeared in screen







































Now run the program































Click Here To download Source code


Creating Text File and using in Android



Click Here to download source code

Package Name   :   selva.file

Project Name     :   File2

Version              :   1.5 ( Support 1.5 and above versions)

main.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" >
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Please enter some text"/>
    <EditText
    android:id="@+id/txtText2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
    <Button
    android:id="@+id/btnLoad"
    android:text="Load"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>



(create raw folder under res) 
res-->raw-->rightclick-->new-->file-->textfile 

















 
textfile.txt

hi.. this is sample file content..


  File2Activity.java


 package selva.file;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;


public class File2Activity extends Activity
{
    /** Called when the activity is first created. */
    private EditText textBox;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
       
    textBox = (EditText) findViewById(R.id.txtText2);
    Button loadBtn = (Button) findViewById(R.id.btnLoad);
   
loadBtn.setOnClickListener(new View.OnClickListener()
{
    public void onClick(View v)
    {
        InputStream  is=File2Activity.this.getResources().openRawResource(R.raw.textfile);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String str=null;
        try {
        while ((str = br.readLine()) != null)
        {
           
            textBox.setText(str);   
        }
   
        is.close();
        br.close();
        }
        catch (IOException e)
        {
        e.printStackTrace();
        }
    }
}); 
}
}


OUTPUT:


























 click Load button










Click Here to download source code


Creating TextFile Dynamically and using in Android


Click Here to download source code

Package Name   :   selva.file

Project Name     :   File1

Version                   :   1.5 ( Supports 1.5 and above versions)


 main.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" >
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Please enter some text"/>
    <EditText
    android:id="@+id/txtText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
    <Button
    android:id="@+id/btnSave"
    android:text="Save"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
    <EditText
    android:id="@+id/txtText2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
    <Button
    android:id="@+id/btnLoad"
    android:text="Load"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>


 File1Activity.java

package selva.file;

import android.app.Activity;
import android.os.Bundle;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class File1Activity extends Activity
{
    private EditText textBox,textBox1;
    private static final int READ_BLOCK_SIZE = 100;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textBox = (EditText) findViewById(R.id.txtText1);
        textBox1 = (EditText) findViewById(R.id.txtText2);
        Button saveBtn = (Button) findViewById(R.id.btnSave);
        Button loadBtn = (Button) findViewById(R.id.btnLoad);
        saveBtn.setOnClickListener(new View.OnClickListener()
            {
            public void onClick(View v)
            {
                String str = textBox.getText().toString();
                try
                {
                    FileOutputStream fOut =openFileOutput("textfile.txt",MODE_WORLD_READABLE);
                    OutputStreamWriter osw = new OutputStreamWriter(fOut);
                    //---write the string to the file---
                    osw.write(str);
                    osw.flush();
                    osw.close();
                    //---display file saved message---
                    Toast.makeText(getBaseContext(),"File saved successfully!",Toast.LENGTH_SHORT).show();
                    //---clears the EditText---
                    textBox.setText("");
                }
                catch (IOException ioe)
                {
                    ioe.printStackTrace();
                }
            }
            });
        loadBtn.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                try
                {
                    FileInputStream fIn =openFileInput("textfile.txt");
                    InputStreamReader isr = new InputStreamReader(fIn);
                    char[] inputBuffer = new char[READ_BLOCK_SIZE];
                    String s = "";
                    int charRead;
                    while ((charRead = isr.read(inputBuffer))>0)
                    {
                        //---convert the chars to a String---
                        String readString =String.copyValueOf(inputBuffer, 0,charRead);
                        s += readString;
                        inputBuffer = new char[READ_BLOCK_SIZE];
                    }
                    //---set the EditText to the text that has been
                    // read---
                    textBox1.setText(s);
                    Toast.makeText(getBaseContext(),"File loaded successfully!",Toast.LENGTH_SHORT).show();
                }
                catch (IOException ioe)
                {
                    ioe.printStackTrace();
                }
            }
        });
    }
}



 OUTPUT:
































Enter some text and click save 

 















































Click Load Button



 







































textfile.txt present in DDMS --> File Explorer --> selva.file (Your Package Name)
 --> File --> textfile.txt


open DDMS




Click File Explorer







Expand selva.file




































Expand files












Click Here to download source code


Store Data in Mobile Memory using SharedPreference



Click Here to download source code

Package Name   :   selva.shared

Project Name     :   SharedPreferences

Version              :   1.5 ( Support 1.5 and above versions)


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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

 
    <Button
        android:id="@+id/button1"
        android:layout_width="98dp"
        android:layout_height="wrap_content"
        android:text="Save" />

</LinearLayout>



m1.xml

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


    <Button
        android:id="@+id/button"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:text="View Saved Data" />
   
</LinearLayout>


SharedPreferencesActivity.java


package selva.shared;

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

public class SharedPreferencesActivity extends Activity
{
     public SharedPreferences prefs;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button btn=(Button) findViewById(R.id.button1);
       
      btn.setOnClickListener(new View.OnClickListener()
      {
       
        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
       
               String str="www.androidprogramz.in";
                int no=234; 
                float fno=234.234f;
               
                prefs = getSharedPreferences("detail", MODE_PRIVATE);
                      // detail is sharedpreference name
                SharedPreferences.Editor editor = prefs.edit();
               
                editor.putString("Urlname",str);
                // To save str value in Urlname
                editor.putInt("Intvalue", no);
               // To save no value in Intvalue
                editor.putFloat("Floatvalue", fno);
                
// To save fno value in Floatvalue
                editor.commit();
           
           
            Intent i=new Intent(SharedPreferencesActivity.this,Anotheractivity.class);
            startActivity(i);
           
        }
    });
       
       
    }
}



Anotheractivity.java



package selva.shared;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Anotheractivity extends Activity
{
    public SharedPreferences prefs;
   
      public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.m1);
       
            Button btn=(Button) findViewById(R.id.button);
   
             prefs = getSharedPreferences("detail", MODE_PRIVATE);
            
                final String name=prefs.getString("Urlname", "novalue");
                     // To get name value from Urlname
               final int val=prefs.getInt("Intvalue", 0);
                       // To get val value from Intvlue
               final float fval=prefs.getFloat("Floatvalue", 0);
                     
// To get fval value from Floatvalue
            btn.setOnClickListener(new View.OnClickListener()
            {
               
                @Override
                public void onClick(View v)
                {
                    // TODO Auto-generated method stub
                LinearLayout l=(LinearLayout) findViewById(R.id.linearlayout);
               
                TextView tv=new TextView(Anotheractivity.this);
                tv.setText(name);
                tv.setTextColor(Color.GREEN);
                l.addView(tv);
               
                TextView tv1=new TextView(Anotheractivity.this);
                tv1.setText(Integer.toString(val));
                tv1.setTextColor(Color.GREEN);
                l.addView(tv1);
               
                TextView tv2=new TextView(Anotheractivity.this);
                tv2.setText(Float.toString(fval));
                tv2.setTextColor(Color.GREEN);
                l.addView(tv2);
               
           
                }
            });
             
             
        }

}

   note : to clear sharedpreference 

               prefs = getSharedPreferences("detail", MODE_PRIVATE);
                    
                SharedPreferences.Editor editor = prefs.edit();
              
                editor.clear(); // to clear all values in detail                

                editor.commit();

   note 1: to remove one particular value

              prefs = getSharedPreferences("detail", MODE_PRIVATE);
                    
                SharedPreferences.Editor editor = prefs.edit();
              
                editor.remove("Urlname"); // to remove paricular value from detail                

                editor.commit();



  AndroidManifest.xml


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

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

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
             android:label="@string/app_name"
             android:name=".Anotheractivity">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

OUTPUT:





















click save button and View Saved Data











































Click Here to download source code


Sunday, July 15, 2012

Create Html file and Displaying in Android



Click Here to Download Source Code

Package Name   :  selva.web

Project Name     :  WebView2

Version               : 1.5 ( Supports 1.5 and above versions)

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <WebView android:id="@+id/webview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>


 Create ex.html under assets folder

<body>
<h1> Hi This is Simple Website </h1>
<a href="http://www.androidprogramz.in/"> Click Here </a>

</body>






 
 WebView2Activity.java

 package selva.web;

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

public class WebView2Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
        WebView wv = (WebView) findViewById(R.id.webview1);
        wv.loadUrl("file:///android_asset/ex.html");
 
    }
}


OUTPUT: 



 




















click On Click Here













































Click Here to Download Source Code


Display HTML content in Android using WebView



Click Here to download Source Code

Package Name  :  selva.web

Project Name   :  WebView

Version              :  1.5 ( Supports 1.5 and above versions)

main.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/webview1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
</LinearLayout>


WebViewActivity.java

package selva.web;

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


public class WebViewActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
                      
              
               WebView wv = (WebView) findViewById(R.id.webview1);
               final String mimeType = "text/html";
               final String encoding = "UTF-8";
         
               String html = "<H1>A simple HTML page</H1><body>" + "<p>Android tutorial  programs present in www.androidprogramz.in</p>";
               wv.loadDataWithBaseURL("", html, mimeType, encoding, "");
    }
  
}



OUTPUT :






























Click Here to download Source Code


Display Images in GridView



Click Here to download Source Code


Package name  :  selva.image

Project name    :  ImageInGridView

Version             :  1.5 ( Supports 1.5 and above versions)
 

main.xml


 <?xml version="1.0" encoding="utf-8"?>
 <GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:gravity="center"/>




ImageInGridViewActivity.java


package selva.image;



import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class ImageInGridViewActivity extends Activity
{

//---the images to display---
Integer[] imageIDs = {
        R.drawable.image1,
        R.drawable.image2,
        R.drawable.image3,
        R.drawable.image4,
        R.drawable.image5,
        R.drawable.image6,
};
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        GridView gridView = (GridView) findViewById(R.id.gridview);
        gridView.setAdapter(new ImageAdapter(this));
        gridView.setOnItemClickListener(new OnItemClickListener()
            {
                public void onItemClick(AdapterView<?> parent,View v, int position, long id)
                    {
                        Toast.makeText(getBaseContext(),"pic" + (position + 1) + "selected",Toast.LENGTH_SHORT).show();
                    }
            });
    }
    public class ImageAdapter extends BaseAdapter
    {
        private Context context;
        public ImageAdapter(Context c)
        {
            context = c;
        }
        //---returns the number of images---
        public int getCount()
        {
            return imageIDs.length;
        }
        //---returns the ID of an item---
        public Object getItem(int position)
        {
            return position;
        }
    //---returns the ID of an item---
        public long getItemId(int position)
        {
            return position;
        }
        //---returns an ImageView view---
        public View getView(int position, View convertView,ViewGroup parent)
        {
            ImageView imageView;
                if (convertView == null)
                {
                    imageView = new ImageView(context);
                    imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    imageView.setPadding(5, 5, 5, 5);
                }
                else
                {

                imageView = (ImageView) convertView;
                }
                imageView.setImageResource(imageIDs[position]);
                return imageView;
        }
    }
}


OUTPUT :































click on image5









































Click Here to download Source Code