Monday 23 September 2013

Android ListView Example Tutorial

Android Custom ListView Android Custom ListView
  1. Android ListView is most powerful component for displaying vertical scrollable list.
  2. For displaying item inside ListView component uses adapter.
  3. For Simple list used Android default adapter and For Customization use Custom adapter.ListView is also useful when displaying grouped data.For group data need Expandabale ListView.
  4. Here we will show you custom Android ListView.
  1. Create Android projects call AndroidCustomListView.For Custom ListView we need one xml file and some of fruit images.You have alredy one XML file so modify name of xml file main.xml to fruits_list.xml and download fruit images and put it into res/drawable folder and modify your XML file as below.
     <?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="wrap_content"  
       android:padding="5dp" >  
       
       <ImageView  
         android:id="@+id/image"  
         android:layout_width="50px"  
         android:layout_height="50px"  
         android:layout_marginLeft="5px"  
         android:layout_marginRight="20px"  
         android:layout_marginTop="5px"  
         android:src="@drawable/apple" >  
       </ImageView>  
       
       <TextView  
         android:id="@+id/name"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:text="@+id/label"  
         android:textSize="30px" >  
       </TextView>  
       
     </LinearLayout>  
    
  2. Now, Create one FruitsListAdapter.java file and modify as below. FruitsListAdapter extands ArrayAdapter which has getView() it is used to inflate layout as row so in that we have inflate our row file we have create fruits_list.xml So,Modify your FruitsListAdapter.java as below.
     package com.idroid.list.adaptor;  
       
     import com.idroid.listview.R;  
       
     import android.content.Context;  
     import android.view.LayoutInflater;  
     import android.view.View;  
     import android.view.ViewGroup;  
     import android.widget.ArrayAdapter;  
     import android.widget.ImageView;  
     import android.widget.TextView;  
       
     public class FruitsArrayAdapter extends ArrayAdapter<String> {  
          private final Context context;  
          private final String[] values;  
       
          public FruitsArrayAdapter(Context context, String[] values) {  
               super(context, R.layout.fruits_list, values);  
               this.context = context;  
               this.values = values;  
          }  
       
          @Override  
          public View getView(int position, View convertView, ViewGroup parent) {  
               LayoutInflater inflater = (LayoutInflater) context  
                         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
               View rowView = inflater.inflate(R.layout.fruits_list, parent, false);  
               TextView textView = (TextView) rowView.findViewById(R.id.name);  
               ImageView imageView = (ImageView) rowView.findViewById(R.id.image);  
               textView.setText(values[position]);  
       
               // Change icon based on name  
               String s = values[position];  
       
               System.out.println(s);  
       
               if (s.equals("Apple")) {  
                    imageView.setImageResource(R.drawable.apple);  
               } else if (s.equals("Gosseberry")) {  
                    imageView.setImageResource(R.drawable.gooseberry);  
               } else if (s.equals("Grape")) {  
                    imageView.setImageResource(R.drawable.grape);  
               } else if (s.equals("Nectarine")) {  
                    imageView.setImageResource(R.drawable.nectarine);  
               } else if (s.equals("Watermelon")) {  
                    imageView.setImageResource(R.drawable.watermelon);  
               } else {  
                    imageView.setImageResource(R.drawable.orange);  
               }  
       
               return rowView;  
          }  
     }  
       
    
  3. So now it time to populate ListView for that you need to create FruitsListActivity.java or you already have one Activity you can modify name of that it's fine.Modify FruitsListActivity.java as below.
     package com.idroid.listview;  
     import com.idroid.list.adaptor.FruitsArrayAdapter;  
     import android.app.ListActivity;  
     import android.os.Bundle;  
     import android.widget.ListView;  
     import android.widget.Toast;  
     import android.view.View;  
       
     public class FruitsListActivity extends ListActivity {  
       
          static final String[] FRUITS = new String[] { "Apple", "Gosseberry",  
                    "Grape", "Nectarine", "Watermelon" , "Orange"};  
       
          @Override  
          public void onCreate(Bundle savedInstanceState) {  
               super.onCreate(savedInstanceState);  
               setListAdapter(new FruitsArrayAdapter(this, FRUITS));  
          }  
       
          @Override  
          protected void onListItemClick(ListView l, View v, int position, long id) {  
       
               //get selected items  
               String selectedValue = (String) getListAdapter().getItem(position);  
               Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();  
       
          }  
     }  
    
  4. Now, Build and Run your project on simulator you should see above images like output and also when you click on any of the row you should see toast message.


