Wednesday, July 11, 2012

TimePickerDialog in Android



Click Here to download source code 

Package Name  :  selva.timepicker

Project Name    :  TimePickerDialog

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="wrap_content"
        android:layout_height="wrap_content"
        android:text="ShowTimepickerDialog" />

</LinearLayout>



 TimePickerDialogActivity.java



package selva.timepicker;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;
import android.app.Dialog;
import android.app.TimePickerDialog;
public class TimePickerDialogActivity extends Activity
{
   
    int hour, minute;
    static final int TIME_DIALOG_ID = 0;
    TimePicker timePicker;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
   
   
        Button btnOpen=(Button) findViewById(R.id.button1);
      
        btnOpen.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                showDialog(TIME_DIALOG_ID);
                }
        });
    }
   
@Override
protected Dialog onCreateDialog(int id)
{
    switch (id)
    {
    case TIME_DIALOG_ID:
        return new TimePickerDialog(
                this, mTimeSetListener, hour, minute, false);
    }
    return null;
}
private TimePickerDialog.OnTimeSetListener mTimeSetListener =new TimePickerDialog.OnTimeSetListener()
{
    public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfHour)
    {
        hour = hourOfDay;
        minute = minuteOfHour;
        Toast.makeText(getBaseContext(),"You have selected : " + hour + ":" + minute,Toast.LENGTH_SHORT).show();
    }
};
}

 OUTPUT:

























ClickTimepickerDialog







































select time and click set button















































 Click Here to download source code


TimePicker in Android



Click Here to download source code

Package Name  :  selva.timepicker

Project Name   :  TimePicker

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="TimePicker" />

    <TimePicker
        android:id="@+id/timePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <Button
        android:id="@+id/button1"
        android:layout_width="121dp"
        android:layout_height="wrap_content"
        android:text=" get Time" />

</LinearLayout>


TimePickerActivity.java


package selva.datepicker;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;
public class TimePickerActivity extends Activity

{
TimePicker timePicker;
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        timePicker = (TimePicker) findViewById(R.id.timePicker1);
        timePicker.setIs24HourView(false);
   
        //---Button view---
        Button btnOpen = (Button) findViewById(R.id.button1);
        btnOpen.setOnClickListener(new View.OnClickListener()
        {
   
            public void onClick(View v)
            {
           Toast.makeText(getBaseContext(),"Time selected:"  
                                      +timePicker.getCurrentHour()+":"
                + timePicker.getCurrentMinute(),Toast.LENGTH_SHORT).show();
            }
        });
    }

OUTPUT:






























click get Time









































Click Here to download source code 


AutoCompleteTextView in Android


Package Name  :  selva.auto

Project Name    :  AutoComplete

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="Name of Fruits" />
  
   <AutoCompleteTextView android:id="@+id/txt"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>


 AutoCompleteActivity.java

package selva.auto;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class AutoCompleteActivity extends Activity {
String[] Fruits = {
            "Apple",
            "Banana",
            "Orange",
            "Mango",
            "Grapes",
            "Jack Fruit",
            "Strawberry",
            "cucumber",
            "pumpkin"
    };
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, Fruits);
        AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.txt);
        textView.setThreshold(3);
        textView.setAdapter(adapter);
}
}


OUTPUT:
























Enter text "app" in textfield. It shows apple.






























Enter text "str" in textfield. It shows strawberry.

































Click Here to download source code

http://www.androidprogramz.in/

ProgressBar in Android


package name   :  selva.progressbar

projectname     :  ProgressBar

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" />
   
    <ProgressBar android:id="@+id/progressbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</LinearLayout>




 Progressbar_exActivity.java


package selva.progressbar;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;
public class Progressbar_exActivity extends Activity
{
    private static int progress;
    private ProgressBar progressBar;
    private int progressStatus = 0;
    private Handler handler = new Handler();
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            progress = 0;
            progressBar = (ProgressBar) findViewById(R.id.progressbar);
            //---do some work in background thread---
            new Thread(new Runnable()
            {
                public void run()
                    {
                        //---do some work here---
                    while (progressStatus < 10)
                        {
                            progressStatus = doSomeWork();
                        }
                    //---hides the progress bar---
                    handler.post(new Runnable()
                    {
                        public void run()
                        {
                                //---0 - VISIBLE; 4 - INVISIBLE; 8 - GONE---
                            progressBar.setVisibility(8);
                        }
                        });
                    }
                //---do some long lasting work here---
                private int doSomeWork()
                {
                    try {
                        //---simulate doing some work---
                        Thread.sleep(500);
                        }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                    return ++progress;
                }
            }).start();
}
}

OUTPUT:
































Click Here to download source code

http://www.androidprogramz.in/

Tuesday, July 10, 2012

Connect SQLite Database from Android



Click Here to download source code

Package name  :  db.com

Project name   :  DBAdapter

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

</LinearLayout>



DBAdapterActivity.java


package db.com;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import android.database.Cursor;

public class DBAdapterActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        DBAdapter db = new DBAdapter(this);
       
        db.open();
        long id = db.insertContact("android","www.androidprogramz.in");
        id = db.insertContact("Blog","androidprogramz.blogspot.in");
        Log.d("","insert done");
        db.close();
       
        db.open();
        Cursor c = db.getAllContacts();
        Log.d("","select done");
        if (c.moveToFirst())
        {
        do {
        DisplayContact(c);
        } while (c.moveToNext());
        }
        db.close();
        }
        public void DisplayContact(Cursor c)
        {
        Toast.makeText(this,
       "id:" + c.getString(0) +"\n" +"Name:" + c.getString(1) +"\n" +
       "Website:" + c.getString(2),
        Toast.LENGTH_LONG).show();
        }
       
       
       
    }
 



