Android

Android Programming Tutorial

                Android is an open source and Linux-based operating system for mobile devices such as smartphones and tablet computers. Android was developed by the Open Handset Alliance, led by Google, and other companies.

                This tutorial will teach you basic Android programming and will also take you through some advance concepts related to Android application development.

                  This tutorial has been prepared for the beginners to help them understand basic Android programming. After completing this tutorial you will find yourself at a moderate level of expertise in Android programming.

Features of Android

The Android features are listed below  
                                             
Android Applications
                    Android applications are usually developed in the Java language using the Android Software Development Kit.

                                  Once developed, Android applications can be packaged easily and sold out either through a  Google Play Store or the Amazon Appstore.

     The requirements of android application development are

           1.JAVA JDK 5/JDK6/JDK7

            2.Android SDK

            3.Android development tool(ADT) Eclipse Plugin

             4.Eclipse IDE for JAVA developers


     Here last two components are optional and if you are working on Windows machine then these components make your life easy while doing Java based application development. So let us have a look how to proceed to set required environment.


Here you can learn about Android applications with Intents and Activity:

  • HelloWorld Application in Android

     In this article you are going to see how to create  a hello world applictaion.

1.Open Eclipse IDE 

2.Click File -> New -> Project -> Android Application Project

3.Enter the name of the application  as HelloWorld  and click next 

4.Make sure that you have enabled the create project in workspace

5.Design application launcher icon and create a blank activity workspace

6.Enter the main activity details.
                                                 




Listview Tutorial            
  
                                      In this tutorial i will be demostrating to build a simple listview .In this project we are going to create a simple listview .

1)Create a new android application project in eclipse 

2)Then after craeting a new project you have to edit the activity_mail.xml file , As shown below

activity_main.xml

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

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>


   3)After editing the activity_main.xml file now you have to code the main activity  file as shown below

Mainactivity.java

package com.example.listviewbasic;

import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
String[] countries = new String[] {
"apple",
"grapes",
"orange",
"mango",
"water melon",

};


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
     
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);       
        ListView listView = (ListView) findViewById(R.id.listView1);
       listView.setAdapter(adapter);       
    }
}

 4)Now the basic listview is ready



                                                              

DownloadSourceCode


ListView Animation Tutorial

In this tutorial you are going to know about the animation in lsitview so first learn to create a basic listview as shown above .

This tutorial will show you how to animate the list view using plenty of animation.

1)First  create a new android application as you created for basic listview.

2)Now you have to create a folder "anim" under res folder 

3)you have to create a different XML file inside the anim folder to create the animation.

4)Let  follow the XML as shown below,create the following xml file in the anim folder

*Fade In
                <?xml version="1.0" encoding="utf-8"?>
              <alpha   xmlns:android="http://schemas.android.com/apk/res/android"

                       android:duration="100"
                       android:fromAlpha="0.0"

                       android:interpolator="@android:anim/accelerate_interpolator"

                     android:toAlpha="1.0" />

*Push Left In
                        <?xml version="1.0" encoding="utf-8"?>
                 <set xmlns:android="http://schemas.android.com/apk/res/android">

                 <translate android:fromXDelta="100%p" android:toXDelta="0"

                     android:duration="300"/>
                 <alpha android:fromAlpha="0.0" android:toAlpha="1.0"

                   android:duration="300" />
               </set>

*Push Left Out
                           <?xml version="1.0" encoding="utf-8"?>
                  <set xmlns:android="http://schemas.android.com/apk/res/android">
                  <translate android:fromXDelta="0" android:toXDelta="-100%p"

                  android:duration="300"/>

                  <alpha android:fromAlpha="1.0" android:toAlpha="0.0"

                   android:duration="300" />
                  </set>

*Push Up In
                     <?xml version="1.0" encoding="utf-8"?>
               <set xmlns:android="http://schemas.android.com/apk/res/android">

               <translate android:fromYDelta="100%p" android:toYDelta="0"

                android:duration="500"/>
             <alpha android:fromAlpha="0.0" android:toAlpha="1.0"

              android:duration="500" />
           </set>

*Push Up Out
                        <?xml version="1.0" encoding="utf-8"?>
                     <set xmlns:android="http://schemas.android.com/apk/res/android">
                    <translate android:fromYDelta="0" android:toYDelta="-100%p"

                      android:duration="500"/>
                   <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
                      android:duration="500" />
                     </set>

*Hyper Space In
                               <?xml version="1.0" encoding="utf-8"?>
                   <alpha xmlns:android="http://schemas.android.com/apk/res/android" 
                 android:fromAlpha="0.0" android:toAlpha="1.0" 
                 android:duration="300" android:startOffset="1200" />

