Monday, 7 October 2013

Android ListView With Alternet ListItem Background Color

Android ListView
  1. In Android ListView we can set background color or background image of an ListItem.
  2. Today we show you how to set background color of an alternate ListView Items.
  3. Also we show you how to set alternate background image of an ListView.
  4. For that you need to understand Simple ListView so first check out our previous posts of ListView download sample source and then modify as per below code.You can find Simple ListView Posts Here
  5. After understanding previous post need to modify code as per below
  6. Look at the getView method of FruitsArrayAdapter.java and write before return rowView write below code.
 if (position % 2 == 0)  
 {  
      rowView.setBackgroundColor(Color.parseColor("#EA5B61"));  
 } else {  
      rowView.setBackgroundColor(Color.parseColor("#DC6BAD"));  
 }  

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

Wednesday, 24 July 2013

Android Simple Hello World Application

Andrid Hello World

Steps to Create Above like HelloWorld Application in Android
  1. Create New > Android Application in Eclipse android will create all necessary files and configuration.
  2. Give name of your application HelloWorld as per java standard
  3. Modify Your main.xml file as given below

     <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" >  
       
       <TextView  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:layout_centerHorizontal="true"  
         android:layout_centerVertical="true"  
         android:textSize="30dp"  
         android:text="Hello world" />  
       
     </RelativeLayout>  
    
  4. Now Build and Run project in Android Virtual Device(AVD)


Download HelloWorld Source Code




My Android Applications

Download Hanuman HD Wallpaper on Google Play

Download Live Currency Exchange Rates on Google Play

Download iSIM Info on Google Play

Download Find Droid on Google Play

Wednesday, 17 July 2013

How to Create Android Project in Eclipse IDE

This post teach you how to create new android project in Eclipse IDE. For creating android project you have must set up Eclipse Android Development Environment if you have already set up Android Development Environment directly Go through below step otherwise follow This Post

Follow few below steps to Create New Android Application/Project :

  1. Select File > New > Android Project
  2. After Selecting Android > Android Project > Click Next
  3. Now You need to specify below all application content
    • Application Name : Simply name of your Application.
    • Package Name : Provide package name as identifier of your project as well as organization Example : com.idroid.livecurrencyexchange all character must be small because it is follow naming conversation of Java
    • Create Activity (Optional): Name of your launcher activity name which is called first when your project runs Example : MySampleApp. It is also follow java class naming conversation.
    • Min SDK Version : Minimum API Version in which your application properly run and also run on above version.If it is blank than system automatically defines minSdkVersion in you Manifiest file.
  4. Click Finish your new android project created.



My Android Applications

Download Hanuman HD Wallpaper on Google Play

Download Live Currency Exchange Rates on Google Play

Download iSIM Info on Google Play

Download Find Droid on Google Play

Thursday, 11 July 2013

How to set up your android environment in Eclipse IDE

For getting started Android development you need to install first Software Development Kit (SDK) You can download from below link or you can get from your friends
 http://developer.android.com/sdk/  
Download Eclipse IDE from below link and make sure you are downloading Eclipse IDE for Java Developers
 www.eclipse.org/downloads/  
After Downloading both the files now Eclipse IDE and Software Development Kit which is Android SDK now available on you system. Now you need to set up ADT (Android Development Toolkit) plugin for that start your eclipse IDE and select help => Install New software => Popup will be appear copy and past below url to Work with text box and add it. On Add Site dialog provide any name or write ADT completion of this process your ADT plugin configure.
 https://dl-ssl.google.com/android/eclipse/  
Now you need to setup for Software Development Kit (SDK) for that in Eclipse Select Window -> Preferences, and then Android. and browse SDK path which you have downloaded previously. Finally you can click on Window -> Android SDK and AVD Manager. In the Installed packages pane, we see the Android SDK Tools we just installed and Also create one (Android Virtual Device)AVD for android application testing purpose.

My Android Applications

Download Hanuman HD Wallpaper on Google Play

Download Live Currency Exchange Rates on Google Play

Download iSIM Info on Google Play

Download Find Droid on Google Play

Wednesday, 26 June 2013

Android ViewPager Swipe Images Using Your Finger like Image Gallery Android

Simply make Image Gallery using ViewPager.When you slide Finger over image and swipe left to right used ViewPager.Many other controller available but i think it is very easy to maintain and create gallery using ViewPager.

Start new project and give name as ViewPagerDemo and begin your development of your android Gallery. Put your all Gallery images into drawable folder and as of now put four images and give name as a,b,c,d.

Now need to create one helper class so it is helpful to in creation of Gallery give name as ImageSwipeAdapter.java and extends PagerAdapter and modified as below

 package com.idroid.viewpager;  
 import android.content.Context;  
 import android.support.v4.view.PagerAdapter;  
 import android.support.v4.view.ViewPager;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.widget.ImageView;  
 public class ImageSwipeAdapter extends PagerAdapter {  
      Context context;  
   private int[] Images = new int[] {  
     R.drawable.a,  
     R.drawable.b,  
     R.drawable.c,  
     R.drawable.d  
   };  
   ImageSwipeAdapter(Context context){  
        this.context=context;  
   }  
   @Override  
   public int getCount() {  
    return Images.length;  
   }  
   @Override  
   public boolean isViewFromObject(View view, Object object) {  
    return view == ((ImageView) object);  
   }  
   @Override  
   public Object instantiateItem(ViewGroup container, int position) {  
    ImageView imageView = new ImageView(context);  
    int pad = context.getResources().getDimensionPixelSize(R.dimen.m_padding);  
    imageView.setPadding(pad, pad, pad, pad);  
    imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);  
    imageView.setImageResource(Images[position]);  
    ((ViewPager) container).addView(imageView, 0);  
    return imageView;  
   }  
   @Override  
   public void destroyItem(ViewGroup container, int position, Object object) {  
    ((ViewPager) container).removeView((ImageView) object);  
   }  
  }  

