A Simple App! (Part 3)

Posted on Updated on

Hi everyone!

Let’s continue from where we left! So, I used Toasts in the previous post. Were you able to figure out what this does? I hope so!

It is basically used to show notifications on the screen. If you are an Android User, you must have encountered them while using Messages or Contacts application. We had also seen the code for the same. Let’s revisit it!

Toast.makeText(getApplicationContext(), “Hello World!”,Toast.LENGTH_SHORT).show();

Simply, Toast.makeText(params).show() is the function which is used to display a Toast on the screen. It’s parameters are used to define it’s various properties.

The first parameter is used to pass the context to the function. Mostly, we will be using getApplicationContext() as the value of this parameter since this provides the context required to display the Toast on the screen in our app (We will be discussing it in detail later).

The second parameter defines the text to be displayed in the notification. Here, Hello World! will appear on the screen. A string type variable can also be used here!

The third parameter simply defines the time for which the notification will be displayed. It can have two values:

  1. Toast.LENGTH_SHORT: Used to display the notification for a short period of time.
  2. Toast.LENGTH_LONG: Used to display the notification for a long period of time.
Now, I think we have understood how to use Toasts in our application.

So, we have completed the development of our first app! Let’s look at all the steps again:

  • We had changed the value of the default TextView to Developing Android Applications with Nikhil Gupta in the post titled Laying Out the Screen!
  • We added a Button to the layout in the post titled A Simple App! (Part 1).
  • We discussed the use of ID and its usage as an attribute in the layout and as a reference in the JAVA code in the post titled A Simple App! (Part 2) .
  • We finally discussed the use of Toasts in this post.

We will start with the development of a new and interesting app in the next post!

Till then,  BYE!

Advertisements

8 thoughts on “A Simple App! (Part 3)

    Salman said:
    June 4, 2012 at 11:28 am

    Nikhil, I am facing some problems in pinpointing the location of a user on a map, actually the an error pops up when I run the application, saying that the application stopped unexpectedly.

    Actually the program should do two things,first is to show the latitude and longitude and the second being pinpointing the location of an android device. The code runs fine if I just include the latitude and longitude part but gives the error for the second part.

    I have added the following permissions in the manifest file

    and

    the final code of the manifest file looks like this

    and the .java file is

    package com.gps.speed;

    import com.google.android.maps.GeoPoint;
    import com.google.android.maps.MapController;
    import com.google.android.maps.MapView;

    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.Toast;

    public class Gps103Activity extends Activity {
    private LocationManager lm;
    private LocationListener locationListener;
    private MapView mapView;
    private MapController mc;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    lm = (LocationManager)
    getSystemService(Context.LOCATION_SERVICE);
    locationListener = new MyLocationListener();
    lm.requestLocationUpdates(
    LocationManager.GPS_PROVIDER,
    0,
    0,
    locationListener);
    mapView = (MapView) findViewById(R.id.mapview);
    mc = mapView.getController();
    }
    protected boolean isRouteDisplayed() {

    return false;
    }
    private class MyLocationListener implements LocationListener
    {
    public void onLocationChanged(Location loc) {
    if (loc != null) {
    Toast.makeText(getBaseContext(),
    “Location changed : Lat: ” + loc.getLatitude() +
    ” Lng: ” + loc.getLongitude(),
    Toast.LENGTH_SHORT).show();
    GeoPoint p = new GeoPoint(
    (int) (loc.getLatitude() * 1E6),
    (int) (loc.getLongitude() * 1E6));
    mc.animateTo(p);
    mc.setZoom(16);
    mapView.invalidate();
    }
    }
    public void onProviderDisabled(String provider) {

    }
    public void onProviderEnabled(String provider) {

    }
    public void onStatusChanged(String provider, int status,
    Bundle extras) {

    }
    }
    }

    and the layout xml file is

    where “0j1Rjn1e9QaAyA_B4GtlH6e_2yc8_iS8AapjxOA” is the map API key

      Nikhil Gupta responded:
      June 4, 2012 at 7:42 pm

      Salman, it will be better if you upload the file and share the link here in order to debug it easily!

        Salman said:
        June 5, 2012 at 10:55 am

        I just emailed you that file

    Nikhil Gupta responded:
    June 5, 2012 at 11:09 pm

    Salman! Class must extend MapActivity instead of Activity while using Maps.Also, include ‘uses-library android:name=”com.google.android.maps”‘
    as a child element of the tag in manifest file.

      Salman said:
      June 6, 2012 at 12:22 am

      I changed Activity to MapActivity and included the child using

      but still the app is giving the same error

        Nikhil Gupta responded:
        June 6, 2012 at 12:52 am

        I think you have not included the above mentioned library in manifest correctly. Check it once again! If error still persists, post the file and the code.

    Salman said:
    June 6, 2012 at 9:24 am

    i used this code to include it in the manifest file

      Nikhil Gupta responded:
      June 6, 2012 at 11:27 am

      In the code above, you should include functions onProviderEnabled, onProviderDisabled and onStatusChanged in the MyLocationListener Class.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s