Monday, 21 December 2015

LIST VIEW AND SPINNER

03:27

ListView is an Android ViewGroup, used extensively to display the collection of data in vertical scrollable rows.
ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.

Spinner:
The spinner is akin to a drop-down list that displays a list of items, allowing the user to select the desired item.After the user Touches a Spinner or presses the center button of the D-pad, a list of  items is displayed,the user to select an item from the list.To populates the spinner control ,we use two methods: one via the string resource and the other via ArrayAdapter that act as a data Source.



Static list view example:




(1)write code For  listview.java :
package menu.test.om.com.myapplication;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class listview extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_listview);
        Toolbar toolbar = (Toolbar) findViewById(R.id.
toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.
fab);
        fab.setOnClickListener(
new View.OnClickListener() {
           
@Override
           
public void onClick(View view) {
                Snackbar.make(view,
"Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction(
"Action", null).show();
            }
        });
    }

   
@Override
   
public boolean onCreateOptionsMenu(Menu menu) {
       
// Inflate the menu; this adds items to the action bar if it is present.
       
getMenuInflater().inflate(R.menu.menu_listview, menu);
       
return true;
    }

   
@Override
   
public boolean onOptionsItemSelected(MenuItem item) {
       
// Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
       
int id = item.getItemId();

       
//noinspection SimplifiableIfStatement
       
if (id == R.id.action_settings) {
           
return true;
        }

       
return super.onOptionsItemSelected(item);
    }
}

(2)write  code for activity_listview.xml:
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

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

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".listview">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_listview" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>

(3)write    code for  conent_listview.xml  :
<?xml version="1.0" encoding="utf-8"?>

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

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_listview" tools:context=".listview">



    <TextView android:text="Hello World!" android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/textView" />



    <ListView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/listView"

        android:entries="@array/options"

         />

</RelativeLayout>

(4)write code for  arrays.xml :
<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string-array name="options">

        <item>Drinks</item>

        <item>Food</item>

        <item>Stores</item>





    </string-array>

</resources>


 output:
--------------------------------------------------------------------------------------------------------------------------

ListView Using ArrayAdapter:
--------------------------------------------------------------------------------------------------------------------------

  1. The list items are automatically inserted to the list using an Adapter and Adapter pulls data from data source source such as an array, cursor, etc.

2. Android Adapter

Adapter is acts as a bridge between data source and adapter views such as ListView, GridView. Adapter iterates through the data set from beginning till the end and generate Views for each item in the list.
Android SDK provides three different Adapter implementation, that includesArrayAdapterCursorAdapter and SimpleAdapter. An ArrayAdapter expects an Array or an List as input, while CursorAdapter accepts the instance of Cursor and SimpleAdapter maps the static data defined in the resources. The type of adapter that suits your app need is purely based on the input data type.
The BaseAdapter is the generic implementation for all of the three adapter types and can be used for ListView, GridView or for Spinners.
You may directly use ArrayAdapter by passing array as input or create your own customized class by extending BaseAdapter.
The simplest way for building list view is by using ArrayAdapter. Following are some of the steps used to implement simple ListView using array adapter.
  1. First step towards building list view is to identify the input data, which you want to display in list. In this example, we will be using a static array of strings.
  2. Secondly, let us declare list view in activity layout. In our example, the activity layout contains a list view inside linear layout. Provide android:id="@+id/months_list" as ListView id.
  3. Now finally, let us instantiate the  the ArrayAdapter and set to ListView by calling setAdapter() method.

(1)write code for listview.java :

package menu.test.om.com.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class listview extends AppCompatActivity {
   
private String[] monthsArray = { "JAN", "FEB", "MAR", "APR", "MAY", "JUNE", "JULY",
           
"AUG", "SEPT", "OCT", "NOV", "DEC" };

   
private ListView monthsListView;
   
private ArrayAdapter arrayAdapter;

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

       
monthsListView = (ListView) findViewById(R.id.listView);

       
// this-The current activity context.
        // Second param is the resource Id for list layout row item
        // Third param is input array
       
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, monthsArray);

       
monthsListView.setAdapter(arrayAdapter);
    }
}

(2)write code for activity_listview.xml:
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

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

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".listview">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_listview" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>
li
(3)write  code for content_listview.xml:
<?xml version="1.0" encoding="utf-8"?>

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

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_listview" tools:context=".listview">



    <TextView android:text="Hello World!" android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/textView" />



    <ListView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/listView"

         />

</RelativeLayout>

output:
================================================================================================================================================

onItemClick() Event in  (static) Listview   simple application Example :



(1)First    write  code  for  listview.java  :
package menu.test.om.com.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ListView;