Download Source Code :AndroidCustomListViewExample.rar

Wednesday 18 September 2013

GridView Example Android SDK Tutorial

Android GridView Example Android GridView Example
  1. Android GridView is most powerful and useful layout.
  2. GriedView is generally used when we want to show images,videos,icons.
  3. Also used to make audio player,video player,image gallary.
  4. Today, We show you how to create Custom GridView Example with Custom Adapter.
  1. Create new Android Project call AndroidGridViewExample,Download 15 images and put into res/drawable folder and create gridlayout.xml file and modify as given below.
     <?xml version="1.0" encoding="utf-8"?>  
     <GridView xmlns:android="http://schemas.android.com/apk/res/android"  
       android:id="@+id/grid_view"  
       android:layout_width="fill_parent"  
       android:layout_height="fill_parent"  
       android:numColumns="auto_fit"  
       android:columnWidth="100dp"  
       android:horizontalSpacing="5dp"  
       android:verticalSpacing="5dp"  
       android:gravity="center"  
       android:stretchMode="columnWidth" >   
       
     </GridView>  
    
  2. Now create one new layout in res/layout folder and give name image.xml file for displaying Full screen image like second image display below and modify source as above.
     <?xml version="1.0" encoding="utf-8"?>  
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
       android:layout_width="match_parent"  
       android:layout_height="match_parent"  
       android:orientation="vertical" >  
       
       <ImageView android:id="@+id/full_image_view"  
              android:layout_width="fill_parent"  
              android:layout_height="fill_parent"/>  
       
     </LinearLayout>  
    
  3. Create ImageAdapter.java in project src folder and modify as below also modify name of your images in array.
     package com.idroid.gridview;  
       
     import android.content.Context;  
     import android.view.View;  
     import android.view.ViewGroup;  
     import android.widget.BaseAdapter;  
     import android.widget.GridView;  
     import android.widget.ImageView;  
       
     public class ImageAdapter extends BaseAdapter {  
          private Context mContext;  
            
          public Integer[] mThumbIds = {  
                    R.drawable.image1, R.drawable.image2,  
                    R.drawable.image3, R.drawable.image4,  
                    R.drawable.image5, R.drawable.image6,  
                    R.drawable.image7, R.drawable.image8,  
                    R.drawable.image9, R.drawable.image10,  
                    R.drawable.image11, R.drawable.image12,  
                    R.drawable.image13, R.drawable.image14,  
                    R.drawable.image15  
          };  
            
          public ImageAdapter(Context c){  
               mContext = c;  
          }  
       
          @Override  
          public int getCount() {  
               return mThumbIds.length;  
          }  
       
          @Override  
          public Object getItem(int position) {  
               return mThumbIds[position];  
          }  
       
          @Override  
          public long getItemId(int position) {  
               return 0;  
          }  
       
          @Override  
          public View getView(int position, View convertView, ViewGroup parent) {                 
               ImageView imageView = new ImageView(mContext);  
         imageView.setImageResource(mThumbIds[position]);  
         imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);  
         imageView.setLayoutParams(new GridView.LayoutParams(70, 70));  
         return imageView;  
          }  
       
     }  
       
    
  4. Create AndroidGridViewActivity.java file in src folder used for displaying GridView and set GriedView Adapter as we have created previously.
     package com.idroid.gridview;  
       
     import android.app.Activity;  
     import android.content.Intent;  
     import android.os.Bundle;  
     import android.view.View;  
     import android.widget.AdapterView;  
     import android.widget.AdapterView.OnItemClickListener;  
     import android.widget.GridView;  
       
     public class AndroidGridViewActivity extends Activity {  
            
          @Override  
          public void onCreate(Bundle savedInstanceState) {  
               super.onCreate(savedInstanceState);  
               setContentView(R.layout.gridlayout);  
       
               GridView gridView = (GridView) findViewById(R.id.grid_view);  
               gridView.setAdapter(new ImageAdapter(this));  
               gridView.setOnItemClickListener(new OnItemClickListener() {  
                    @Override  
                    public void onItemClick(AdapterView<?> parent, View v,  
                              int position, long id) {  
                           
                         // Sending image id to FullScreenActivity and pass index  
                         Intent i = new Intent(getApplicationContext(), FullSizeImageActivity.class);  
                         i.putExtra("id", position);  
                         startActivity(i);  
                    }  
               });  
          }  
     }  
    
  5. Finally create new .java file for displaying Full size image and modify as above code.
     package com.idroid.gridview;  
       
     import android.app.Activity;  
     import android.content.Intent;  
     import android.os.Bundle;  
     import android.widget.ImageView;  
       
     public class FullSizeImageActivity extends Activity {  
            
            
          @Override  
          public void onCreate(Bundle savedInstanceState) {  
               super.onCreate(savedInstanceState);  
               setContentView(R.layout.image);  
                 
               // get intent data  
               Intent i = getIntent();  
                 
               // Selected image id  
               int position = i.getExtras().getInt("id");  
               ImageAdapter imageAdapter = new ImageAdapter(this);  
                 
               ImageView imageView = (ImageView) findViewById(R.id.full_image_view);  
               imageView.setImageResource(imageAdapter.mThumbIds[position]);  
          }  
       
     }  
       
    
  6. Now Build and run project in Simulator you can see the below like output and when click on image it is display full screen image on next Activity.


