Thursday, December 3, 2009

Working with Alert / Log / Notification in Android

Here we are discussing about the some essential Source which is helps to track or use the inbuild system call.
1. Toast
    Toast notification are holdes alert message to the user. It does not accept the interaction events. It automatically fades in and out.

System Toast Notification
Activity : 1 -  MainActivity
 public class MainActivity extends Activity {
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
     }
}
 

2.  Define Toast Object and Message
    // One-line Toast Notification Implementation
    Toast.makeText(this, "Demo Toast", Toast.LENGTH_LONG).show();

    // Change the Notification Position
    Toast myToast = Toast.makeText(this, "Demo Toast", Toast.LENGTH_LONG);
    myToast.setGravity(Gravity.TOP, 100, 100);
    myToast.show();

3. Save and run the Project.

Custom Toast Notification
Activity : 1 -  MainActivity
 public class MainActivity extends Activity {
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
     }
}


2. Layout : Create a custom Layout with some icon and text message

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout android:id="@+id/AbsoluteLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView android:id="@+id/ImageView01" android:layout_height="wrap_content"
    android:layout_width="wrap_content" android:layout_y="161dip" android:layout_x="45dip"
    android:background="@drawable/icon"></ImageView>
    <TextView android:layout_width="wrap_content" android:layout_x="131dip"
    android:layout_height="wrap_content" android:layout_y="163dip" android:id="@+id/TextView01"
    android:text="Custom Message"></TextView>
</AbsoluteLayout>

3. Get the Layout View using the LayoutInflator in Activity Class
    LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.custommessage,
        (ViewGroup) findViewById(R.id.AbsoluteLayout01));

4. Define the Toast object with the custom message.
        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP | Gravity.LEFT, 100, 100);
        toast.setView(layout);
        toast.show();

5. Save and run the Project.

Full Source
custommessage.xml - Layout
    <?xml version="1.0" encoding="utf-8"?>
    <AbsoluteLayout android:id="@+id/AbsoluteLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
        <ImageView android:id="@+id/ImageView01" android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:layout_y="161dip" android:layout_x="45dip"
        android:background="@drawable/icon"></ImageView>
        <TextView android:layout_width="wrap_content" android:layout_x="131dip"
        android:layout_height="wrap_content" android:layout_y="163dip" android:id="@+id/TextView01"
        android:text="Custom Message"></TextView>
    </AbsoluteLayout>
    
    
    
MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // One-line Toast Notification Implementation
        Toast.makeText(this, "Demo Toast", Toast.LENGTH_LONG).show();

        // Change the Notification Position
        Toast myToast = Toast.makeText(this, "Demo Toast", Toast.LENGTH_LONG);
        myToast.setGravity(Gravity.TOP, 100, 100);
        myToast.show();

        // Custom Notification
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.custommessage,
                (ViewGroup) findViewById(R.id.AbsoluteLayout01));
        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP | Gravity.LEFT, 100, 100);
        toast.setView(layout);
        toast.show();

    }
}


Log -
Log Class provides to create a log statement with Key Value Pair.
You can create Log statement using the following steps.
1. Create a Project -> Activity
public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
    }
}
2. Call the d() with Key value pair. ->
    Log.d("@G MYLOG","Sample Log Message");
    Key -> @G MYLOG
    Value->Sample Log Message
    
3. Run and Check the Log.
    Go to Window->Show View -> Android -> Log Cat


    

Source :
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d("@G MYLOG","Sample Log Message");
    }
}


No comments:

Post a Comment