public class listview extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_listview);


        AdapterView.OnItemClickListener itemClickListener;
        itemClickListener =
new AdapterView.OnItemClickListener() {
           
@Override
           
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               
if(position==0)
                {
                    Intent i =
new Intent(listview.this, DrinkCategory.class);
                    startActivity(i);

                }
               
if(position==1)
                {
                    Intent i =
new Intent(listview.this, MainActivity.class);
                    startActivity(i);

                }
               
if(position==2)
                {

                    Intent i =
new Intent(listview.this, Main2Activity.class);
                    startActivity(i);
                }
            }
        };


        ListView l = (ListView)findViewById(R.id.
listView);
        l.setOnItemClickListener(itemClickListener);
        Toolbar toolbar = (Toolbar) findViewById(R.id.
toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.
fab);
        fab.setOnClickListener(
new View.OnClickListener() {
           
@Override
           
public void onClick(View view) {
                Snackbar.make(view,
"Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction(
"Action", null).show();
            }
        });


    }

   
@Override
   
public boolean onCreateOptionsMenu(Menu menu) {
       
// Inflate the menu; this adds items to the action bar if it is present.
       
getMenuInflater().inflate(R.menu.menu_listview, menu);
       
return true;
    }

   
@Override
   
public boolean onOptionsItemSelected(MenuItem item) {
       
// Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
       
int id = item.getItemId();

       
//noinspection SimplifiableIfStatement
       
if (id == R.id.action_settings) {
           
return true;
        }

       
return super.onOptionsItemSelected(item);
    }
}

(2)write code for  activity_listview.xml :
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

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

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".listview">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_listview" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>
 

(3)write   code for content_listview.xml   :
<?xml version="1.0" encoding="utf-8"?>

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

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_listview" tools:context=".listview">



    <TextView android:text="Hello World!" android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/textView" />



    <ListView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/listView"

        android:entries="@array/options"

         />

</RelativeLayout>

(4)write code for  arrays.xml :
<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string-array name="options">

        <item>Drinks</item>

        <item>Food</item>

        <item>Stores</item>





    </string-array>

</resources>


 output:


after click on Drinks means on positon ==0  output will be:

after click on Drinks means on positon ==1  output will be:


after click on Drinks means on positon ==2  output will be:

================================================================================================================================================


Listview    example using  implements of   OnItemClickListener:

(1)write code for MainActivity.java file:
package testing.om.com.listview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements OnItemClickListener {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        ListView l = (ListView)findViewById(R.id.
listView);
        l.setOnItemClickListener(
this);

    }

   
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
       
if(position==0)
        {
            Toast.makeText(getApplicationContext(),
"position zero",Toast.LENGTH_LONG).show();
        }
       
if(position==1)
        {
            Toast.makeText(getApplicationContext(),
"position one",Toast.LENGTH_LONG).show();
        }
       
if(position==2)
        {

            Toast.makeText(getApplicationContext(),
"position two",Toast.LENGTH_LONG).show();
        }

    }


}

(2)WRITE   CODE   FOR   activity_main.xml   file :
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

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

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true"

    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_main" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>

(3)write  code for content_main.xml file :
<?xml version="1.0" encoding="utf-8"?>

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

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main" tools:context=".MainActivity">



    <TextView android:text="Hello World!" android:layout_width="wrap_content"

        android:layout_height="wrap_content" />



    <ListView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/listView"

        android:entries="@array/options"

        android:layout_alignParentRight="true"

        android:layout_alignParentEnd="true" />



</RelativeLayout>

(4)write  code for strings.xml file:
<resources>

    <string name="app_name">listview</string>

    <string name="action_settings">Settings</string>



    <string-array name="options">



        <item>Drinks</item>



        <item>Food</item>



        <item>Stores</item>











    </string-array>



</resources>




================================================================================================================================================
Dynamic  listview Application example:



(1)first write code for  DrinkCategory.java :
package com.om.test.listviewexample;

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

public class DrinkCategory extends ListActivity
{


   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);


        ListView listView = getListView();

        ArrayAdapter<Drinks> listAdapter =
new ArrayAdapter<Drinks>(this,android.R.layout.simple_list_item_1,Drinks.drinks);

       listView.setAdapter(listAdapter);



    }

   
@Override
   
protected void onListItemClick(ListView l, View v, int position, long id) {
        Intent i =
new Intent(DrinkCategory.this,DrinkDetails.class);
        i.putExtra(
"EXTRA_DRINK_NO",(int)id);
        startActivity(i);


    }
}