Download Source Code :AndroidGridViewExample.rar

Thursday 12 September 2013

Android Custom Toggle Button Example

Android Custom Toggle Android Custom Switch
  1. Toggle button is allowed to change setting between two state.
  2. Today's Tutorial we show you how to create Custom Toggle Button in Android and how to change their state and store it in any variable.
  1. Create new Android Project call Custom Toggle Button and create togglebutton.xml file in res/drawable folder and modify as below.
     <?xml version="1.0" encoding="UTF-8"?>  
     <selector xmlns:android="http://schemas.android.com/apk/res/android">  
       
       <item android:drawable="@drawable/switch_on" android:state_checked="true"/>  
       <item android:drawable="@drawable/switch_off" android:state_checked="false"/>  
       <item android:drawable="@drawable/switch_off"></item>  
       
     </selector>  
    
  2. Now, You need two images on and off state and give name switch_off and switch_on and put in res/drawable folder.And modify main.xml file take ToggleButton and TextView for display on and off state.
     <?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:background="#345334"  
       android:orientation="vertical" >  
       
       <ToggleButton  
         android:id="@+id/toggle"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:layout_centerHorizontal="true"  
         android:layout_centerVertical="true"  
         android:background="@drawable/toggleswitch"  
         android:button="@null"  
         android:textOff=""  
         android:textOn="" />  
       
       <TextView  
         android:id="@+id/tvstate"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:layout_below="@+id/toggle"  
         android:layout_centerHorizontal="true"   
         android:layout_marginTop="20dp"  
         android:textColor="#FFFFFF"  
         android:textAppearance="?android:attr/textAppearanceLarge" />  
       
       
     </RelativeLayout>  
    
  3. For getting toggle switch state need to understand setOnCheckedChangeListener() method. This method is executed when the ToggleButton State change from on to off and visa vars. For that you need to modify MainActivity.java as below.
     package com.idroid.toggleswitch;  
       
     import android.app.Activity;  
     import android.os.Bundle;  
     import android.widget.CompoundButton;  
     import android.widget.CompoundButton.OnCheckedChangeListener;  
     import android.widget.TextView;  
     import android.widget.ToggleButton;  
       
     public class MainActivity extends Activity {  
          ToggleButton toggleButton;  
       TextView stateOnOff;  
          @Override  
          public void onCreate(Bundle savedInstanceState) {  
               super.onCreate(savedInstanceState);  
               setContentView(R.layout.main);  
       
               toggleButton = (ToggleButton) findViewById(R.id.toggle);  
               stateOnOff=(TextView)findViewById(R.id.tvstate);  
               stateOnOff.setText("OFF");  
               toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
       
                    @Override  
                    public void onCheckedChanged(CompoundButton buttonView,  
                              boolean isChecked) {  
                           
                         if(isChecked){  
                              stateOnOff.setText("On");  
                         }else{  
                              stateOnOff.setText("Off");  
                         }  
       
                    }  
               });  
          }  
     }  
    
  4. Now Build and run project in Simulator you can see the below like output and when click on switch TextView text will be change.


