Showing posts with label beginner. Show all posts
Showing posts with label beginner. Show all posts

Monday, 18 April 2011

Android - Auto Complete Tutorial

Auto Complete feature is a great feature for applications that involve fields that have to be filled manually. In Android the AutoCompleteTextView brings this feature. This is a relatively simple control to use and a fully functional auto complete feature can be implemented in just 4 lines of code.

Here is what we are going to do:
  1. Define the AutoCompleteTextView in XML (or Java).
  2. Create a ArrayAdapter for the AutoCompleteTextView.
  3. Set the adapter you created in step 2 to the AutoCompleteTextView.
  4. Set the threshold value for the AutoCompleteTextView. The threshold value defines the number of characters that has to be typed before the auto complete list is displayed on the screen.
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">  
      <AutoCompleteTextView   
        android:id="@+id/autocomplete_textview"   
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content" />  
 </LinearLayout>  

MainActivity.java
 package com.mobsandgeeks.acd;  
   
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.widget.ArrayAdapter;  
 import android.widget.AutoCompleteTextView;  
   
 public class MainActivity extends Activity {  
        
      private static final String[] FRUITS = {  
           "Apple", "Orange", "Banana", "Avacado", "Pear",  
           "Peach", "Lemon", "Pineapple", "Strawberry", "Gooseberry",  
           "Grapes", "Watermelon", "Mango", "Papaya", "Dragonfruit",  
           "Guava", "Honeydew", "Jackfruit", "Kiwifruit", "Olive",  
           "Pomegranate", "Raspberry", "Fig", "Custard Apple"  
      };  
        
      @Override  
      public void onCreate(Bundle savedInstanceState) {  
             super.onCreate(savedInstanceState);  
             setContentView(R.layout.main);  
       
             AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autocomplete_textview);  
       
             ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,   
                      android.R.layout.simple_dropdown_item_1line, FRUITS);  
       
             actv.setThreshold(1);  
             actv.setAdapter(adapter);  
      }  
 }  

Source Code
You can download the source code from here.

Screenshot

Saturday, 16 April 2011

Android - Intents and Broadcast Receivers

Intents
Intents are messages that are passed between Android components. They are used to do specific jobs inside the Android platform. Most of the actions that are carried out in Android involve intents. For instance, you as an application developer should send intents for starting activities, starting / stopping services, completing an action using other applications like taking a photograph, sending email, text messages, etc.,

The primary pieces of an Intent are:
  1. Action - The general action to be performed
  2. Data - The data to operate on, generally a URI
You can also define your own actions and receive those intents using broadcast receivers.

Broadcast Receivers a.k.a Intent Receviers
Broadcast Receivers receive intents that sent by sendBroadcast(). You can statically publish a receiver in the AndroidManifest.xml using the <receiver> tag or you can do the same using Context.registerReceiver(). In this example we will be registering out broadcast receiver statically.

MainActivity.java
 package com.mobsandgeeks.ire;  
   
 import android.app.Activity;  
 import android.content.Intent;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.widget.Button;  
   
 public class MainActivity extends Activity implements OnClickListener {  
      private Button sendIntentButton;  
     
      @Override  
      public void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.main);  
       
           sendIntentButton = (Button) findViewById(R.id.send_intent_button);  
           sendIntentButton.setOnClickListener(this);  
      }  
   
      public void onClick(View view) {  
           if(view == sendIntentButton) {  
                Intent i = new Intent();  
                i.setAction("com.mobsandgeeks.action.DEMO_ACTION");  //Our Action, use the same in the BroadcastReceiver too
                sendBroadcast(i);  //Broadcast the intent with the custom action
           }  
      }  
 }  

