Friday, July 19, 2013

Android Progres Dialog Example

The Android Development Tutorials blog contains Basic as well as Advanced android tutorials.Go to Android Development Tutorials to get list of all Android Tutorials.

ProgresBar


ProgressBar is used to show the progress of an operation.
ProgressBar is a Visual indicator of progress in some operation. Displays a bar to the user representing how far the operation has progressed; the application can change the amount of progress (modifying the length of the bar) as it moves forward. There is also a secondary progress displayable on a progress bar which is useful for displaying intermediate progress, such as the buffer level during a streaming playback progress bar.

Progress Bar Example

In this example I have shown the progress while downloading a file.

We can also customize a progress bar to make it more beautiful

 .Read How To Customize Progress Bar

 


Some More Good  Android Topics
Customizing Toast In Android 
 Showing Toast for Longer Time
Customizing Checkboxes In Android  
Customizing Progress Bar
To learn Basic of Android Animation  go to  Android Animation Basics

 

main..xml





<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

   <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:gravity="center_horizontal"
        android:textSize="25dp"
        android:text="Progress Bar Example" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:onClick="startProgressDialog"
        android:text="Start Downloading File" />

</LinearLayout>


Note: To add a progress bar to a layout file, you can use the <ProgressBar> element. By default, the progress bar is a spinning wheel (an indeterminate indicator). To change to a horizontal progress bar, apply the Widget.ProgressBar.Horizontal style, like so:







ProgressBarActivity.java


public class ProgressBarActivity extends Activity
{
  

    Button btnStartProgress;
    ProgressDialog progressBar;
    private int progressBarStatus = 0;
    private Handler progressBarHandler = new Handler();

    private long fileSize = 0;

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

    public void startProgressDialog(View V)
    {
       

                    // prepare for a progress bar dialog
                    progressBar = new ProgressDialog(V.getContext());
                    progressBar.setCancelable(true);
                    progressBar.setMessage("Downloading File...");
                    progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    progressBar.setProgress(0);
                    progressBar.setMax(100);
                    progressBar.show();

                    //reset progress bar status
                    progressBarStatus = 0;

                    //reset filesize
                    fileSize = 0;

                    new Thread(new Runnable() {
                        public void run() {
                            while (progressBarStatus < 100) {

                                    // process some tasks
                                progressBarStatus = fileDownloadStatus();

                                //  sleep 1 second to show the progress
                                try {
                                        Thread.sleep(1000);
                                    }
                                catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }

                                // Update the progress bar
                                progressBarHandler.post(new Runnable() {
                                    public void run() {
                                        progressBar.setProgress(progressBarStatus);
                                    }
                                });
                            }

                            // when, file is downloaded 100%,
                            if (progressBarStatus >= 100) {

                                // sleep 2 seconds, so that you can see the 100% of file download
                                try {
                                    Thread.sleep(2000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }

                                // close the progress bar dialog
                                progressBar.dismiss();
                            }
                        }
                    }).start();

      }


      


  //method returns the % of file downloaded
    public int fileDownloadStatus()
    {

        while (fileSize <= 1000000) {

                      fileSize++;

                       if (fileSize == 100000) {
                            return 10;
                       } else if (fileSize == 200000) {
                           return 20;
                       } else if (fileSize == 300000) {
                          return 30;
                       } else if (fileSize == 400000) {
                          return 40;
                       } else if (fileSize == 500000) {
                          return 50;
                      } else if (fileSize == 600000) {
                         return 60;
                      }
            // write your code here

        }

        return 100;

    }


We can also customize a progress bar to make it more beautiful

 .Read How To Customize Progress Bar





 


New Advance Topics:
Android ImageSwitcher                    Android TextSwitcher                                Android ViewFlipper
Android Gesture Detector               Handling/Detecting Swap Events                Gradient Drawable
Detecting Missed Calls                    Hide Title Bar                                           GridView Animation

 Beginning With Android
      Android : Introduction                                                              Configuring Eclipse for Android Development
     Creating Your First Android Project                                           Understanding Android Manifest File of your android app

