Monday, March 21, 2011

Gallary And Grid View In Android

The Following Application show how to Perform the Gallary and Grid view in android.

First Take a new Project and Name it as:GallaryAndGrid
And then create a android class "GalleryCustom" which extnds Activity

The Source Code for "GalleryCustom.java"  is like this



package com.gallrygrid;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.gallrygrid.GalleryOne.ImageAdapter;


public class GalleryCustom extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.galleryone);
     // Reference the Gallery view
        Gallery g = (Gallery) findViewById(R.id.gallery);
        // Set the adapter to our custom adapter (below)
        g.setAdapter(new ImageAdapter(this));
       
        // Set a item click listener, and just Toast the clicked position
        g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
               
                Toast.makeText(GalleryCustom.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }
    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private Context mContext;

       
        public ImageAdapter(Context c) {
            mContext = c;
            // See res/values/attrs.xml for the <declare-styleable> that defines
            // Gallery1.
          
        }

        public int getCount() {
            return mImageIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            //LinearLayout ll = new LinearLayout(mContext);
            //LayoutParams lp;
           
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.gallerybox, null);
            }
            TextView tvname = (TextView)v.findViewById(R.id.txtviewbox);
            ImageView tvdetail = (ImageView)v.findViewById(R.id.imagebox);
           
            tvname.setText(mNames[position]);
            tvdetail.setImageResource(mImageIds[position]);
           
            //tvdetail.setScaleType(ImageView.ScaleType.FIT_XY);
            //tvdetail.setLayoutParams(new Gallery.LayoutParams(136, 88));
           
            return v;
        }

      
        private Integer[] mImageIds = {
                R.drawable.photo1,
                R.drawable.photo2,
                R.drawable.photo3,
                R.drawable.photo4,
                R.drawable.photo5,
                R.drawable.photo6,R.drawable.photo7
        };
        private String[] mNames = {
            "One", "Two","Three","Four","Five","Six"   
        };
    }
}



The "GalloryOne.xml" file is Like this

<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:spacing="3dip"
/>

Create An Activity in "AndroidManifeast.xml" like this

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.gallrygrid"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".GalleryOne"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

then this one Gives Out put as



Retirment calculator

This is the Retirement  calculator done by using awt and swings this is good and execute fastley

the source code is :