Download Source Code :Android_Custom_Toggle_Button.rar

Tuesday 10 September 2013

Android Custom Button Example Tutorial

Android Custom Button With Gradient
  1. The purpose of this tutorial is to create custom button in android with custom background colors. The result will be like above image.
  2. There is two way to create such a buttons in android by using images and by using xml file which i have describe here.
  3. Android provides default UI buttons with default background sometimes we need some more custom design so it is look batter in any application User Interface.
  4. Follow below step by step tutorial to create custom button with selected unselected state by xml file.
  1. Create new Android Project call Android Custom Buttons and create btn1.xml file in res/drawable folder and modify as below.
     <?xml version="1.0" encoding="utf-8"?>  
     <selector xmlns:android="http://schemas.android.com/apk/res/android">  
       
       <item android:state_pressed="true"><shape android:shape="rectangle">  
           <corners android:radius="15dp" />  
       
           <solid android:color="#0097AA" />  
       
           <padding android:bottom="15.0dip" android:left="15.0dip" android:right="15.0dip" android:top="15.0dip" />  
       
           <stroke android:width="2dip" android:color="#FFFFFF" />  
         </shape></item>  
       <item android:state_pressed="false"><shape android:shape="rectangle">  
           <corners android:radius="15dp" />  
       
           <solid android:color="#0097AA" />  
       
           <padding android:bottom="15.0dip" android:left="15.0dip" android:right="15.0dip" android:top="15.0dip" />  
       
           <stroke android:width="2dip" android:color="#FFFFFF" />  
         </shape></item>  
     </selector>  
    
    By using this btn1.xml file we create one button by using selector in that we have taken two lt;itemgt; tags one for selected and one for unselected you can set both different color in this file for both the state. stroke property is for displaying border of buttons also you can set any type of color in stroke.

  2. Now it time to use btn1.xml file so look at your main.xml file and modify as per below code.
     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
       xmlns:tools="http://schemas.android.com/tools"  
       android:layout_width="fill_parent"  
       android:layout_height="fill_parent"  
            >  
       
       <ScrollView  
         android:layout_width="fill_parent"  
         android:layout_height="fill_parent" >  
       
         <RelativeLayout  
           android:layout_width="match_parent"  
           android:layout_height="match_parent" >  
       
           <Button  
             android:id="@+id/btn1"  
             android:layout_width="match_parent"  
             android:layout_height="wrap_content"  
             android:layout_alignParentTop="true"  
             android:layout_centerHorizontal="true"  
             android:layout_marginBottom="5dp"  
             android:layout_marginLeft="5dp"  
             android:layout_marginRight="5dp"  
             android:layout_marginTop="20dp"  
             android:background="@drawable/btn1"  
             android:gravity="center_horizontal"  
             android:onClick="btnClick"  
             android:text="@string/btn1_name" />  
       
         </RelativeLayout>  
       </ScrollView>  
       
     </RelativeLayout>  
    
  3. Now Modify your MainActivity.java file as per below code.
     package com.idroid.custombuttons;  
       
     import android.app.Activity;  
     import android.os.Bundle;  
     import android.view.View;  
     import android.widget.Toast;  
       
     public class MainActivity extends Activity {  
       
          @Override  
          public void onCreate(Bundle savedInstanceState) {  
               super.onCreate(savedInstanceState);  
               setContentView(R.layout.main);  
       
          }  
       
          public void btnClick(View view) {  
               switch (view.getId()) {  
               case R.id.btn1:  
                    showToast("Button1 Action");  
                    break;  
               default:  
                    showToast("");  
                    break;  
               }  
          }  
       
          private void showToast(String name) {  
               Toast.makeText(this, "Button " + name + " is pressed!!!",  
                         Toast.LENGTH_SHORT).show();  
       
          }  
     }  
       
    
  4. Now compile and run your project on Andoid simulator you can see first button display in below layout.If you want more buttons you can create that much xml files in drawable folder add you can create same layout like display in layout or you can download code from below link.