 Advance Android Topics                                                              Customizing Android Views


Working With Layouts                                                                Working With Views

Understanding Layouts in Android                                                   Using Buttons and EditText in Android
Working with Linear Layout (With Example)                                     Using CheckBoxes in Android
Nested Linear Layout (With Example)                                              Using AutoCompleteTextView in Android                                                                                          Grid View
Relative Layout In Android                                                               ListView
Table Layout                                                                                   Android ProgressBar
Frame Layout(With Example)                                                          Customizing ProgressBar
Absolute Layout                                                                             Customizing Radio Buttons
Grid Layout                                                                                    Customizing Checkboxes In Android

Android Components                                                                 Dialogs In Android

Activity In Android                                                                    Working With Alert Dialog
Activity Life Cycle                                                                    Adding Radio Buttons In Dialog
Starting Activity For Result                                                       Adding Check Boxes In Dialog
Sending Data from One Activity to Other in Android                    Creating Customized Dialogs in Android
Returning Result from Activity                                                   Creating Dialog To Collect User Input
Android : Service                                                                     DatePicker and TimePickerDialog
BroadcastReceiver                                                                   Using TimePickerDialog and DatePickerDialog In android

Menus In Android                                                                ListView:
Creating Option Menu                                                               Populating ListView With DataBase
Creating Context Menu In Android                                              Populating ListView with ArrayList
                                                                                               ListView with Custom Adapter

Toast                                                                                      Working With SMS
Customizing Toast In Android                                                       How to Send SMS in Android
Customizing the Display Time of Toast                                        How To Receive SMS
Customizing Toast At Runtime                                                  Accessing Inbox In Android
Adding Image in Toast
Showing Toast for Longer Time


TelephonyManager                                                            Storage: Storing Data In Android
Using Telephony Manager In Android                                          SharedPreferences In Android
                                                                                              Reading and Writing files to Internal Stoarage
Working With Incoming Calls                                             DataBase
How To Handle Incoming Calls in Android                                Working With Database in Android
How to Forward an Incoming Call In Android                            Creating Table In Android
CALL States In Android                                                          Inserting, Deleting and Updating Records In Table in Android


Miscellaneous
Notifications In Android
How To Vibrate The Android Phone
Sending Email In Android
Opening a webpage In Browser
How to Access PhoneBook In Android
Prompt User Input with an AlertDialog






Wednesday, July 17, 2013

Android Contact Manager Example

In this blog  I  will describe how to access Conatcts/Phonebook in your Android Application.

Contacts are stored in separate tables(in Row and Column form)

 ContactsContract 


ContactsContract defines an extensible database of contact-related information. Contact information is stored in a three-tier data model:
  • A row in the ContactsContract.Data table can store any kind of personal data, such as a phone number or email addresses. The set of data kinds that can be stored in this table is open-ended. There is a predefined set of common kinds, but any application can add its own data kinds.
  • A row in the ContactsContract.RawContacts table represents a set of data describing a person and associated with a single account (for example, one of the user's Gmail accounts).
  • A row in the ContactsContract.Contacts table represents an aggregate of one or more RawContacts presumably describing the same person. When data in or associated with the RawContacts table is changed, the affected aggregate contacts are updated as necessary. 
find more  Information here


Cursor c1;
// list Columns to retive  , pass null to get all the columns
                String col[]={ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME};
                c1 = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, col, null, null, ContactsContract.Contacts.DISPLAY_NAME);
               

                String personName = null, number = "";
                if(c1==null)
                    return;
// Fetch the Corresponding Phone Number of  Person Name
                try
                {
                        if(c1.getCount() > 0)
                        {
                                while(c1.moveToNext())
                                {

                                    String id = c1.getString(c1.getColumnIndex(Contacts._ID));
                                    personName = c1.getString(c1.getColumnIndex(Contacts.DISPLAY_NAME));
                                    if(id==null||personName==null)
                                        continue;
                                    Cursor cur = mContext.getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI, null, CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
                                    if(cur==null)
                                        continue;
                                    number = "";
                                    while(cur.moveToNext())
                                    {
                                        number = cur.getString(cur.getColumnIndex(CommonDataKinds.Phone.NUMBER));
                                    }
                                    cur.close();
                                    if(!number.equals(""))
                                    {
                                            number=removeUnnecessaryCharacters(number);
                                            numberList.add(number);
                                            nameList.add(personName);
                                    }
                       
                                }
                        }
                }
                catch(Exception e)
                {
                   
                }
                finally
                {
                        c1.close();
                }

Tuesday, July 16, 2013

TimePicker In Android

Android TimePicker is used to select a time. Whenever we want from user to select a time, we use TimePicker.

main.xml

In layout we have a timrpicker, a button, and a textView
When user select some time and click on OK button we will show the selected timein textView.

<LinearLayout
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:orientation="vertical"
               xmlns:android="http://schemas.android.com/apk/res/android">
              
