Thursday, 13 October 2011

Default Notification

Setting the default notification to the notification bar.

For setting the default notification, we have to use the android Notification Manager, Notification, Pending Intent.

Notification Manager - For getting notification service and setting the notification.
Notification - For adding contents inside the notification.
Pending Intent- For calling the activity on clicking the notification.

In notification, first we have to instantiate the notification, that will describe which application notification you are receiving..

For Instantiating the notification, below code is used.


               CharSequence tickerText  = "Default Notificaion";
long when = System.currentTimeMillis();
int icon = R.drawable.notf_icon;
Notification notification = new Notification(icon, tickerText, when);


This code will display a small icon at left side followed by the notification text.

At the time of receiving notification, you can set the vibration or display light color.

For setting the vibration, Below code is used.and you have to add permission in the manifest file.


notification.defaults = Notification.DEFAULT_VIBRATE; 


this will set the default vibration.

                         (or)


You can also define the your own vibration pattern.

int dot = 200;      // Length of a Morse Code "dot" in milliseconds
int dash = 500;     // Length of a Morse Code "dash" in milliseconds
int short_gap = 200;    // Length of Gap Between dots/dashes
int medium_gap = 500;   // Length of Gap Between Letters
int long_gap = 1000;    // Length of Gap Between Words
long[] pattern = {
   0,  // Start immediately
   dot, short_gap, dot, short_gap, dot,    // s
   medium_gap,
   dash, short_gap, dash, short_gap, dash, // o
   medium_gap,
   dot, short_gap, dot, short_gap, dot,    // s
   long_gap
};
notification.vibrate=pattern;


For setting the LED light color, the below code is used. Setting LED light color is fully depends upon the Hardware support.


notification.defaults=Notification.DEFAULT_LIGHTS;


this will set the default LED light.
                               
                                  (or)
You can also define your owl light color by using the below code.

notification.ledARGB = 0xff0000ff;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;


The complete code for Default Notification with Vibration and the LED light display is as follows.



package com.sample.notification;

import java.util.Calendar;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.RemoteViews.RemoteView;

public class Test extends Activity
{
private Button button1;
private static final int NOTIFICAION_ID = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        button1 = (Button) findViewById(R.id.button01);
        button1.setOnClickListener(button1Listner);
        
        onPush("Notification 2","Channel 2");
    }
    
public void onPush(String notification, String channel) 
{
stausBarNotificaion(notification, channel); // For Default Notification
}
public void stausBarNotificaion(String message, String Channel)
{
String ns = NOTIFICATION_SERVICE;
NotificationManager notfManager = (NotificationManager) getSystemService(ns);
CharSequence tickerText  = "Default Notificaion";
long when = System.currentTimeMillis();
int icon = R.drawable.notf_icon;
Notification notification = new Notification(icon, tickerText, when);
Context context = this.getApplicationContext();
CharSequence contentTitle = "A.B.C.E.F.G.H | TESTING";
CharSequence contentText = message;
Intent notificaionIntent = new Intent("com.sample.notification.Notificaions");
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificaionIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
//notification.defaults = Notification.DEFAULT_VIBRATE;
int dot = 200;      // Length of a Morse Code "dot" in milliseconds
int dash = 500;     // Length of a Morse Code "dash" in milliseconds
int short_gap = 200;    // Length of Gap Between dots/dashes
int medium_gap = 500;   // Length of Gap Between Letters
int long_gap = 1000;    // Length of Gap Between Words
long[] pattern = {
   0,  // Start immediately
   dot, short_gap, dot, short_gap, dot,    // s
   medium_gap,
   dash, short_gap, dash, short_gap, dash, // o
   medium_gap,
   dot, short_gap, dot, short_gap, dot,    // s
   long_gap
};
notification.vibrate=pattern;
//notification.defaults=Notification.DEFAULT_LIGHTS;
notification.ledARGB = 0xff0000ff;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notfManager.notify(NOTIFICAION_ID,notification);
}
private OnClickListener button1Listner = new View.OnClickListener() {
public void onClick(View arg0)
{
startActivity(new Intent("com.sample.notification.Notificaions"));
}
};
}

