Saturday, July 14, 2012

Creating menu in Android



Click Here to download source code

Package Name  :   selva.menu

Project Name    :   Menu

Version             :   1.5 (Supports 1.5 and above versions) 

main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>


MenuActivity.java


package selva.menu;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class  MenuActivity  extends Activity
{

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }
   
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    CreateMenu(menu);
    return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    return MenuChoice(item);
    }

    private void CreateMenu(Menu menu)
    {
            MenuItem mnu1 = menu.add(0, 0, 0, "Item 1");
            {
                    mnu1.setAlphabeticShortcut('a');
                    mnu1.setIcon(R.drawable.image1);
            }
            MenuItem mnu2 = menu.add(0, 1, 1, "Item 2");
            {
                    mnu2.setAlphabeticShortcut('b');
                    mnu2.setIcon(R.drawable.image2);
            }
            MenuItem mnu3 = menu.add(0, 2, 2, "Item 3");
            {
                    mnu3.setAlphabeticShortcut('c');
                    mnu3.setIcon(R.drawable.image3);
            }
            MenuItem mnu4 = menu.add(0, 3, 3, "Item 4");
            {
                    mnu4.setAlphabeticShortcut('d');
            }
            menu.add(0, 4,4, "Item 5");
            menu.add(0, 5,5, "Item 6");
            menu.add(0, 6,6, "Item 7");
    }
    private boolean MenuChoice(MenuItem item)
    {
            switch (item.getItemId())
            {
            case 0:
                Toast.makeText(this, "You clicked on Item 1",Toast.LENGTH_LONG).show();
                return true;
            case 1:
                Toast.makeText(this, "You clicked on Item 2",Toast.LENGTH_LONG).show();
                return true;
            case 2:
                Toast.makeText(this, "You clicked on Item 3",Toast.LENGTH_LONG).show();
                return true;
            case 3:
                Toast.makeText(this, "You clicked on Item 4",Toast.LENGTH_LONG).show();
                return true;
            case 4:
                Toast.makeText(this, "You clicked on Item 5",Toast.LENGTH_LONG).show();
                return true;
            case 5:
                Toast.makeText(this, "You clicked on Item 6",Toast.LENGTH_LONG).show();
                return true;

            case 6:
                Toast.makeText(this, "You clicked on Item 7",Toast.LENGTH_LONG).show();
                return true;
            }
            return false;
    }

}


OUTPUT:














































Click home button



















































Click Item2











































Click home button again














































Click Item6












































Click Here to download source code


Friday, July 13, 2012

Gallery View in Android



Click Here to Download  Source Code

Package Name  :  selva.gallery

Project Name   :  Gallery

Version Name   :  1.5 (Supports 1.5 and above versions)

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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Images" />
    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/image1"
        android:layout_width="320px"
        android:layout_height="250px"
        android:scaleType="fitXY" />
</LinearLayout>


 note: res/values/img.xml

img.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery1">
<attr name="android:galleryItemBackground"/>
</declare-styleable>
</resources>



GalleryActivity.java


package selva.gallery;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

public class GalleryActivity extends Activity {
   
    //---the images to display---
    Integer[] imageIDs = {
    R.drawable.image1,
    R.drawable.image2,
    R.drawable.image3,
    R.drawable.image4,
    R.drawable.image5,
    R.drawable.image6,
        };
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Gallery gallery = (Gallery) findViewById(R.id.gallery1);
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setOnItemClickListener(new OnItemClickListener()
        {
        public void onItemClick(AdapterView<?> parent, View v,
        int position, long id)
        {
        Toast.makeText(getBaseContext(),
        "pic" + (position + 1) + "selected",
        Toast.LENGTH_SHORT).show();
        }
        });
    }
   
    public class ImageAdapter extends BaseAdapter
    {
      private Context context;
      private int itemBackground;
       public ImageAdapter(Context c)
       {
          context = c;
          //---setting the style---
          TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
          itemBackground = a.getResourceId(
          R.styleable.Gallery1_android_galleryItemBackground, 0);
          a.recycle();
       }
       //---returns the number of images---
       public int getCount()
       {
           return imageIDs.length;
       }
           //---returns the ID of an item---
       public Object getItem(int position)
       {
           return position;
       }
       //---returns the ID of an item---
       public long getItemId(int position)
       {
           return position;
       }
       //---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent)
    {
    ImageView imageView = new ImageView(context);
    imageView.setImageResource(imageIDs[position]);
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
    imageView.setBackgroundResource(itemBackground);
 
    return imageView;
    }
    } 
}


OUTPUT:














































Click Here to Download  Source Code





Display several items in one row of a Listview in Android



Click here to download source code

Package Name  :  selva.list

Project Name   :  ListView3

Version              : 1.5 (Support 1.5 and above versions) 

main.xml