import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;
import javax.swing.text.PlainDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
class ret12
{
  JFrame x;
  JTextField t1,t2,t3,t4,t5,t6;
  JLabel l1,l2,l3,l4,l5,l7;
  Button b1,b2;
  ret12()
  {
      x=new JFrame("RETIRMENT CALUCULATOR");
      l1=new JLabel("PRESENT AGE");
      l2=new JLabel("RETIRED AGE");
      l3=new JLabel(" INTEREST RATE");
      l4=new JLabel("TOTAL MONEY");
      l5=new JLabel("AMOUNT DEPOSIT");
      l7=new JLabel("TOTAL YEARS");
      t1=new JTextField(new numbers(2),"",10);
      t2=new JTextField(new numbers(2),"",10);
      t3=new JTextField("",10);
      t4=new JTextField("",10);
      t5=new JTextField(new numbers(5),"",10);
      t6=new JTextField("",10);
      b1=new Button("CHECK");
      b2=new Button("CLEAR");
      x.setLayout(null);
      x.setResizable(false);
      l1.setBounds(30,40,120,40);
      t1.setBounds(220,50,150,25);
      l2.setBounds(30,80,120,35);
      t2.setBounds(220,85,150,25);
      l5.setBounds(30,120,150,35);
      t5.setBounds(220,125,150,25);
      l3.setBounds(28,160,150,35);
      t3.setBounds(220,165,150,25);
      l7.setBounds(30,200,120,35);
      t6.setBounds(220,205,150,25);
      l1.setFont(new Font("italic",Font.BOLD,16));
      l2.setFont(new Font("italic",Font.BOLD,16));
      l3.setFont(new Font("italic",Font.BOLD,16));
      l4.setFont(new Font("italic",Font.BOLD,16));
      l5.setFont(new Font("italic",Font.BOLD,16));
      l7.setFont(new Font("italic",Font.BOLD,16));
      l4.setBounds(30,240,120,35);
      t4.setBounds(220,245,150,25);
      t4.setEditable(false);
      b1.setBounds(60,300,100,30);
      b2.setBounds(210,300,100,30);
      Font f=new Font("Arial",Font.BOLD,30);
      x.add(l1);
      x.add(t1);
      x.add(l2);
      x.add(t2);
      x.add(l5);
      x.add(t5);
      x.add(l3);
      x.add(t3);
      x.add(l7);
      x.add(t6);
      x.add(l4);
      x.add(t4);
      x.add(b1);
      x.add(b2);
      x.addWindowListener(new WindowAdapter()
                              {
          public void windowClosing(WindowEvent ee)
          {
              System.exit(0);
          }
      });
      t1.setBackground(Color.PINK);
      t2.setBackground(Color.PINK);
      t3.setBackground(Color.PINK);
      t5.setBackground(Color.PINK);
      t6.setEditable(false);
      x.setSize(400,400);
      x.setVisible(true);
      b1.addKeyListener(new KeyAdapter()
                            {
          public void KeyPressed(KeyEvent e)
          {
              try
              {
                  int key=e.getKeyCode();
                  if(key==KeyEvent.VK_SPACE)
                  {
                      if(t1.getText().equals(""))
                          JOptionPane.showMessageDialog(x,"Please Enter the Present Age");
                      else if(Integer.parseInt(t1.getText())<20||Integer.parseInt(t1.getText())>55)
                          JOptionPane.showMessageDialog(x,"enter the present age between 20 and 55");  
                      else if(t2.getText().equals(""))
                          JOptionPane.showMessageDialog(x,"Please Enter Retired Age");
                      else if(Integer.parseInt(t2.getText())<50||Integer.parseInt(t2.getText())>65)
                          JOptionPane.showMessageDialog(x,"enter the Retirment age between 50 and 65");
                      else if(t5.getText().equals(""))
                          JOptionPane.showMessageDialog(x,"Please Enter Deposit Amount");
                      else if(Double.parseDouble(t5.getText())<=0||Double.parseDouble(t5.getText())>20000)
                          JOptionPane.showMessageDialog(x,"enter the amount between 1 and 13000");
                      else if(t3.getText().equals(""))
                          JOptionPane.showMessageDialog(x,"Please Enter Intrest Rate");
                      else if(Double.parseDouble(t3.getText())<=0||Double.parseDouble(t3.getText())>12)
                          JOptionPane.showMessageDialog(x,"enter the Intrest Rate between 1 and 12");
                      myfunction();
                           
                  }
              }
              catch(Exception ex)
              {
                  JOptionPane.showMessageDialog(x,"Check all the fields correctly without any Blanks and Alphabets");
              }
          }
      });
      b2.addKeyListener(new KeyAdapter()
                            {
          public void KeyPressed(KeyEvent e)
          {
              int key1=e.getKeyCode();
              if(key1==KeyEvent.VK_SPACE)
              {
                  t1.setText("");
                  t2.setText("");
                  t3.setText("");
                  t4.setText("");
                  t5.setText("");
                  t6.setText("");
              }
          }
      });
      b1.addMouseListener(new MouseAdapter()
                              {
          public void mouseClicked(MouseEvent e)
          {
              try
              {
                  if(t1.getText().equals(""))
                  JOptionPane.showMessageDialog(x,"Please Enter the Present Age");
                  else if(Integer.parseInt(t1.getText())<20||Integer.parseInt(t1.getText())>55)
                      JOptionPane.showMessageDialog(x,"Please Enter the Present Age Between 20 and 55");  
                  else if(t2.getText().equals(""))
                      JOptionPane.showMessageDialog(x,"Please Enter Retired Age");
                  else if(Integer.parseInt(t2.getText())<50||Integer.parseInt(t2.getText())>65)
                      JOptionPane.showMessageDialog(x,"Please Enter the Retirment Age Between 50 and 65");
                  else if(t5.getText().equals(""))
                      JOptionPane.showMessageDialog(x,"Please Enter Deposit Amount");
                  else if(Double.parseDouble(t5.getText())<=0||Double.parseDouble(t5.getText())>50000)
                      JOptionPane.showMessageDialog(x,"Please Enter the Amount Between 1 and 50000");
                  else if(t3.getText().equals(""))
                      JOptionPane.showMessageDialog(x,"Please Enter Intrest Rate");
                  else if(Double.parseDouble(t3.getText())<=0||Double.parseDouble(t3.getText())>20)
                      JOptionPane.showMessageDialog(x,"Please Enter the Intrest Rate Between 1 and 20");
                  myfunction();
              }
              catch(Exception ex)
              {
                 JOptionPane.showMessageDialog(x,"Please Enter the Intrest Rate With Out Any Charectres");
                 t6.setText("");
                 t4.setText("");
              }
          }
      });
 
      b2.addMouseListener(new MouseAdapter(){
          public void mouseClicked(MouseEvent e)
          {
              t1.setText("");
              t2.setText("");
              t3.setText("");
              t4.setText("");
              t5.setText("");
              t6.setText("");
      }
      });
  }
  public void myfunction()
  {
      Double p=Double.parseDouble(t5.getText());
      Double i=Double.parseDouble(t3.getText())/100;
      Double q=12.0;
      int p1=Integer.parseInt(t2.getText());
      int p2=Integer.parseInt(t1.getText());
      Double p3=Double.parseDouble(t3.getText());
      int n=Integer.parseInt(t2.getText())-Integer.parseInt(t1.getText());
      Double tot=0.0;
      Double n2=60.0;
      for(int j=0;j<n;j++)
      {
          tot=p+(tot+(tot*i));
      }
      if((p2>=20||p2<=55)&&(p1>=50&&p1<=65)&&(p3>0&&p3<=20)&&(p>=0&&p<=50000))
      {
          Long l=(new Double(Math.round(tot))).longValue();
          String s=(Long.toString(l));
          String s1=" ";
          if(s.length()==5)
          {
              s1+=(s.substring(0,2))+","+(s.substring(2,s.length()));
              t4.setText("Rs:"+s1);
          }
          else if(s.length()==6)
          {
              s1+=(s.substring(0,1))+","+(s.substring(1,3))+","+(s.substring(3,s.length()));
              t4.setText("Rs:"+s1);
          }
          else if(s.length()==7)
          {
              s1+=(s.substring(0,2))+","+(s.substring(2,4))+","+(s.substring(4,s.length()));
              t4.setText("Rs:"+s1);
          }
          else if(s.length()==8)
          {
              s1+=(s.substring(0,1))+","+(s.substring(1,3))+","+(s.substring(3,5))+","+(s.substring(5,s.length()));
              t4.setText("Rs:"+s1);
          }
          else if(s.length()==9)
          {
              s1+=(s.substring(0,2))+","+(s.substring(2,4))+","+(s.substring(4,6))+","+(s.substring(6,s.length()));
              t4.setText("Rs:"+s1);
          }
      }
      else
      {
          t4.setText("");
      }
    if((n>0&&n<=45)&&(p2>=20&&p2<=55)&&(p1>=50&&p1<=65)&&(p3>0&&p3<=20)&&(p>=0&&p<=50000))
    {
        t6.setText(Integer.toString(n));
    }
    else
    {
        t6.setText("");
    }
  }
  class numbers extends PlainDocument
  {
      private int limit;
      numbers(int limit)
      {
          super();
          this.limit = limit;
      }
    numbers(int limit, boolean upper)
      {
          super();
          this.limit = limit;
      }
    public void insertString(int offset, String str, AttributeSet a)
        throws BadLocationException
    {
        for (int i = 0; i < str.length(); i++)
        {
            if (!Character.isDigit(str.charAt(i)))
            {
                  return;
            }
        }
        if (str == null)
            return;
        if ((getLength() + str.length()) <= limit)
        {
        super.insertString(offset, str, a);
        }
    }
  }
  public static void main(String[] args)
  {
      ret12 r=new ret12();
  }
}

it executs perfectly

thank you