DBAdapter.java

package db.com;
import android.content.ContentValues;
import android.content.Context;

import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter
{
    public static final String KEY_ROWID ="_id";
    public static final String KEY_NAME ="name";
    public static final String KEY_SITE ="website";
    private static final String TAG ="DBAdapter";
    private static final String DATABASE_NAME ="MyDb";
    private static final String DATABASE_TABLE ="contacts";
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_CREATE =
            "create table contacts (_id integer primary key autoincrement,name text not null, website text not null);";
    private final Context context;
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;
    public DBAdapter(Context ctx)
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }
    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            Log.d("","constructor done");
        }
        @Override
        public void onCreate(SQLiteDatabase db)
        {
            try {
                db.execSQL(DATABASE_CREATE);
                Log.d("","create done");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            Log.w(TAG,"Upgrading database from version" + oldVersion +" to"
                    + newVersion +", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS contacts");
            onCreate(db);
        }
    }
    //    ---opens the database---
    public DBAdapter open() throws SQLException
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }
//    ---closes the database---
    public void close()
    {
        DBHelper.close();
    }
//    ---insert a contact into the database---
    public long insertContact(String name, String email)
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_NAME, name);
        initialValues.put(KEY_SITE, email);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }
//---deletes a particular contact---
    public boolean deleteContact(long rowId)
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID +"=" + rowId, null) > 0;
    }
//    ---retrieves all the contacts---
    public Cursor getAllContacts()
    {
        return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
                KEY_SITE}, null, null, null, null, null);
       
    }
//---retrieves a particular contact---
    public Cursor getContact(long rowId) throws SQLException
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                        KEY_NAME, KEY_SITE}, KEY_ROWID +"=" + rowId, null,
                        null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
     //    ---updates a contact---
    public boolean updateContact(long rowId, String name, String email){
        ContentValues args = new ContentValues();
        args.put(KEY_NAME, name);
        args.put(KEY_SITE, email);
        return db.update(DATABASE_TABLE, args, KEY_ROWID +"=" + rowId, null) > 0;
        }
        }


OUTPUT:







Click Here to download source code


Calling Builtin Applications Using Intents



Click Here to download source code

package name  :  selva.builtin

project name    :  callingbuiltinapp

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" >
<Button
android:id="@+id/webbrowser"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Web Browser" />
<Button
android:id="@+id/makecalls"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Make Calls" />

</LinearLayout>



 CallingbuiltinappActivity.java



package selva.builtin;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
       
        public class CallingbuiltinappActivity extends Activity {
        Button b1, b2;
        int request_Code = 1;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //---Web browser button---
       
        b1 = (Button) findViewById(R.id.webbrowser);
        b1.setOnClickListener(new OnClickListener()
        {
        public void onClick(View arg0){
        Intent i = new
        Intent(android.content.Intent.ACTION_VIEW,
        Uri.parse("http://www.google.com"));
        startActivity(i);
        }
        });
        //---Make calls button---
        b2 = (Button) findViewById(R.id.makecalls);
        b2.setOnClickListener(new OnClickListener()
        {
        public void onClick(View arg0){
        Intent i = new
        Intent(android.content.Intent.ACTION_DIAL,
        Uri.parse("tel:+919750376373"));
        startActivity(i);
        }
        });
     
   
        }
        public void onActivityResult(int requestCode, int resultCode, Intent data)
        {
        if (requestCode == request_Code)
        {
        if (resultCode == RESULT_OK)
        {
            Toast.makeText(this,data.getData().toString(),
                    Toast.LENGTH_SHORT).show();
                    Intent i = new Intent(
                    android.content.Intent.ACTION_VIEW,
                    Uri.parse(data.getData().toString()));
                    startActivity(i);
                    }
                    }
                    }
   
    }



OUTPUT:
























click web Browser button

















































click make calls button



























Click Here to download source code



Calling One Activity from non activity class



Click here to download source code

Package name  : selva.activity

Project name    : callactivity

verssion            : 1.5 (supports 1.5 and above)


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="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click to move next Activity" />

</LinearLayout>




CallactivityActivity.java

package selva.activity;

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

public class CallactivityActivity extends Activity
{

String str="";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

     setContentView(R.layout.main);
    
     Bundle extras = getIntent().getExtras();
     if (extras!=null)
     {
        str = extras.getString("val");

        // get the string stored in val which is passed from Nonactivityclass.java
        Toast.makeText(getApplicationContext(),str,Toast.LENGTH_LONG).show();
         
     }
    
      Button btn=(Button) findViewById(R.id.button1);
     
      btn.setOnClickListener(new View.OnClickListener()
      {
       
        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            Intent i=new Intent(CallactivityActivity.this,Nonactivityclass.class);
            startActivity(i);
           
        }
    });
     
}
      
     

}





Nonactivityclass.java



package selva.activity;

import android.content.Intent;
import android.os.Bundle;

public class Nonactivityclass extends CallactivityActivity{
   
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
       
       
       
        Intent i = new Intent();
       
        i.setClass(Nonactivityclass.this, CallactivityActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       
        Bundle extras = new Bundle();
        extras.putString("val", "This is from non-activity class");

      // pass the string   "This is from non-activity class" to "val" key
        i.putExtras(extras);
        startActivityForResult(i, 1);
       
    }

 }




OUTPUT:


 






































































Click here to download source code