<?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"
    android:orientation="vertical" >
   
  <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:focusableInTouchMode="true"
    android:focusable="true">
   
       
       
     <HorizontalScrollView
         android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:focusableInTouchMode="true"
        android:focusable="true">
  
        <TableLayout
        android:id="@+id/table1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:focusableInTouchMode="true"
        android:focusable="true">
          
           
        </TableLayout>
     </HorizontalScrollView>
    
  </ScrollView>

   

</RelativeLayout>



shape.xml

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" >
        <shape>
            <gradient
                android:startColor="#DDA0DD"
                android:endColor="#ffff00"
                android:angle="270" />
                       <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
      
    </item>

    <item android:state_focused="true" >
        <shape>
            <gradient
                android:endColor="#ff00ff"
                android:startColor="#000fff"
                android:angle="270" />
            <stroke
                android:width="3dp"
                android:color="#f0f0f0" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item>       
        <shape>
            <gradient
                android:endColor="#000000"
                android:startColor="#000000"
                android:angle="270" />
            <stroke
                android:width="1dp"
              
                android:color="#000000" />
            <corners
                android:radius="3dp"
                />
            <padding
                android:left="1dp"
                android:top="1dp"
                android:right="1dp"
                android:bottom="10dp" />
        </shape>
        
    </item>
   
</selector>










 

Listview2Activity.java

package selva.list;



import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TableRow.LayoutParams;
import android.widget.Toast;

public class Listview2Activity extends Activity

{
    // name and age is equal length array
    String[] name =
        {
            "Apple",
            "Banana",
            "Orange",
            "Mango",
            "Grapes",
            "Jack Fruit",
            "Strawberry",
            "cucumber",
            "pumpkin",
            "pine Apple"
        };
    String[] age =
        {
            "20",
            "21",
            "22",
            "23",
            "24",
            "25",
            "26",
            "27",
            "28",
            "29"
        };
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
       
       TableLayout tv=(TableLayout) findViewById(R.id.table1);
        tv.removeAllViewsInLayout();
       
        int Fruits_length=name.length;
        int i=0;
       
        while(i<Fruits_length)
        {
          
   
        String str1 = "<font color=#0099ff>Name</font> <br/> <font color=#ffffff> "+name[i]+" </font>";
       
        String str2="<font color=#0099ff>Age</font> <br/> <font color=#ffffff> "+age[i]+" </font>";


        final TableRow tr=new TableRow(Listview2Activity.this);

            tr.setLayoutParams(new LayoutParams(
                       LayoutParams.FILL_PARENT,
                       LayoutParams.WRAP_CONTENT));
          
             
                 tr.setId(i);
               
               
               
                final TextView b1=new TextView(Listview2Activity.this);
                   b1.setTextSize(15);
                   b1.setText(Html.fromHtml(str1));
                   b1.setWidth(200);
                tr.addView(b1);
              
                final TextView b2=new TextView(Listview2Activity.this);
                b2.setTextSize(15);
                b2.setText(Html.fromHtml(str2));
                b2.setWidth(200);
             tr.addView(b2);
          

                 tr.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape));
               tv.addView(tr);
              
              
            final View vline1 = new View(Listview2Activity.this);
          vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
          vline1.setBackgroundColor(Color.GREEN);
          tv.addView(vline1);      
     
                        
                    tr.setOnClickListener(new View.OnClickListener() {
                      
                        @Override
                        public void onClick(View v) {
                                          
                           
                            String e=String.valueOf(tr.getId());
                          
                            Toast.makeText(getApplicationContext(), e, Toast.LENGTH_SHORT).show(); 
                  
                          
                        }
                    });
                  
        i=i+1;
        }

       
    }
}


OUTPUT:













































click first row. You will be get row number as














































click fifth row. You will be get row number as 4

 








































Click here to download source code


ListView Using Dynamic Table



Click Here To Download Source code

Package Name   :   selva.list

Project  Name    :   ListView2

Version              :   1.5 (Supports 1.5 and above versions)

main.xml

<?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"
    android:orientation="vertical" >
   
  <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:focusableInTouchMode="true"
    android:focusable="true">
   
       
       
     <HorizontalScrollView
         android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:focusableInTouchMode="true"
        android:focusable="true">
  
        <TableLayout
        android:id="@+id/table1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:focusableInTouchMode="true"
        android:focusable="true">
          
           
        </TableLayout>
     </HorizontalScrollView>
    
  </ScrollView>

</RelativeLayout> 

note: path name ---->   res/drawable/shape.xml
shape.xml

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" >
        <shape>
            <gradient
                android:startColor="#DDA0DD"
                android:endColor="#ffff00"
                android:angle="270" />
                       <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
      
    </item>

    <item android:state_focused="true" >
        <shape>
            <gradient
                android:endColor="#ff00ff"
                android:startColor="#000fff"
                android:angle="270" />
            <stroke
                android:width="3dp"
                android:color="#f0f0f0" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item>       
        <shape>
            <gradient
                android:endColor="#000000"
                android:startColor="#000000"
                android:angle="270" />
            <stroke
                android:width="1dp"
              
                android:color="#000000" />
            <corners
                android:radius="3dp"
                />
            <padding
                android:left="1dp"
                android:top="1dp"
                android:right="1dp"
                android:bottom="10dp" />
        </shape>
        
    </item>
   