(2)write code for  activity_drink_category.xml:
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

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

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true"

    tools:context="com.om.test.listviewexample.DrinkCategory">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />




    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_drink_category" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>
 
(3)write  code  for content_drink_category.xml:
 
<?xml version="1.0" encoding="utf-8"?>

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

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_drink_category"

    tools:context="com.om.test.listviewexample.DrinkCategory">



</RelativeLayout>
 
(4)write  code for Drinks.java :
 
package com.om.test.listviewexample;



/**

 * Created by om maurya

 */

public class Drinks {







    String name;

    String desc;

    int imageResouceId;



    private    Drinks(String name, String desc, int resId)

    {

        this.name=name;

        this.desc = desc;

        this.imageResouceId=resId;



    }





    public static final Drinks[] drinks = {

            new Drinks("Latte", "Latte desc" , 0),

            new Drinks("Latte2", "Latte2 desc" , 0),

            new Drinks("Latte3", "Latte3 desc" , 0)



};











    @Override

    public String toString() {

        return this.name;

    }

}
 
 
 
 
 (5)now write code for  DrinksDetails.java:
package com.om.test.listviewexample;



import android.os.Bundle;

import android.support.design.widget.FloatingActionButton;

import android.support.design.widget.Snackbar;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;

import android.view.View;

import android.widget.TextView;



public class DrinkDetails extends AppCompatActivity {



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_drink_details);







        int drinkNo = (int) getIntent().getExtras().getInt("EXTRA_DRINK_NO");





        Drinks drink = Drinks.drinks[drinkNo];



        TextView drinkTV, descTV=null;



        drinkTV = (TextView) findViewById(R.id.drink);

        descTV = (TextView) findViewById(R.id.desc);



        drinkTV.setText(drink.name);

        descTV.setText(drink.desc);









        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);



        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

        fab.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)

                        .setAction("Action", null).show();

            }

        });

    }



}
(6)write code for activity_drink_details.xml:
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

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

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true"

    tools:context="com.om.test.listviewexample.DrinkDetails">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_drink_details" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>
 
(7)write code for content_drink_details.xml:
<?xml version="1.0" encoding="utf-8"?>

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

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_drink_details"

    tools:context="com.om.test.listviewexample.DrinkDetails">



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge"

        android:text="Large Text"

        android:id="@+id/drink"

        android:layout_alignParentTop="true"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true"

        android:layout_marginTop="77dp" />



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceMedium"

        android:text="Medium Text"

        android:id="@+id/desc"

        android:layout_below="@+id/textView"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true" />

</RelativeLayout>
 
 
 output:



after click on   listview  you will get output :


 

================================================================================================================================================

DYNAMIC   LISTVIEW   EXAMPLE:

(1)write    Code for MainActivity.java   file  :
package testing.om.com.myapplication;

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 MainActivity extends ListActivity {
    String []
presidents={"hello","om maurya"};

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        ArrayAdapter adp=
new ArrayAdapter(this,android.R.layout.simple_list_item_1,presidents);
        setListAdapter(adp);
    }

   
public void onListItemClick(ListView parent,View v,int position,long id)
    {
        Toast.makeText(
this,"you have selected"+presidents[position], Toast.LENGTH_LONG).show();
    }
}

(2)Write  code for  activity_main.xml  file:
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

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

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true"

    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_main" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>

(3)Write   code for   content_main.xml file :
<?xml version="1.0" encoding="utf-8"?>

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

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main" tools:context=".MainActivity">



    <TextView android:text="Hello World!" android:layout_width="wrap_content"

        android:layout_height="wrap_content" />

</RelativeLayout>



OUTPUT  :









================================================================================================================================================
Spinner example:



(1)write    code for MainActvity.java    file:
package spinner.test.om.com.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
       
final TextView selectedOpt = (TextView) findViewById(R.id.textView);
        Spinner spin = (Spinner) findViewById(R.id.
spinner);
       
final String[] fruitsArray = getResources().getStringArray(R.array.fruits);
        spin.setOnItemSelectedListener(
new OnItemSelectedListener() {

           
@Override
           
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
               
selectedOpt.setText("you have selected" + fruitsArray[position]);
            }

           
@Override
           
public void onNothingSelected(AdapterView<?> adapterView) {
               
selectedOpt.setText("");
            }
        });

    }


}

(2)write code for activity_main.xml  file:

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

<android.support.design.widget.CoordinatorLayout

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

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true"

    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_main" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>

(3)content_main.xml   file:


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

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

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main" tools:context=".MainActivity">



    <Spinner

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/spinner"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="147dp"

        android:spinnerMode="dropdown"

        android:entries="@array/fruits"/>



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge"

        android:text="Large Text"

        android:id="@+id/textView"

        android:layout_alignParentTop="true"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true" />