Screen shot for the Results.





Saturday, 17 September 2011

Gallery with paging dots

This is the sample application for android Gallery and the GridView. Gallery also contains the small white and grey dot at the bottom. The Small and white dots are the Gallery image indicators(ie., Total Number of images available in the Gallery).

If the user moves the image in the gallery, the small white and grey dots are also updated, depends upon the user moves.

Below is the code for the layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                   android:layout_width="fill_parent" android:layout_height="fill_parent" >
                  
     <RelativeLayout android:id="@+id/gallery_paging"
        android:layout_width="fill_parent"
        android:layout_height="180dp">
   
        <com.common.vmi.ExtendedGallery android:id="@+id/mygallery01"
            android:layout_width="fill_parent"
            android:layout_height="180dp"
            android:spacing="0px"
            android:fadingEdge="none"/>
       
        <LinearLayout android:id="@+id/image_count"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            androidrientation="horizontal"
            android:gravity="center"
            androidbackground="#00000000"
            android:layout_alignParentBottom="true" >   
        </LinearLayout>

   
    </RelativeLayout>
   
    <GridView     android:id="@+id/iconic_grid" android:layout_width="fill_parent"  
                android:layout_height="185dip" android:padding="10dp" android:columnWidth="10dp"
                android:verticalSpacing="10dp" android:horizontalSpacing="30dp"
                android:numColumns="3"  android:stretchMode="columnWidth"
                android:tag="temp"   android:layout_below="@+id/gallery_paging"
                android:listSelector="@drawable/corner_orange" android:layout_weight="1.0"
                android:layout_alignParentBottom="true" android:layout_marginBottom="30dip"/>  

       
    <RelativeLayout android:id="@+id/power_text_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/dash_text"
        android:gravity="center">
       
        <TextView android:id="@+id/power_text"
            android:layout_width="125dp"
            android:layout_height="30dip"
            android:gravity="center_vertical|right"
            android:textColor="@color/white">
        </TextView>
       
        <TextView android:id="@+id/power_ever_text"
            android:layout_width="125dp"
            android:layout_height="30dip"
            android:layout_toRightOf="@+id/power_text"
            android:gravity="center_vertical"
            android:textStyle="bold"
            android:textColor="@color/white">
        </TextView>
       
    </RelativeLayout>
   
   
    <RelativeLayout android:id="@+id/info_layout"
        android:layout_width="50dp"
        android:layout_height="35dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true">
       
        <ImageButton android:id="@+id/info_icon"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginBottom="2dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:background="@android:drawable/ic_dialog_info"
            android:layout_marginRight="10dp">
        </ImageButton>
       
    </RelativeLayout>
               
</RelativeLayout>

In the above gallery, im using extended gallery for controling the scrool of the Gallery. By using this extended gallery class user can scrool one image at the time.

In the above XML, I used linear layout  android:id="@+id/image_count" is used for loading the small white and gray dot programatically.

Below is the code for Extended Gallery.

package com.common.vmi;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Gallery;

public class ExtendedGallery extends Gallery{

      private boolean stuck = false;

      public ExtendedGallery(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
      }

      public ExtendedGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
      }

      public ExtendedGallery(Context context) {
        super(context);
      }

      @Override
      public boolean onTouchEvent(MotionEvent event) {
        return stuck || super.onTouchEvent(event);
      }

      @Override
      public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
        case KeyEvent.KEYCODE_DPAD_RIGHT:
          return stuck || super.onKeyDown(keyCode, event);
        }
        return super.onKeyDown(keyCode, event);
      }

      public void setScrollingEnabled(boolean enabled) {
        stuck = !enabled;
      }
     
      @Override
      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
          return super.onFling(e1, e2, 0, velocityY);
      }
}


Below is the code for loading the Gallery image and the Grid items.

package com.fsp.vmialumni;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

import com.common.vmi.ExtendedGallery;