MyReceiver.java
 package com.mobsandgeeks.ire;  
   
 import android.content.BroadcastReceiver;  
 import android.content.Context;  
 import android.content.Intent;  
 import android.widget.Toast;  
   
 public class MyReceiver extends BroadcastReceiver {  
   
      @Override  
      public void onReceive(Context context, Intent intent) {  
           String action = intent.getAction();

           //This check is redundant since the intent-filter in the manifest only 
           //accepts our action. Use condition checking only when you use a receiver 
           //to handle multiple actions
           if(action.equals("com.mobsandgeeks.action.DEMO_ACTION")) {
                Toast.makeText(context, "Intent received", Toast.LENGTH_SHORT).show();  
           }  
      }  
 }  

AndroidManifest.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="com.mobsandgeeks.ire"  
    android:versionCode="1"  
    android:versionName="1.0">  
   <uses-sdk android:minSdkVersion="4" />  
        
   <application android:icon="@drawable/icon" android:label="@string/app_name">  
     <activity android:name=".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>  
       
     <receiver android:name="MyReceiver">  
          <intent-filter>  
               <action android:name="com.mobsandgeeks.action.DEMO_ACTION" />  
          </intent-filter>  
     </receiver>  
   </application>  
 </manifest>  

Source Code
You can download the source from here.

Screenshot

Android - Finding current GPS Coordinates

This is a very simple example to find the current GPS coordinates and altitude. In order to use this example you should turn on your device's GPS.

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:id="@+id/latitude_textview"  
           android:layout_width="fill_parent"  
           android:layout_height="wrap_content"  
           android:text="@string/latitude" />  
      <TextView  
           android:id="@+id/longitude_textview"  
           android:layout_width="fill_parent"  
           android:layout_height="wrap_content"  
           android:text="@string/longitude" />  
      <TextView  
           android:id="@+id/altitude_textview"  
           android:layout_width="fill_parent"  
           android:layout_height="wrap_content"  
           android:text="@string/altitude" />  
 </LinearLayout>  

GPSActivity.java
 package com.mobsandgeeks.gps;  
   
 import android.app.Activity;  
 import android.content.Context;  
 import android.location.Location;  
 import android.location.LocationListener;  
 import android.location.LocationManager;  
 import android.os.Bundle;  
 import android.widget.TextView;  
 import android.widget.Toast;  
   
 public class GPSActivity extends Activity implements LocationListener {  
      // UI References  
      private TextView latitudeTextView;  
      private TextView longitudeTextView;  
      private TextView altitudeTextView;  
   
      private LocationManager locationManager;  
   
      @Override  
      public void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.main);  
   
           latitudeTextView = (TextView) findViewById(R.id.latitude_textview);  
           longitudeTextView = (TextView) findViewById(R.id.longitude_textview);  
           altitudeTextView = (TextView) findViewById(R.id.altitude_textview);  
   
           locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
           locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,  
                     0, this);  
      }  
        
      public void onLocationChanged(Location location) {  
           String latitude = getString(R.string.latitude) + location.getLatitude();  
           String longitude = getString(R.string.longitude) + location.getLongitude();  
           String altitude = getString(R.string.altitude) + location.getAltitude();  
             
           latitudeTextView.setText(latitude);  
           longitudeTextView.setText(longitude);  
           altitudeTextView.setText(altitude);  
      }  
        
      public void onProviderDisabled(String provider) {  
           Toast.makeText(this, "GPS Disabled", Toast.LENGTH_SHORT).show();  
      }  
        
      public void onProviderEnabled(String provider) {  
           Toast.makeText(this, "GPS Enabled", Toast.LENGTH_SHORT).show();  
      }  
        
      public void onStatusChanged(String provider, int status, Bundle extras) {
      }
 }  

AndroidManifest.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="com.mobsandgeeks.gps"  
    android:versionCode="1"  
    android:versionName="1.0">  
   <uses-sdk android:minSdkVersion="4" />  
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
   
   <application android:icon="@drawable/icon" android:label="@string/app_name">  
     <activity android:name=".GPSActivity"  
          android:label="@string/app_name">  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </manifest>  

Source Code
You can download the entire source code from here.

Screenshot