</RelativeLayout>

(4)write   code for arrays.xml  file :
<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string-array name="fruits">

        <item>Apple</item>

        <item>Mango</item>

        <item>Orange</item>

        <item>Grapes</item>

        <item>Banana</item>

    </string-array>

</resources>


Output:

================================================================================================================================================
Spinner  Example   for    using   OnItemSelectedListener :
(1)write code for MainActivity.java file:
package testing.om.com.spinner;



import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.Spinner;

import android.widget.TextView;

import android.widget.Toast;



public  class MainActivity extends  AppCompatActivity  implements OnItemSelectedListener

{

     TextView selectedOpt;



     String[] fruitsArray;







    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        selectedOpt = (TextView) findViewById(R.id.textView);

        fruitsArray = getResources().getStringArray(R.array.fruits);

        Spinner spin = (Spinner) findViewById(R.id.spinner);

        spin.setOnItemSelectedListener(this);

    }



    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id)

    {

        if(position==0)

        {

            Toast.makeText(getApplicationContext(),"zero",Toast.LENGTH_LONG).show();



        }

        if(position==1)

        {



            Toast.makeText(getApplicationContext(),"one",Toast.LENGTH_LONG).show();

        }

        if(position==2)

        {

            Toast.makeText(getApplicationContext(),"two",Toast.LENGTH_LONG).show();



        }

    }



    @Override

    public void onNothingSelected(AdapterView<?> adapterView)

    {

        Toast.makeText(getApplicationContext(),"no selected",Toast.LENGTH_LONG).show();

    }





}
 
 (2)write  code for activity_main.xml file :
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout

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

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true"

    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_main" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>
 
(3)write  code for content_main.xml file:
<?xml version="1.0" encoding="utf-8"?>

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

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main" tools:context=".MainActivity">



    <TextView android:text="Hello World!" android:layout_width="wrap_content"

        android:layout_height="wrap_content"



        android:id="@+id/textView" />



    <Spinner

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/spinner"

        android:entries="@array/fruits"

        android:layout_below="@+id/textView"

        android:layout_toRightOf="@+id/textView"

        android:layout_toEndOf="@+id/textView"

        android:layout_marginTop="137dp" />

</RelativeLayout>

(4)write code for strings.xml file:
<resources>

    <string name="app_name">spinner</string>

    <string name="action_settings">Settings</string>

    <string-array name="fruits">



        <item>Apple</item>



        <item>Mango</item>



        <item>Orange</item>



        <item>Grapes</item>



        <item>Banana</item>



    </string-array>

</resources>



========================================================================================================================================================================================================================
Spinner  using ArrayAdapter :



(1)write    code for MainActvity.java    file:
package spinner.test.om.com.myapplication;



import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.Spinner;

import android.widget.TextView;

import android.widget.AdapterView.OnItemSelectedListener;



public class MainActivity extends AppCompatActivity {



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        final TextView selectedOpt = (TextView) findViewById(R.id.textView);

        Spinner spin = (Spinner) findViewById(R.id.spinner);

        final String[] fruits={"Apple","Mango","Orange","Grapes","Banana"};

        ArrayAdapter<String> arrayAdpt=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,fruits);

        spin.setAdapter(arrayAdpt);

        spin.setOnItemSelectedListener(new OnItemSelectedListener() {



            @Override

            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {

                selectedOpt.setText("you have selected" + fruits[position]);

            }



            @Override

            public void onNothingSelected(AdapterView<?> adapterView) {

                selectedOpt.setText("");

            }

        });



    }





}

(2)write code for activity_main.xml  file:

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

<android.support.design.widget.CoordinatorLayout

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

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

    android:layout_height="match_parent" android:fitsSystemWindows="true"

    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"

        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"

            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />



    </android.support.design.widget.AppBarLayout>



    <include layout="@layout/content_main" />



    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"

        android:src="@android:drawable/ic_dialog_email" />



</android.support.design.widget.CoordinatorLayout>

(3)content_main.xml    file:


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

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

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    tools:showIn="@layout/activity_main" tools:context=".MainActivity">



    <Spinner

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/spinner"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="147dp"

        android:spinnerMode="dropdown"

        android:entries="@array/fruits"/>



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge"

        android:text="Large Text"

        android:id="@+id/textView"

        android:layout_alignParentTop="true"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true" />


</RelativeLayout>


 output:


Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

© 2013 ANDROID TRAINING AT VISSICOMP BY OM SIR. All rights resevered. Designed by Templateism

Back To Top