Friday, July 13, 2012

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 


ListView in Android



Click Here to download source code

Package Name   :  selva.list

Project Name     :  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>




ListViewActivity.java


package selva.list;

import android.os.Bundle;
import android.app.ListActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class 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);
        setListAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, Fruits));
    }
public void onListItemClick(ListView parent, View v, int position, long id)
    {
    Toast.makeText(this,"You have selected " + Fruits[position],Toast.LENGTH_SHORT).show();
    }
}




OUTPUT : 











































Click item   "Apple"



































































































































Click Here to download source code


Wednesday, July 11, 2012

DatePickerDialog in Android



Click Here to Download source code 
 

Package Name    :   selva.datepicker 

Project Name      :   DatePickerDialog

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" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ShowDatepickerDialog" />

</LinearLayout>



DatePickerDialogActivity.java



package selva.datepicker;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;
import android.app.DatePickerDialog;
import java.util.Calendar;

public class DatePickerDialogActivity extends Activity
{
    int yr, month, day;
    static final int DATE_DIALOG_ID = 1;
    DatePicker datePicker;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
      
       
        Button btnOpen = (Button) findViewById(R.id.button1);
       
        btnOpen.setOnClickListener(new View.OnClickListener()
        {
        public void onClick(View v)
        {
      
             Calendar today = Calendar.getInstance();
             yr = today.get(Calendar.YEAR);
             month = today.get(Calendar.MONTH);
             day = today.get(Calendar.DAY_OF_MONTH);
             showDialog(DATE_DIALOG_ID);
           
        }
        });
       
    }
   
    @Override
    protected Dialog onCreateDialog(int id)
    {
        switch (id)
        {
  
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this, mDateSetListener, yr, month, day);
        }
    return null;
    }
   
    private DatePickerDialog.OnDateSetListener mDateSetListener =
    new DatePickerDialog.OnDateSetListener()
    {
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
    {
        yr = year;
    month = monthOfYear;
    day = dayOfMonth;
    Toast.makeText(getBaseContext(), "You have selected :" + (month + 1) + "/" + 
              day+ "/" + year,Toast.LENGTH_SHORT).show();
    }
    };
}



OUTPUT: 
























Click ShowDatepickerDialog button 






































select date and click set button 











































 Click Here to Download source code