public class Gallery_Grid extends Activity {
    GridView icon_grid;
    TextView power_text, power_imagine;
    ImageButton infobtn;
    RelativeLayout info_layout;
    LinearLayout count_layout;
    int count = 0;
    static TextView page_text[];

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboard_main);

        icon_grid = (GridView) findViewById(R.id.iconic_grid);
        ExtendedGallery mGallery = (ExtendedGallery) findViewById(R.id.mygallery01);
        mGallery.setAdapter(new AddImageAdapter(this));
        mGallery.setScrollingEnabled(true);

        infobtn = (ImageButton) findViewById(R.id.info_icon);
        info_layout = (RelativeLayout) findViewById(R.id.info_layout);
        power_text = (TextView) findViewById(R.id.power_text);
        power_imagine = (TextView) findViewById(R.id.power_ever_text);

        power_text.setText("Always");
        power_imagine.setText(" " + "Imagine");

        icon_grid.setAdapter(new GridMenuAdapter(Gallery_Grid.this));

        count_layout = (LinearLayout) findViewById(R.id.image_count);
        count = mGallery.getAdapter().getCount();
        System.out.println("Gallery Image Count======>>>" + count);

        page_text = new TextView[count];
        for (int i = 0; i < count; i++) {
            page_text[i] = new TextView(this);
            page_text[i].setText(".");
            page_text[i].setTextSize(45);
            page_text[i].setTypeface(null, Typeface.BOLD);
            page_text[i].setTextColor(android.graphics.Color.GRAY);
            count_layout.addView(page_text[i]);
        }

        mGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) {
                // TODO Auto-generated method stub
                System.out.println("Item Selected Position=======>>>" + pos);
                for (int i = 0; i < count; i++) {
                    Gallery_Grid.page_text[i]
                            .setTextColor(android.graphics.Color.GRAY);
                }
                Gallery_Grid.page_text[pos]
                        .setTextColor(android.graphics.Color.WHITE);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
    }


//This is tha adapter class for loading the images into the Gallery.

    public class AddImageAdapter extends BaseAdapter {
        Context mycontext = null;
        int galitembg = 0;

        public AddImageAdapter(Context c) {
            mycontext = c;
            TypedArray typArray = mycontext
                    .obtainStyledAttributes(R.styleable.GalleryTheme);
            galitembg = typArray.getResourceId(
                    R.styleable.GalleryTheme_android_galleryItemBackground, 0);
            typArray.recycle();
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return Gallery_Grid.IMAGE_IDS.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ImageView imageview = new ImageView(mycontext);
            imageview.setImageResource(Gallery_Grid.IMAGE_IDS[position]);
            imageview.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageview;
        }
    }



//These are the Images Going to load to the gallery from the res/drawable folder.

    public static int[] IMAGE_IDS = { R.drawable.aconite, R.drawable.alliums,
            R.drawable.babys_breath, R.drawable.gillyflower,
            R.drawable.grid_background, R.drawable.butterfly,
            R.drawable.cactus, R.drawable.camomile,
            R.drawable.helxinesoleiralii, R.drawable.dimetrodon,
            R.drawable.argali_sheep, R.drawable.background_image1,
            R.drawable.background_image2, R.drawable.plectranthusaustralis };

}

Below is the Layout for GridView  Items.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:gravity="center_horizontal">
   
    <ImageView     android:id="@+id/grid_item_icon"
                android:layout_width="100dip" android:layout_height="56dip" />

    <TextView    android:layout_width="wrap_content" android:layout_height="wrap_content" 
                android:id="@+id/grid_item_label" android:paddingTop="2sp"
                android:textColor="@color/white" android:textSize="13sp"
                android:gravity="center_horizontal"    android:singleLine="true"
                android:ellipsize="marquee" android:textStyle="bold" android:typeface="serif"/>
</LinearLayout>

The GridView Contains the Image and a Textview.

Below is the code for GridView Adapter class.