*Hyper Space Out
                               <?xml version="1.0" encoding="utf-8"?>
                     <set xmlns:android="http://schemas.android.com/apk/res/android"                              android:shareInterpolator="false">  
               <scale
               android:interpolator="@android:anim/accelerate_decelerate_interpolator"
                android:fromXScale="1.0"
                android:toXScale="1.4"

                 android:fromYScale="1.0"

                 android:toYScale="0.6"
                 android:pivotX="50%"
                 android:pivotY="50%"
                 android:fillAfter="false"
                 android:duration="700" />
             <set
                android:interpolator="@android:anim/accelerate_interpolator"
                android:startOffset="700">

                <scale
                android:fromXScale="1.4"
                android:toXScale="0.0"
               android:fromYScale="0.6"
               android:toYScale="0.0"
               android:pivotX="50%"
               android:pivotY="50%"
               android:duration="400" />
             <rotate
              android:fromDegrees="0"
              android:toDegrees="-45"
              android:toYScale="0.0"
              android:pivotX="50%"
              android:pivotY="50%"
              android:duration="400" />

            </set>

            </set>

*Shake

               <?xml version="1.0" encoding="utf-8"?>
          <translate xmlns:android="http://schemas.android.com/apk/res/android"
          android:fromXDelta="0" android:toXDelta="10"
           android:duration="1000" android:interpolator="@anim/cycle" />

*Wave Scale

                      <set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
     <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="100" />

    <scale
        android:fromXScale="0.5" android:toXScale="1.5"
        android:fromYScale="0.5" android:toYScale="1.5"
        android:pivotX="50%" android:pivotY="50%"
        android:duration="200" />
    <scale

        android:fromXScale="1.5" android:toXScale="1.0"
        android:fromYScale="1.5" android:toYScale="1.0"
        android:pivotX="50%" android:pivotY="50%"
        android:startOffset="200"
        android:duration="100" />
</set>

*Slide In Top
                       <?xml version="1.0" encoding="utf-8"?>
                      <set xmlns:android="http://schemas.android.com/apk/res/android"

                    android:interpolator="@android:anim/accelerate_interpolator">
                    <translate android:fromYDelta="-100%" android:toXDelta="0"

                    android:duration="100" />

                   <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
                   android:duration="50" />

                   </set>
*Slide In Top To Bottom
                    <?xml version="1.0" encoding="utf-8"?>
                         <set>
                           <set xmlns:android="http://schemas.android.com/apk/res/android"
                           android:interpolator="@android:anim/accelerate_interpolator">
                          <translate android:fromYDelta="-100%" android:toXDelta="0"

                          android:duration="100" />

                            <alpha android:fromAlpha="0.0" android:toAlpha="1.0"

                            android:duration="50" />
                        </set>
    

                         </set>

*Cycle
             <cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
                   android:cycles="7" />

After creating the XML file now its time to code the MainActivity.java .

MainActivity.java
                        package com.example.listviewanimation;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

private ListView listview;
private DisplayMetrics metrics;

private int mode = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

listview = new ListView(this);
listview.setFadingEdgeLength(0);
ArrayList<String> strings = new ArrayList<String>();

for (int i = 0; i < 300; i++) {
strings.add("Item:#" + (i + 1));
}

MainAdapter mAdapter = new MainAdapter(this, strings, metrics);

listview.setAdapter(mAdapter);

setContentView(listview);
}

public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE, 1, 0, "TranslateAnimation1");
menu.add(Menu.NONE, 2, 0, "TranslateAnimation2");
menu.add(Menu.NONE, 3, 0, "ScaleAnimation");
menu.add(Menu.NONE, 4, 0, "fade_in");
menu.add(Menu.NONE, 5, 0, "hyper_space_in");
menu.add(Menu.NONE, 6, 0, "hyper_space_out");
menu.add(Menu.NONE, 7, 0, "wave_scale");
menu.add(Menu.NONE, 8, 0, "push_left_in");
menu.add(Menu.NONE, 9, 0, "push_left_out");
menu.add(Menu.NONE, 10, 0, "push_up_in");
menu.add(Menu.NONE, 11, 0, "push_up_out");
menu.add(Menu.NONE, 12, 0, "shake");
return result;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
mode = item.getItemId();
return super.onOptionsItemSelected(item);
}