Download Source Code :Android_CustomButtons_Demo.zip

Monday 9 September 2013

Android Splash Screen Example with Animation Tutorial

Android Splash Android Splash
  1. When user click on application icon application is take some time or couple of seconds to load.
  2. During this time user know your application is loading so that time splash screen is important thing in our application
  3. Many application start with splash screen we know that very well because in Game app and other application have a splash screen.
  4. The purpose of splash screen is to user know that application is loading. Does not give more delay time to load splash screen because user does not want more time to wait for application load.
  5. For Creating splash screen you need one image of launch screen we have taken 480x800 here for our example you can see Android Developer Site for Different sizes of splash for all device resolution.
  6. Follow below step by step process of creating Splash Screen for Android Application
  1. Create new Android Project call Android Splash Screen and put your launch image at res/drawable folder.
  2. Also create three xml file one for splash screen give name splashscreen.xml in res/layout folder and create one new folder in res folder and give name anim and create two xml file called fade_in.xml and fade_out.xml for animation.
  3. Modify Animations xml file as per below code.

    fade_in.xml
     <set xmlns:android="http://schemas.android.com/apk/res/android"   
        android:fillAfter="true">  
        <alpha android:fromAlpha="0.0"   
           android:toAlpha="1.0"  
           android:duration="4000"/> //Milliseconds  
     </set>  
       
    

    fade_out.xml
     <set xmlns:android="http://schemas.android.com/apk/res/android"   
        android:fillAfter="true">  
        <alpha android:fromAlpha="1.0"   
           android:toAlpha="0.0"  
           android:duration="4000"/> //Miliseconds  
     </set>  
       
    
  4. Now modify your main.xml and splashscreen.xml file as per below code

    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:background="#FFFFFF"  
       android:gravity="center"  
       >  
       
       <TextView  
         android:id="@+id/tv1"  
         android:gravity="center"  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:layout_marginTop="70dp"  
         android:textSize="20sp"  
         android:text="@string/main_screen"  
         android:textColor="#000000" />  
       
     </RelativeLayout>  
    

    splashscreen.xml
     <?xml version="1.0" encoding="utf-8"?>  
     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
       android:layout_width="fill_parent" android:background="@drawable/splash_screen"  
       android:layout_height="fill_parent"  
       android:orientation="vertical" >  
       
     </RelativeLayout>  
    
  5. Now Create one SplashScreen.java file in src folder and modify as below code

    SplashScreen.java
     package com.idroid.splashscreen;  
       
     import android.app.Activity;  
     import android.content.Intent;  
     import android.os.Bundle;  
     import android.os.Handler;  
       
     public class SplashScreen extends Activity {  
       
          private static final int TIME = 4 * 1000;// 4 seconds  
       
          @Override  
          protected void onCreate(Bundle savedInstanceState) {  
               super.onCreate(savedInstanceState);  
               setContentView(R.layout.splashscreen);  
       
               new Handler().postDelayed(new Runnable() {  
       
                    @Override  
                    public void run() {  
       
                         Intent intent = new Intent(SplashScreen.this,  
                                   MainActivity.class);  
                         startActivity(intent);  
       
                         SplashScreen.this.finish();  
       
                         overridePendingTransition(R.anim.fade_in, R.anim.fade_out);  
       
                    }  
               }, TIME);  
                 
               new Handler().postDelayed(new Runnable() {  
                     @Override  
                     public void run() {  
                         }   
                      }, TIME);  
       
          }  
       
            
          @Override  
          public void onBackPressed() {  
               this.finish();  
               super.onBackPressed();  
          }  
     }  
       
    
  6. Now build and run your project on Simulator you can see the splash screen for 4 seconds.