Now start programming for Swipe feature for that modify your main.xml file as below code

 <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" >  
            <android.support.v4.view.ViewPager  
            android:id="@+id/view_pager"  
            android:layout_width="match_parent"  
            android:layout_height="match_parent" />  
 </RelativeLayout>  

Now need to modify your MainActivity.java file as per below so your Image ViewPager is redy to run

 package com.idroid.viewpager;  
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.support.v4.view.ViewPager;  
 public class MainActivity extends Activity {  
  @Override  
  public void onCreate(Bundle savedInstanceState) {  
   super.onCreate(savedInstanceState);  
   setContentView(R.layout.main);  
   ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);  
   ImageSwipeAdapter adapter = new ImageSwipeAdapter(this);  
   viewPager.setAdapter(adapter);  
  }  
 }  

My Android Applications

Download Hanuman HD Wallpaper on Google Play

Download Live Currency Exchange Rates on Google Play

Download iSIM Info on Google Play

Download Find Droid on Google Play

Thursday, 20 June 2013

Android Bound Service and Unbound Service run in Background

Android Services is used to do any background task in current activity.There is two types of Services available in android.Service class is responsible for using Services in android for that we need to extends Services class in out Custom service.

  1. Bound Service :Service which call indefinitely in between activity
  2. Unbound Service :Service which call at the life span of calling activity

Services has different life cycle from Activity. Services has different method for their life cycle which is startService(),stopService(),onBindService()


My Android Applications

Download Hanuman HD Wallpaper on Google Play

Download Live Currency Exchange Rates on Google Play

Download iSIM Info on Google Play

Download Find Droid on Google Play

Thursday, 13 June 2013

Call Log Apps For Android


This Application Quickly access Call log from your android device and display in android TextView. For that you need to create application give name as myCalllog.

1. You need to look your main.m file first. modify main.m code as given below tack one TextView so we can add call log data in TextView
 <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" >  
   <TextView  
     android:id="@+id/textview"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:padding="10px"  
     android:scrollbars="vertical"  
     android:text="@string/hello_world"  
     tools:context=".MainActivity" />  
 </RelativeLayout>  

2.Now you need to change your java file as per our requirement you need to get your TextView by using findViewById method and also set scroll enable so more record it is scroll so in your onCreate() method put below line of code.

  tv=(TextView) findViewById(R.id.textview);  
  tv.setMovementMethod(new ScrollingMovementMethod());  

3.You have get your TextView now you need to move forword get call log data in cursor so you need one StringBuffer and Cursor for moving each and every record and get Incoming call,Outgoing call and Miss call put below TextView code

 StringBuffer sb = new StringBuffer();  
 Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,null, null, null);  
 int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);  
 int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);  
 int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);  
 int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);  
 sb.append("Call Details :")  
 while (managedCursor.moveToNext()) {  
           String phNumber = managedCursor.getString(number);  
           String callType = managedCursor.getString(type);  
           String callDate = managedCursor.getString(date);  
           Date callDayTime = new Date(Long.valueOf(callDate));  
           String callDuration = managedCursor.getString(duration);  
           String dir = null;  
           int dircode = Integer.parseInt(callType);  
          switch (dircode) {  
                case CallLog.Calls.OUTGOING_TYPE:  
                     dir = "OUTGOING";  
                     break;  
                case CallLog.Calls.INCOMING_TYPE:  
                     dir = "INCOMING";  
                     break;  
                case CallLog.Calls.MISSED_TYPE:  
                     dir = "MISSED";  
                     break;  
                }  
                sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- "  
                          + dir + " \nCall Date:--- " + callDayTime  
                          + " \nCall duration in sec :--- " + callDuration);  
                sb.append("\n----------------------------------");  
 }  
 managedCursor.close();  
 tv.setText(sb);  

4.Your Call log application is ready to build now build and run your application check output in your simulator.

My Android Applications

Download Hanuman HD Wallpaper on Google Play

Download Live Currency Exchange Rates on Google Play

Download iSIM Info on Google Play

Download Find Droid on Google Play

Wednesday, 5 June 2013

Live Currency Exchange Rates


currency converter screen 1 currency converter rates currency converter rates setting screen

Live Currency Exchange Rates is a free live currency converter. It offers to convert one word currency to another.

You can add currency by clicking text box below button and press convert button to convert any of the currency which you have selected.


Live Currency Exchange Rates provide following features
  • Currency Converter
  • Offline Access
  • Automatic update currency rates if you set in setting section > set automatic update on
  • Over 31 currency supported
  • View last updated currency news
  • Easy selection of currency by currency code and country name
  • Not require to create an account and easy one
  • Sharing functionality

Use Offline
  • Store the latest updated currency rate.
  • Convert currency without internet access.

Updates in Feature Versions also suggest your ideas
  • Provide widget for currency information
  • Provide detail about selected currency
  • Increase more country
  • Add your own percentage with converted currency
  • Provide visa – versa conversion
  • Save any currency to and from to Favorite list
  • Recently converted currency list

Keywords : currency exchange rates, money exchange rate, money conversion, currency converter calculator, currency converters

My Android Applications

Download Hanuman HD Wallpaper on Google Play

Download Live Currency Exchange Rates on Google Play

Download iSIM Info on Google Play

Download Find Droid on Google Play