          <TimePicker
                android:id="@+id/timePicker1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        
        
         <Button
                   android:id="@+id/buttonSelectTime"
                   android:layout_gravity="center_horizontal"
                   android:layout_width="fill_parent"
                   android:layout_height="wrap_content"
                   android:layout_marginTop="40dp"
                   android:textSize="23dp"
                   android:text="   OK   " />
   
            <TextView
                android:id="@+id/textViewTime"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center_horizontal"
                android:text="Date and Time Selected: "
                android:textSize="17dp" />

</LinearLayout>


 TimePickerActivity

public class TimePickerActivity extends Activity
{
   
            TimePicker timePicker;
            TextView textView;
            Button btnTime;
            @Override
            protected void onCreate(Bundle savedInstanceState)
            {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);
                  
                    timePicker=(TimePicker)findViewById(R.id.timePicker1);
                    textView=(TextView)findViewById(R.id.textViewTime);
                    btnTime=(Button)findViewById(R.id.buttonSelectTime);
                  
                    btnTime.setOnClickListener(new View.OnClickListener() {
                      
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            int hour=timePicker.getCurrentHour();
                            int minute=timePicker.getCurrentMinute();
                          
                          
                            textView.setText("Time Selected  "+hour+":"+minute);
                        }
                    });
            }

}
 

DatePicker in Android

In android we can use DatePicker to select a date. When we want that user should select  a date  we use DatePicker.

main.xml

In layout we have a datepicker, a button, and a textView
When user select some date and click on OK button we will show the selected date in textView.

DatePicker

<LinearLayout
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:orientation="vertical"
               xmlns:android="http://schemas.android.com/apk/res/android">
              
          <DatePicker
                android:id="@+id/datePicker1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        
        
         <Button
                   android:id="@+id/buttonSelectDate"
                   android:layout_gravity="center_horizontal"
                   android:layout_width="fill_parent"
                   android:layout_height="wrap_content"
                   android:layout_marginTop="40dp"
                   android:textSize="23dp"
                   android:text="   OK   " />
   
            <TextView
                android:id="@+id/textViewDate"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center_horizontal"
                android:text="Date and Time Selected: "
                android:textSize="17dp" />

</LinearLayout>

DatePickerActivity.java

public class DatePickerActivity extends Activity
{
   
            DatePicker datePicker;
            TextView textView;
            Button btnDate;
            @Override
            protected void onCreate(Bundle savedInstanceState)
            {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);
                   
                    datePicker=(DatePicker)findViewById(R.id.datePicker1);
                    textView=(TextView)findViewById(R.id.textViewDate);
                    btnDate=(Button)findViewById(R.id.buttonSelectDate);
                   
                    btnDate.setOnClickListener(new View.OnClickListener() {
                       
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            // get the selected day, month and year
                            int day=datePicker.getDayOfMonth();
                            int month=datePicker.getMonth();
                            int year=datePicker.getYear();
                           
                            // show the selected date in textView
                            textView.setText("Date Selected  "+day+":"+month+":"+year);
                        }
                    });
            }

}

Android Custom AlertDialog Example

We can create a dialog with Edittext   and other views like Button, CheckBoxes, RadioButtons etc.

For this we need to Create A xml layout and and  inflate it in AlertDialog



                               

<?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" >
   
   
    <EditText
        android:id="@+id/editTextKeywordsToBlock"
        android:hint="Enter 1 or more keywords. Use space berween two keywords"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>

    <LinearLayout
                 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
           
            android:layout_marginTop="10dp">
                 

     <Button
         android:id="@+id/buttonBlockByKeyword"
         android:layout_marginTop="15dp"
         android:layout_weight="1"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="SAVE"
         />
    
      <Button
         android:id="@+id/buttonCancelBlockKeyword"
         android:layout_marginTop="15dp"
         android:layout_weight="1"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="Cancel"
         />
   
     </LinearLayout>
   

</LinearLayout>


And inflate this Layout at run time    like following