Download Source Code :Android_SplashScreen_Demo.zip

Saturday 7 September 2013

Android Options Menu Integration Guide Tutorials With Example

Android Option Menu Android Option Menu
  1. In this tutorial we show you how to implement Option Menu in Android SDK.
  2. Option menu is one of the attractive user interface in Android.
  3. Option menu is basically used to provide some help in application,setting part of your application and provide some edit,delete functionality or also you can provide navigation in same Activity or Another Activity.
  4. There is three types of menu available in android
    1. Option Menu
    2. Context Menu
    3. Submenus
  5. For Implementing Option Menu in Android Application Follow below step by step guideline
  1. Create new Android Project and fill all require details.
  2. Look at your android project structure find out res folder in that create one menu folder and create color.xml for creating menu items which is shown in above image and modify as given code.
     <?xml version="1.0" encoding="utf-8"?>  
     <menu xmlns:android="http://schemas.android.com/apk/res/android">  
           <item   
                android:title="Red"  
                android:orderInCategory="2"  
                android:id="@+id/red"/>  
           <item   
                android:title="Green"  
                android:orderInCategory="1"  
                android:id="@+id/green"/>  
           <item   
                android:title="Blue"  
                android:orderInCategory="3"  
                android:id="@+id/blue"/>  
           <item   
                android:title="Pink"  
                android:orderInCategory="4"  
                android:id="@+id/pink"/>  
                  
           <item   
                android:title="Yellow"  
                android:orderInCategory="5"  
                android:id="@+id/yellow"/>  
                   
     </menu>  
       
    
  3. We are created 5 menu items for option menu as display in above image you can also set icon by android:icon property of menu item and set any android drawable resources.
  4. Now open your MenuActivity.java file and type following code. In Following code each items define by ID and it is identify by ID in switch case statement.As per ID selection we will set color of background resources.
     package com.idroid.android.menudemo;  
       
     import android.app.Activity;  
     import android.os.Bundle;  
     import android.view.Menu;  
     import android.view.MenuItem;  
     import android.widget.LinearLayout;  
     import android.widget.TextView;  
       
       
     public class MenuActivity extends Activity  
     {  
          TextView tv;  
          LinearLayout l;  
            
       /** Called when the activity is first created. */  
       @Override  
       public void onCreate(Bundle savedInstanceState)   
       {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.main);  
           
         tv=(TextView)findViewById(R.id.txt1);  
         l=(LinearLayout)findViewById(R.id.LL1);  
       }  
         
       @Override  
       public boolean onCreateOptionsMenu(Menu menu)  
       {  
            getMenuInflater().inflate(R.menu.color, menu);  
            return true;  
       }  
         
       @Override  
       public boolean onOptionsItemSelected(MenuItem item)   
       {  
               int color=0;  
               switch(item.getItemId())  
               {  
                    case R.id.red:  
                         color=getResources().getColor(R.color.red);  
                         break;  
                           
                    case R.id.green:  
                         color=getResources().getColor(R.color.green);  
                         break;  
                           
                    case R.id.blue:  
                         color=getResources().getColor(R.color.blue);  
                         break;  
                           
                    case R.id.pink:  
                         color=getResources().getColor(R.color.pink);  
                         break;  
                           
                    case R.id.yellow:  
                         color=getResources().getColor(R.color.yellow);  
                         break;  
                           
                    default:  
                         break;  
               }  
               l.setBackgroundColor(color);  
               return true;  
          }  
         
     }  
    
  5. Finally run your project by right click on project and run as Android Application and on Android Simulator select Menu button to launch Option Menu