public class MainAdapter extends ArrayAdapter<String> {
private Context context;
private LayoutInflater mInflater;
private ArrayList<String> strings;
private DisplayMetrics metrics_;

private class Holder {
public TextView textview;
}

public MainAdapter(Context context, ArrayList<String> strings,
DisplayMetrics metrics) {
super(context, 0, strings);
this.context = context;
this.mInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.strings = strings;
this.metrics_ = metrics;
}

@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
final String str = this.strings.get(position);
final Holder holder;

if (convertView == null) {
convertView = mInflater.inflate(
android.R.layout.simple_list_item_1, null);
convertView.setBackgroundColor(0xFF202020);

holder = new Holder();
holder.textview = (TextView) convertView
.findViewById(android.R.id.text1);
holder.textview.setTextColor(0xFFFFFFFF);
holder.textview.setBackgroundResource(R.drawable.background);

convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}

holder.textview.setText(str);

Animation animation = null;

switch (mode) {
case 1:
animation = new TranslateAnimation(metrics_.widthPixels / 2, 0,
0, 0);
break;

case 2:
animation = new TranslateAnimation(0, 0, metrics_.heightPixels,
0);
break;

case 3:
animation = new ScaleAnimation((float) 1.0, (float) 1.0,
(float) 0, (float) 1.0);
break;

case 4:
animation = AnimationUtils.loadAnimation(context, R.anim.fadein);
break;
case 5:
animation = AnimationUtils.loadAnimation(context, R.anim.hyperspacein);
break;
case 6:
animation = AnimationUtils.loadAnimation(context, R.anim.hyperspaceout);
break;
case 7:
animation = AnimationUtils.loadAnimation(context, R.anim.wavescale);
break;
case 8:
animation = AnimationUtils.loadAnimation(context, R.anim.pushleftin);
break;
case 9:
animation = AnimationUtils.loadAnimation(context, R.anim.pushleftout);
break;
case 10:
animation = AnimationUtils.loadAnimation(context, R.anim.pushupin);
break;
case 11:
animation = AnimationUtils.loadAnimation(context, R.anim.pushupout);
break;
case 12:
animation = AnimationUtils.loadAnimation(context, R.anim.shake);
break;
}

// animation.setDuration(500);
convertView.startAnimation(animation);
animation = null;

return convertView;
}

}
}


Now the Listview Animation is ready ......

                                                                        



                                                                     

Expandable ListView:
  •  Expandable ListView is used to group, that can individually expanded to show its children.
  •  When the user touches the header it has the ability to expand and collapse the group.
  •   In this example you can learn how to create a simple expandable listview as shown below. 
                                               

  
  1)Lets start a new project in eclipse as we created for earlier projects
  2)we need three xml to create a expandable list view,first xml is for the main list view ,second xml is for the list group and the third xml is for  child list view.
 3)Design your activity_main.xml with Expandable listview as shown below.

                                                  

      4)Create a list_group.xml for the listview group and code as follows
                                             

5)Create another xml file named list_item.xml it contaoin only listview item.
                                             
6)Here we are going to use a custom adapter class to create the expandable listview,create a new class file named Expandablelistadpater.java and extend this from Baseexpandablelistadapter.java

  7)This Baseexpandablelistadapter provide some requierd methods to render listview
  • Getgroupview( )-listgroup header view are returned
  • Getchildview ( )-listchild view are returned
ExpandableListView.java:
 
 package com.example.expandablelist;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;


public class ExpandableListAdapter  extends BaseExpandableListAdapter{
    private Context _context;
    private List<String> _listDataHeader;
    private HashMap<String, List<String>> _listDataChild;
    public ExpandableListAdapter(Context context,List<String> listDataHeader,HashMap<String, List<String>> listDataChild){
        this._context = context ;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listDataChild;
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return this._listDataHeader.size();    
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosition);
    }

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

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.textView1);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        final String childText = (String)getChild(groupPosition, childPosition);
        if(convertView == null){
            LayoutInflater infalInflater = (LayoutInflater)this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.textView1);

        txtListChild.setText(childText);
        return convertView;
        // TODO Auto-generated method stub
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return false;
    }

}
   8) Next its time to code the main activity   here i split and post the main activity ibn to two files .
                                             

                                                                            

      9)Add these three lines at  the end of the main activity which is used for header and child data 
            listDataChild.put(listDataHeader.get(0), top250);
            listDataChild.put(listDataHeader.get(1), nowShowing);
            listDataChild.put(listDataHeader.get(2),comingSoon);     
 10)Now its time to run and test the project ,check with your emulator or device .
                                         

Speech To Text
                                            


1)Lets start a new project in eclipse as we created for earlier projects.

2)Change the layout ,we have to change activity_main.xml appearence by using a image button and a text view as show below.
              
 3)Now you have to code it in java to trigger Speech To Text

4)Open the Main Activity.java  and replace the code as shown below
                                                    

      Here i split the main activity files in to two 
                                                                         

5)Now run and test your Project You will get the Output as shown below.

Download SourceCode                                              




0 comments:

Post a Comment

Total Pageviews

Contact Form

Name

Email *

Message *