</selector>


package selva.list;



import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TableRow.LayoutParams;
import android.widget.Toast;

public class Listview2Activity extends Activity

{
   
    String[] Fruits =
        {
            "Apple",
            "Banana",
            "Orange",
            "Mango",
            "Grapes",
            "Jack Fruit",
            "Strawberry",
            "cucumber",
            "pumpkin"
        };
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
       
       TableLayout tv=(TableLayout) findViewById(R.id.table1);
        tv.removeAllViewsInLayout();
       
        int Fruits_length=Fruits.length;
       
        for(int i=0;i<Fruits_length;i++)
        {
      
       
        String str=Fruits[i];


        final TableRow tr=new TableRow(Listview2Activity.this);

            tr.setLayoutParams(new LayoutParams(
                       LayoutParams.FILL_PARENT,
                       LayoutParams.WRAP_CONTENT));
           
              
                 tr.setId(i);
                
                
                
                final TextView b1=new TextView(Listview2Activity.this);
                   b1.setTextSize(15);
                   b1.setText(str);
                   b1.setWidth(300);
                tr.addView(b1);
           

                 tr.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape));
               tv.addView(tr);
               
               
            final View vline1 = new View(Listview2Activity.this);
          vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
          vline1.setBackgroundColor(Color.GREEN);
          tv.addView(vline1);       
        
                         
                    tr.setOnClickListener(new View.OnClickListener() {
                       
                        @Override
                        public void onClick(View v) {
                                           
                            
                            String e=(String) b1.getText();
                           
                            Toast.makeText(getApplicationContext(), e, Toast.LENGTH_SHORT).show();  
                              
       
                        }
                    });
                   
       
        }
       
         
    }
}


 
output:












































click apple











































click jack fruit











































 Click Here To Download Source code


Thursday, July 12, 2012

Display Storing Items in strings.xml file using ListView



Click Here to download source code

Package Name  :   selva.list

Project  Name   :   ListView1

Version             :   1.5 ( Supports 1.5 and above Versions)

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>


strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, ListView1Activity!</string>
    <string name="app_name">ListView1</string>
  <string-array name="Fruits">
    <item>Apple</item>
    <item>Banana</item>
    <item>Orange</item>
    <item>Mango</item>
    <item>Grapes</item>
    <item>Pine Apple</item>
    <item>Jack Fruit</item>
    <item>Strawberry</item>
    <item>Cucumber</item>
    <item>Pumpkin</item>
  </string-array>
</resources>


ListView1Activity.java

package selva.list;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListView1Activity extends ListActivity {
    /** Called when the activity is first created. */
   
    String Fruits[];
    @Override
public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        ListView lstView = getListView();
        //lstView.setChoiceMode(0); //CHOICE_MODE_NONE
        //lstView.setChoiceMode(1); //CHOICE_MODE_SINGLE
        lstView.setChoiceMode(2); //CHOICE_MODE_MULTIPLE
        lstView.setTextFilterEnabled(true);
        Fruits=getResources().getStringArray(R.array.Fruits);
        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, Fruits));
    }
public void onListItemClick(ListView parent, View v, int position, long id)
    {
    parent.setItemChecked(position, parent.isItemChecked(position));
    Toast.makeText(this,"You have selected " + Fruits[position],Toast.LENGTH_SHORT).show();
    }

}


OUTPUT: 












































Click Here to download source code


Customizing The ListView In Android



Click Here To Download Source Code

Package Name  :  selva.list

Project Name    :  Cust_ListView

Version             :  1.5 (Supports 1.5 and above versions)

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>


 Cust_ListviewActivity.java



package selva.list;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Cust_ListviewActivity extends ListActivity {
    String[] Fruits = {
            "Apple",
            "Banana",
            "Orange",
            "Mango",
            "Grapes",
            "Jack Fruit",
            "Strawberry",
            "cucumber",
            "pumpkin"
        };
   
    /** Called when the activity is first created. */
    @Override
public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        ListView lstView = getListView();
        //lstView.setChoiceMode(0); //CHOICE_MODE_NONE
        //lstView.setChoiceMode(1); //CHOICE_MODE_SINGLE
        lstView.setChoiceMode(2); //CHOICE_MODE_MULTIPLE
        lstView.setTextFilterEnabled(true);
        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, Fruits));
// note : You will get pop-up menu when type R . layout .          
//Then select your option. It will display items with different customization
    }
public void onListItemClick(ListView parent, View v, int position, long id)
    {
    parent.setItemChecked(position, parent.isItemChecked(position));
    Toast.makeText(this,"You have selected " + Fruits[position],Toast.LENGTH_SHORT).show();
    }
}






OUTPUT:

 






















































































Click Here To Download Source Code