Download Source Code :Android_OptionMenu_Demo.zip

Friday 6 September 2013

Android Drop Down List or Spinner Example

Android Drop Down List Android Drop Down List
  1. In this tutorial we show you simple spinner example which allow user to select an item from Drop Down list.
  2. In Spinner display static three image data selection after selecting image name picture1,picture2,picture3 effect on ImageView disply below Spinner or Drop Down list.
  3. In Android use android.widget.Spinner class for Drop Down selection list.
  4. Follow below steps for Android Spinner integration
  1. Create new Android Project and fill all require details.
  2. Open String.xml file under resources folder and modify as below.
     <?xml version="1.0" encoding="utf-8"?>  
     <resources>  
       <string name="app_name">SpinnerDemo Idroid</string>  
       <string name="item_list">Select Picture</string>  
       <string-array name="spinner">  
            <item>Picture1</item>  
            <item>Picture2</item>  
            <item>Picture3</item>  
       </string-array>  
     </resources>  
       
    
  3. Open your main.xml file and simply change design display as above layout one Spinner top of the screen and below display ImageView for that you need to modify main.xml file under layout folder.
     <?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"  
       >  
         
       <Spinner  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:entries="@array/spinner"  
            android:prompt="@string/item_list"  
            android:id="@+id/spin1"/>  
              
       <ImageView  
            android:layout_width="fill_parent"  
            android:layout_height="fill_parent"  
            android:id="@+id/imgv1"/>  
         
     </LinearLayout>  
    
  4. Now put three images in res>drawable folder particular size which you want to display and also give proper name for images after that modify your MainActivity.java file given below.
     <?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"  
       >  
         
       <Spinner  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:entries="@array/spinner"  
            android:prompt="@string/item_list"  
            android:id="@+id/spin1"/>  
              
       <ImageView  
            android:layout_width="fill_parent"  
            android:layout_height="fill_parent"  
            android:id="@+id/imgv1"/>  
         
     </LinearLayout>  
    

Download Source Code :Android_Spinner_Demo.zip

Wednesday 4 September 2013

How To Pass Simple Intent from one Android Activity to another Activity