final Dialog dialog = new Dialog(this);

                    dialog.setContentView(R.layout.block_by_keyword);
                    dialog.setTitle("Keyword To Block");

                    final EditText editTextKeywordToBlock=(EditText)dialog.findViewById(R.id.editTextKeywordsToBlock);
                    Button btnBlock=(Button)dialog.findViewById(R.id.buttonBlockByKeyword);
                    Button btnCancel=(Button)dialog.findViewById(R.id.buttonCancelBlockKeyword);
                    dialog.show();

Saturday, July 13, 2013

Android Development Tutorial

The Android Development Tutorials blog contains Beginners as well as Advanced android tutorials. Learn Android Development by example with this blog.



New Advance Topics:                   Android LiveWallpaer Tutorial
Android ImageSwitcher                    Android TextSwitcher                                Android ViewFlipper
Android Gesture Detector               Handling/Detecting Swipe Events                Gradient Drawable
Detecting Missed Calls                    Hide Title Bar                                           GridView Animation
Android AlarmManager                 Android BootReceiver                       Vibrate Phone In a Desirable Pattern    
Developing for Different Screen Sizes           Showing Toast for Longer Time       Publishing your App
How to publish Android App on Google Play
Android TextWatcher                               Android ExpandableListView
Android Image Slider Example
Android Custom GridView Example
Android MediaPlayer Example
Android Text To Speech Example
How to check Radiation Level of Android Phone
How to find Advertising ID of Android Phone
CountDownTimer in Android Example


 Beginning With Android
      Android : Introduction(What is Android)                                                              Configuring Eclipse for Android Development
     Creating Your First Android Project                                           Understanding Android Manifest File of your android app

 Advance Android Topics                                                              Customizing Android Views

      Handling Keyboard Events                                                        Animating A Button In Android
                                                                                                         Android ViewAnimator Example

Working With Layouts                                                                Working With Views

Understanding Layouts in Android                                                   Using Buttons and EditText in Android
Working with Linear Layout (With Example)                                     Using CheckBoxes in Android
Nested Linear Layout (With Example)                                              Using AutoCompleteTextView in Android                                                                                          Grid View
Relative Layout In Android                                                               ListView
Table Layout                                                                                   Android ProgressBar
Frame Layout(With Example)                                                          Customizing ProgressBar
Absolute Layout                                                                             Customizing Radio Buttons
Grid Layout                                                                                    Customizing Checkboxes In 
Android
                                                                                                        Android RatingBar Example
                                                                           Deeveloping Simple Calculator App in Android

Android Advance Views
Android Spinner                                                                           Android GalleryView
Android TabWidget                                                                      Android ExpandableListView

Android Components                                                                 Dialogs In Android

Activity In Android                                                                    Working With Alert Dialog
Activity Life Cycle                                                                    Adding Radio Buttons In Dialog
Starting Activity For Result                                                       Adding Check Boxes In Dialog
Sending Data from One Activity to Other in Android                    Creating Customized Dialogs in Android
Returning Result from Activity     
PreferenceActivity In Android                                            Creating Dialog To Collect User Input
Android : Service                                                                     DatePicker and TimePickerDialog
BroadcastReceiver                                                                   Using TimePickerDialog and DatePickerDialog In android

Menus In Android                                                                ListView:
Creating Option Menu                                                               Populating ListView With DataBase
Creating Context Menu In Android                                              Populating ListView with ArrayList
                                                                                               ListView with Custom Adapter

Toast                                                                                      Working With SMS
Customizing Toast In Android                                                       How to Send SMS in Android
Customizing the Display Time of Toast                                        How To Receive SMS
Customizing Toast At Runtime                                                  Accessing Inbox In Android
Adding Image in Toast
Showing Toast for Longer Time


TelephonyManager                                                            Storage: Storing Data In Android
Using Telephony Manager In Android                                          SharedPreferences In Android
                                                                                              Reading and Writing files to Internal Stoarage

Working With Incoming Calls                                       DataBase :  Introduction of SQLiteDataBase
How To Handle Incoming Calls in Android                                Working With Database in Android
How to Forward an Incoming Call In Android                            Creating Table In Android
CALL States In Android                                                          Inserting, Deleting and Updating Records In Table in Android


Miscellaneous
Notifications In Android
How To Vibrate The Android Phone
Sending Email In Android
Opening a webpage In Browser
How to Access PhoneBook In Android
Prompt User Input with an AlertDialog
How to Hide Title Bar In Android
How to show an Activity in Landscape or Portrait Mode only.
How to Set an Image as Wallpaper.