package com.fsp.vmialumni;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GridMenuAdapter extends BaseAdapter {
    Context mycontext = null;

    public static int[] IMAGE_IDS = { R.drawable.tennis, R.drawable.racecar,
            R.drawable.jewwls, R.drawable.gold_medel, R.drawable.football,
            R.drawable.ic_dictionary

    };

    public static String[] item_name = { "Item 01", "Item 02", "Item 03",
            "Item 04", "Item 05", "Item 06" };

    public GridMenuAdapter(Context c) {
        mycontext = c;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return IMAGE_IDS.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View itemview = convertView;
        if (convertView == null) {
            LayoutInflater sl = LayoutInflater.from(mycontext);
            itemview = sl.inflate(R.layout.dashboard_item, null);

            TextView stv = (TextView) itemview
                    .findViewById(R.id.grid_item_label);
            stv.setText(item_name[position]);

            ImageView simg = (ImageView) itemview
                    .findViewById(R.id.grid_item_icon);
            simg.setImageResource(IMAGE_IDS[position]);
        }

        return itemview;
    }

}

Below is the some of the Screen Shot of the OutPut.

Small dot at Fourth Position
Small Dot at First Position



Small dot at EightPosition


Small dot at Thirteen Position

Saturday, 10 September 2011

Adding String array resource data to the Spinner

First Create a layout in the res/layout folder, (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"
    android:background="@drawable/treeimage"
    >
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:layout_marginLeft="80dp"
        android:layout_marginTop="20dp"
        android:textSize="25sp"
        android:textColor="#CCCCCC"
        />
    <Spinner android:id="@+id/Spinner01"
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:prompt="@string/Convertion"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="10dp"
            />
 
</LinearLayout>

Run the application, it will display the page like below, A Spinner will be added in the layout.

After that Add your Array values in the string.xml, available in the res/values folder.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Convertion List</string>
    <string name="app_name">DCConvertion</string>
   
    <string name="Convertion">Select Convertion</string>
   
    <string-array name="Convertion_Element">
        <item>(Select)</item>
        <item>Dollar To Currency</item>
        <item>Currency To Dollar</item>
    </string-array>

   
    <string name="title">Dollar To Currency</string>
    <string name="titlecur">Currency To Dollar</string>
    <string name="dollar">Dollar Value</string>
    <string name="currency">Currency Value</string>
    <string name="convert">Convert</string>
   
</resources>

The above bolded item is the array element that are going to load in the spinner. You can add as many item you needed inside the <item></item> tag.

After that, Create a class file called DCConvertion.java

package com.convertion.dc;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class DCConvertion extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);
      //  con=getApplicationContext();
        Spinner spinner=(Spinner)findViewById(R.id.Spinner01);
        ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.Convertion_Element, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

            public void onItemSelected(AdapterView<?> parent, View view, int pos,long id)
            {
                // TODO Auto-generated method stub
                //Toast.makeText(parent.getContext(), "Selected : "+parent.getItemIdAtPosition(pos), Toast.LENGTH_LONG).show();
                switch(pos)
                {
                    case 0:
                        Toast.makeText(parent.getContext(), "Please Select Your Choice", Toast.LENGTH_LONG).show();
                        break;
                       
                    case 1:
                        Toast.makeText(parent.getContext(), "You are Selected Doller to Currency Convertion", Toast.LENGTH_LONG).show();
                        Intent indtoc=new Intent(DCConvertion.this,Dollartocurrencyconvert.class);
                        startActivity(indtoc);
                        break;
                       
                    case 2:
                        Toast.makeText(parent.getContext(), "You are Selected Currency to Dollar Convertion", Toast.LENGTH_LONG).show();
                        Intent inctod=new Intent(DCConvertion.this,Currencytodollarconvert.class);
                        startActivity(inctod);
                        break;
                }
            }

            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub
               
            }
           
            });
    }
}


The above bolded code will create an array adapter for the spinner, inside the array adapter you should load your string array resource R.id.Convertion_Element. Finally set the loaded adapter to the spinner.

Run the program, and click the spinner, it will display like above image.