Passing Intent Android Passing Intent Android
  1. Here we have written my second tutorial about passing intent from one activity to another activity and also pass data to second screen by using intent.putExtra method.
  2. Intent.putExtra method accept many kind of data like String,int,float,...etc.
  3. Using intent we can pass control to another Activity.Bundle can be used to pass data from one Activity to another Activity.
  4. Below we have written code about login activity to pass username to next Android Activity.
  • Modify your main.xml file by using below code
     <?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"  
       android:gravity="center|center_vertical"  
       android:background="@color/bgcolor"  
       >  
          <TextView   
            android:text="@string/StrTitle"   
            android:layout_height="wrap_content"   
            android:layout_width="wrap_content"   
            android:padding="15px"   
            android:textSize="25px"/>  
            
          <TextView   
               android:text="Username: "   
               android:id="@+id/textView1"   
               android:layout_height="wrap_content"  
               android:gravity="left"   
               android:layout_width="match_parent">  
          </TextView>  
            
          <EditText   
               android:id="@+id/editText1"   
               android:layout_width="match_parent"   
               android:layout_height="wrap_content"   
               android:hint="Username"   
               android:paddingBottom="5px">  
            <requestFocus></requestFocus>  
          </EditText>  
       
          <TextView   
               android:gravity="left|fill"  
               android:layout_width="match_parent"   
               android:text="Password: "   
               android:layout_height="wrap_content"   
               android:id="@+id/textView2"   
               android:paddingTop="5px">  
          </TextView>  
       
          <EditText   
               android:id="@+id/editText2"   
               android:layout_width="match_parent"   
               android:layout_height="wrap_content"   
               android:inputType="textPassword"   
               android:hint="Password">  
          </EditText>  
            
          <Button   
               android:text="Login me"   
               android:id="@+id/button1"   
               android:layout_width="wrap_content"   
               android:layout_height="wrap_content"   
               android:layout_marginTop="10px">  
          </Button>  
     </LinearLayout>  
       
    
  • MainActivity.java
     package com.idroid.passingintent;  
       
     import android.app.Activity;  
     import android.content.Intent;  
     import android.os.Bundle;  
     import android.view.View;  
     import android.view.View.OnClickListener;  
     import android.widget.Button;  
     import android.widget.EditText;  
     import android.widget.Toast;  
       
     public class Prog2Activity extends Activity {  
       /** Called when the activity is first created. */  
       @Override  
       public void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.main);  
           
         Toast.makeText(this,"Enter username and password : user",Toast.LENGTH_LONG).show();  
         Button login = (Button)findViewById(R.id.button1);  
         final EditText etuname=(EditText)findViewById(R.id.editText1);  
         final EditText etpwd=(EditText)findViewById(R.id.editText2);  
               final String uname="user";  
               final String pwd="user";  
       
                 
               final Intent intent=new Intent(this,NextScreen.class);  
               login.setOnClickListener(new OnClickListener()   
               {  
              @Override  
              public void onClick(View arg0)   
              {  
                   //TODO Auto-generated method stub  
                   if((uname.equals(etuname.getText().toString())) && (pwd.equals(etpwd.getText().toString())))  
                   {    
                        intent.putExtra("uname", uname);  
                        startActivity(intent);  
                   }  
                   else  
                   {  
                        Toast.makeText(Prog2Activity.this,"Username or password incorrect" , Toast.LENGTH_LONG).show();                     
                   }  
                   }  
         });  
       }  
     }  
    
  • Create one new file call NextScreen.jave and modify as given below
     package com.idroid.passingintent;  
     import android.app.Activity;  
     import android.os.Bundle;  
     import android.text.Html;  
     import android.view.Gravity;  
     import android.view.ViewGroup.LayoutParams;  
     import android.widget.TextView;  
     import android.content.Intent;  
     import android.graphics.Color;  
       
     public class NextScreen extends Activity   
     {  
          public void onCreate(Bundle savedInstanceState)   
          {  
         super.onCreate(savedInstanceState);  
           
         Intent intent=getIntent();  
         String uname=intent.getStringExtra("uname");  
           
         TextView tv=new TextView(this);  
         tv.setPadding(20, 20, 10, 10);  
         tv.setBackgroundColor(Color.rgb(00, 66, 99));  
         tv.setTextColor(Color.rgb(255, 255, 255));  
         tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
         tv.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER);  
         tv.setTextSize(25);  
         tv.setText(Html.fromHtml("Welcome <b>"+uname+"</b>"));  
         setContentView(tv);  
          }  
     }  
       
    
  • Now build and run your project in Android simulator provide username and password user you can see next screen with username If you provide wrong username and password it will show you Tost Message.

Download Source Code :Android_Intent_Demo.zip