Android


  • TextView Example in Android

Open res -> layout -> activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <TextView
        android:id="@+id/textView1"
        android:clickable="true"
        android:textSize="20dp"
        android:textColor="#0000FF"
        android:gravity="center"
        android:lineSpacingMultiplier="1.5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Static TextView" />
 
    <TextView
        android:id="@+id/textView2"
        android:clickable="true"
        android:textSize="20dp"
        android:textColor="#FF9900"
        android:gravity="center"
        android:lineSpacingMultiplier="1.5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="120dp"
        android:text="Java" />
    
    <TextView
        android:id="@+id/textView3"
        android:clickable="true"
        android:textSize="20dp"
        android:textColor="#FF0000"
        android:gravity="center"
        android:lineSpacingMultiplier="1.5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="190dp"
        android:text="Android" />
 
</RelativeLayout>
 Open src -> package -> MainActivity.java  :
package com.example.textview;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
 TextView tv1, tv2, tv3;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        tv1 = (TextView) findViewById(R.id.textView1);
        tv2 = (TextView) findViewById(R.id.textView2);
        tv3 = (TextView) findViewById(R.id.textView3);
        
        tv1.setOnClickListener(new View.OnClickListener() {
   
  public void onClick(View v) {
   // TODO Auto-generated method stub
    
   Toast.makeText(getBaseContext(), "Static TextView Clicked",
    Toast.LENGTH_SHORT).show();
  }
 });
        
        tv2.setOnLongClickListener(new View.OnLongClickListener() {
   
  public boolean onLongClick(View v) {
   // TODO Auto-generated method stub
    
   Toast.makeText(getBaseContext(), "Java Long Clicked",
    Toast.LENGTH_SHORT).show();
 
   return false;
  }
 });
        
        tv3.setOnClickListener(new View.OnClickListener() {
   
  public void onClick(View v) {
   // TODO Auto-generated method stub
    
   Toast.makeText(getBaseContext(), "Android Clicked",
    Toast.LENGTH_SHORT).show();
  }
 });
        
        tv3.setOnLongClickListener(new View.OnLongClickListener() {
   
  public boolean onLongClick(View v) {
   // TODO Auto-generated method stub
    
   Toast.makeText(getBaseContext(), "Android Long Clicked",
    Toast.LENGTH_SHORT).show();
 
   return true;
  }
 });
    }
  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
    Button Example in Android
 Open res -> layout ->  main.xml  :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <TextView
        android:id="@+id/textView1"
        android:textSize="20dp"
        android:textColor="#FF0000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="67dp"
        android:text="Static Button" />
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="140dp"
        android:padding="8dp"
        android:text="Java" />
    
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="210dp"
        android:background="#FF00FF"
        android:padding="8dp"
        android:textColor="#FFFFFF"
        android:text="Android" />
    
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="290dp"
        android:background="#4169E1"
        android:padding="8dp"
        android:textColor="#FFFFFF"
        android:text="PHP" />
 
</RelativeLayout>
Open src -> package -> MainActivity.java  :
package com.example.button;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
 Button b1, b2, b3;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        
        b1.setOnClickListener(new View.OnClickListener() {
   
  public void onClick(View v) {
   // TODO Auto-generated method stub
    
   Toast.makeText(getBaseContext(), "Java Clicked",
    Toast.LENGTH_SHORT).show();
  }
 });
        
        b2.setOnLongClickListener(new View.OnLongClickListener() {
   
  public boolean onLongClick(View v) {
   // TODO Auto-generated method stub
    
   Toast.makeText(getBaseContext(), "Android Long Clicked",
    Toast.LENGTH_SHORT).show();
 
   return false;
  }
 });
        
        b3.setOnClickListener(new View.OnClickListener() {
   
  public void onClick(View v) {
   // TODO Auto-generated method stub
    
   Toast.makeText(getBaseContext(), "PHP Clicked",
    Toast.LENGTH_SHORT).show();
  }
 });
        
        b3.setOnLongClickListener(new View.OnLongClickListener() {
   
  public boolean onLongClick(View v) {
   // TODO Auto-generated method stub
    
   Toast.makeText(getBaseContext(), "PHP Long Clicked",
    Toast.LENGTH_SHORT).show();
 
   return true;
  }
 });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
    Toggle Button Example in Android
 Open res -> layout ->  main.xml  :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <TextView
        android:id="@+id/textView1"
        android:textColor="#FF0000"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="Static ToggleButton" />
 
    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="76dp"
        android:text="ToggleButton" />
 
</RelativeLayout>
 Open src -> package -> MainActivity.java  :
package com.example.togglebutton;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;
 
public class MainActivity extends Activity {
 
 ToggleButton tb;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        tb = (ToggleButton) findViewById(R.id.toggleButton1);
        
        tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
   
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
   // TODO Auto-generated method stub
    
   if(isChecked == true)
   {
    Toast.makeText(getBaseContext(), "ON State",
     Toast.LENGTH_SHORT).show();
   }
   
   else
   {
    Toast.makeText(getBaseContext(), "OFF State",
     Toast.LENGTH_SHORT).show();
   }
  }
 });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
  Static CheckBox Example in Android
   Open res -> layout -> main.xml  :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <TextView
        android:id="@+id/textView1"
        android:textColor="#FF0000"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="Static CheckBox" />
 
    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="110dp"
        android:text="Package 1" />
    
    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="160dp"
        android:text="Package 2" />
    
    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="210dp"
        android:text="Package 3" />
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="290dp"
        android:text="Proceed" />
 
</RelativeLayout>
Open src -> package -> MainActivity.java  :
package com.example.checkbox;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
 CheckBox c1, c2, c3;
 Button b;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        c1 = (CheckBox) findViewById(R.id.checkBox1);
        c2 = (CheckBox) findViewById(R.id.checkBox2);
        c3 = (CheckBox) findViewById(R.id.checkBox3);
        
        b = (Button) findViewById(R.id.button1);
        
        b.setOnClickListener(new View.OnClickListener() {
   
  public void onClick(View v) {
   // TODO Auto-generated method stub
   
   if((c1.isChecked()==false) && (c2.isChecked()==false) && (c3.isChecked()==false))  
   {
    Toast.makeText(getBaseContext(),"None Package Selected", 
     Toast.LENGTH_SHORT).show();
   }
   else
   {
    String str = null;

    if(c1.isChecked())
    {
     str="Package 1";
    }
    if(c2.isChecked())
    {
     str=str.concat(" Package 2");
    }
    if(c3.isChecked())
    {
     str=str.concat(" Package 3");
    }

    int i=str.length();
    String str1=" are selected";
    String str2=" is selected";

    if(i>9)
    {
     str=str.concat(str1);

     Toast.makeText(getBaseContext(), str, 
      Toast.LENGTH_SHORT).show();
    }
    else
    {
     str=str.concat(str2);

     Toast.makeText(getBaseContext(), str, 
      Toast.LENGTH_SHORT).show();
    }
   }
  }
 });
        
        b.setOnLongClickListener(new View.OnLongClickListener() {
   
  public boolean onLongClick(View v) {
   // TODO Auto-generated method stub
    
   c1.setChecked(false);
   c2.setChecked(false);
   c3.setChecked(false);
   
   Toast.makeText(getBaseContext(), "Long Pressed 
    & Refreshed ChekBoxes", Toast.LENGTH_SHORT).show();
   
   return true;
  }
 });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
   Static Radio Button Example in Android
  Open res -> layout ->  main.xml  :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <TextView
        android:id="@+id/textView1"
        android:textColor="#FF0000"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="Static RadioButton" />
 
    <TextView
        android:id="@+id/textView2"
        android:textColor="#FF00FF"
        android:textSize="17dp"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="130dp"
        android:text="Do you like Android ?" />
 
    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="180dp"
        android:text="Yes" />
 
    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="230dp"
        android:text="No" />
 
</RelativeLayout>
Open src -> package -> MainActivity.java  :
package com.example.radiobutton;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
 
public class MainActivity extends Activity {
 
 RelativeLayout rl;
 RadioButton r1, r2;
 ImageView iv1, iv2;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        rl = (RelativeLayout) findViewById(R.id.rl);
        
        r1 = (RadioButton) findViewById(R.id.radioButton1);
        r2 = (RadioButton) findViewById(R.id.radioButton2);
        
        iv1 = new ImageView (MainActivity.this);
        iv2 = new ImageView (MainActivity.this);
        
        RelativeLayout.LayoutParams params1=new RelativeLayout.LayoutParams
          ((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);
 
        params1.leftMargin=200;
        params1.topMargin=500;
 
        iv1.setLayoutParams(params1);
        iv1.setImageResource(R.drawable.yes);
        rl.addView(iv1);
        iv1.setVisibility(View.INVISIBLE);
 
        iv2.setLayoutParams(params1);
        iv2.setImageResource(R.drawable.no);
        rl.addView(iv2);
        iv2.setVisibility(View.INVISIBLE);
 
        r1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
         
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  // TODO Auto-generated method stub
          if(r1.isChecked())
   {
    r2.setChecked(false);
    iv2.setVisibility(View.INVISIBLE);
    iv1.setVisibility(View.VISIBLE);
   }
  }
        });
 
        r2.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
         
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          // TODO Auto-generated method stub
          if(r2.isChecked())
          {
           r1.setChecked(false);
           iv1.setVisibility(View.INVISIBLE);
           iv2.setVisibility(View.VISIBLE);
          }
         }
        });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
 Static Spinner Example in Android
Open res -> layout ->  main.xml  :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <TextView
        android:id="@+id/textView1"
        android:textSize="20dp"
        android:textColor="#FF0000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="Static Spinner" />
 
    <Spinner
        android:id="@+id/spinner1"
        android:entries="@array/spinner_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="150dp" />
 
</RelativeLayout>
  Open src -> package -> MainActivity.java  :
package com.example.spinner;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
 
public class MainActivity extends Activity {
 
 Spinner sp;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        sp = (Spinner) findViewById(R.id.spinner1);
        
        sp.setOnItemSelectedListener(new OnItemSelectedListener() {
 
  public void onItemSelected(AdapterView<?> arg0, View arg1,
    int arg2, long arg3) {
   // TODO Auto-generated method stub
    
   Toast.makeText(getBaseContext(), sp.getSelectedItem().toString(), 
    Toast.LENGTH_SHORT).show();
  }
 
  public void onNothingSelected(AdapterView<?> arg0) {
   // TODO Auto-generated method stub
    
  }
 });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
  Download Event using ProgressBar in Android
   Open res -> layout -> main.xml  :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="Download File" />
 
</RelativeLayout>
Open src -> package -> MainActivity.java a :
package com.example.progressbar;
 
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
    Button btnStartProgress;
 
    private ProgressDialog _progressDialog;
    private int _progress = 0;
    private Handler _progressHandler;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  btnStartProgress = (Button) findViewById(R.id.button1);
  btnStartProgress.setOnClickListener(new OnClickListener() 
  {
 
     @Override
     public void onClick(View v) 
     {
   showDialog(1);
         _progress = 0;
         _progressDialog.setProgress(0);
         _progressHandler.sendEmptyMessage(0);
     }
  });
  
  _progressHandler = new Handler() 
  {
   
   public void handleMessage(Message msg) 
   {
    super.handleMessage(msg);
    if (_progress >= 100) 
    {
        _progressDialog.dismiss();
    } else 
    {
        _progress++;
        _progressDialog.incrementProgressBy(1);
        _progressHandler.sendEmptyMessageDelayed(0, 100);
    }
   }
         };
 }
 
 @Override
 protected Dialog onCreateDialog(int i)
     {
            _progressDialog = new ProgressDialog(this);
            _progressDialog.setIcon(R.drawable.ic_launcher);
            _progressDialog.setTitle("Downloading files...");
            _progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            _progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Hide", new
                DialogInterface.OnClickListener() 
            {
                public void onClick(DialogInterface dialog,
                    int whichButton)
                {
                    Toast.makeText(getBaseContext(),
                            "Hide clicked!", Toast.LENGTH_SHORT).show();
                }
            });
            _progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new
                DialogInterface.OnClickListener() 
            {
                public void onClick(DialogInterface dialog,
                     int whichButton)
                {
                    Toast.makeText(getBaseContext(),
                            "Cancel clicked!", Toast.LENGTH_SHORT).show();
                }
            });
            return _progressDialog;
 }
}
  Set TextView TextSize using SeekBar
   Open res -> layout -> main.xml  :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="43dp" />
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/seekBar1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="Hello Android" />
 
</RelativeLayout>
Open src -> package -> MainActivity.java  :
package com.example.seekbar;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
 
public class MainActivity extends Activity {
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final TextView t1=(TextView) findViewById(R.id.textView1);
        final SeekBar sk=(SeekBar) findViewById(R.id.seekBar1);
       
        sk.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
          int p=0;

  @Override
  public void onStopTrackingTouch(SeekBar seekBar) {
   // TODO Auto-generated method stub
   if(p<30)
   {
    p=30;
    sk.setProgress(p);
   }
  }
  
  @Override
  public void onStartTrackingTouch(SeekBar seekBar) {
   // TODO Auto-generated method stub
  }
  
  @Override
  public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
   // TODO Auto-generated method stub
   p=progress;
   t1.setTextSize(p);
  }
  });     
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }    
}
  Find selected RadioButton in RadioGroup
   Open res -> layout -> main.xml  :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="48dp"
        android:layout_marginTop="82dp" >
 
        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="RadioButton 1" />
 
        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton 2" />
 
        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton 3" />

    </RadioGroup>
 
</RelativeLayout>
Open src -> package -> MainActivity.java  :
package com.example.radiogroup;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
 RadioGroup rg;
 int pos;
 int pos1;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        rg = (RadioGroup) findViewById(R.id.radioGroup1);
        
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
   
  @Override
  public void onCheckedChanged(RadioGroup group, int checkedId) {
   // TODO Auto-generated method stub
   
   // Method 1 For Getting Index of RadioButton
   pos=rg.indexOfChild(findViewById(checkedId));

       Toast.makeText(getBaseContext(), "Method 1 ID = "+String.valueOf(pos),
    Toast.LENGTH_SHORT).show();
  
       //Method 2 For Getting Index of RadioButton
      pos1=rg.indexOfChild(findViewById(rg.getCheckedRadioButtonId()));

       Toast.makeText(getBaseContext(), "Method 2 ID = "+String.valueOf(pos1),
    Toast.LENGTH_SHORT).show();
      
       switch (pos)
       {
       case 0 :
    Toast.makeText(getBaseContext(), "You have Clicked RadioButton 1",
     Toast.LENGTH_SHORT).show();
        break;
       case 1 :
        Toast.makeText(getBaseContext(), "You have Clicked RadioButton 2",
     Toast.LENGTH_SHORT).show();
        break;
       case 2 :
        Toast.makeText(getBaseContext(), "You have Clicked RadioButton 3",
     Toast.LENGTH_SHORT).show();
        break;
   default :
    //The default selection is RadioButton 1
        Toast.makeText(getBaseContext(),"You have Clicked RadioButton 1" ,
     Toast.LENGTH_SHORT).show();
        break;
       }
  }
 });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
  RatingBar Example in Android
    Open res -> layout -> main.xml  :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="86dp"
        android:numStars="5" />
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="20dp" />
 
</RelativeLayout>
 Open src -> package -> MainActivity.java  :
package com.example.ratingbar;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.RatingBar.OnRatingBarChangeListener;
 
public class MainActivity extends Activity {
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        RatingBar rr=(RatingBar) findViewById(R.id.ratingBar1);
        final TextView t1=(TextView) findViewById(R.id.textView1);
        
        rr.setRating((float) 2);
        rr.setStepSize((float) 0.3);
        
        rr.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
   
  @Override
  public void onRatingChanged(RatingBar ratingBar, float rating,
    boolean fromUser) {
   // TODO Auto-generated method stub
    
   t1.setText(String.valueOf(rating));
  }
 });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }    
}
Static Switch Example in Android
  Open res -> layout ->  main.xml  :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:textSize="18sp"
        android:textColor="#4169E1"
        android:text="Static Switch" />
 
    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Switch" />
 
</RelativeLayout>
 Open src -> package -> MainActivity.java  :
package com.example.switch;
 
import android.os.Bundle;
import android.app.Activity;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
 
public class MainActivity extends Activity {
 
 Switch switch_controller;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        switch_controller = (Switch) findViewById(R.id.switch1);
        
        switch_controller.setOnCheckedChangeListener(new OnCheckedChangeListener() {
   
   @Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    // TODO Auto-generated method stub
    
    if(isChecked == true)
     Toast.makeText(getBaseContext(), "ON Stage", Toast.LENGTH_SHORT).show();
    else
     Toast.makeText(getBaseContext(), "OFF Stage", Toast.LENGTH_SHORT).show();
   }
  });
    }
}
 AutoCompleteTextView Example in Android
   Open res -> layout -> main.xml  :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:ems="10"
        android:hint="AutoCompleteTextView" />
 
</RelativeLayout>
 Open src -> package -> MainActivity.java  :
package com.example.autocompletetextview;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
 
public class MainActivity extends Activity {
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        String str[]={"Arun","Mathev","Vishnu","Vishal","Arjun",
          "Arul","Balaji","Babu","Boopathy","Godwin","Nagaraj"};
        
        AutoCompleteTextView t1 = (AutoCompleteTextView) 
          findViewById(R.id.autoCompleteTextView1);
        
        ArrayAdapter<String> adp=new ArrayAdapter<String>(this,
          android.R.layout.simple_dropdown_item_1line,str);
        
        t1.setThreshold(1);
        t1.setAdapter(adp);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }    
}
  Static ImageView Example in Android
  Open res -> layout ->  main.xml  :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <TextView
        android:id="@+id/textView1"
        android:textSize="20dp"
        android:textColor="#FF0000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="110dp"
        android:text="Static ImageView" />
 
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/gkbs_logo" />
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="80dp"
        android:text="Change" />
 
</RelativeLayout>
Add images in res -> drawable-hdpi folder(.png)
 Open src -> package -> MainActivity.java  :
package com.example.imageview;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
 
public class MainActivity extends Activity {
 
 ImageView img;
 Button b;
 int flag = 0;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        img = (ImageView) findViewById(R.id.imageView1);
        b = (Button) findViewById(R.id.button1);
        
        b.setOnClickListener(new View.OnClickListener() {
   
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   
   if (flag==0)
   {
    img.setImageResource(R.drawable.gkbs_logo_red);
    flag = 1;
   }
   
   else
    if(flag==1)
    {
     img.setImageResource(R.drawable.gkbs_logo_megenta);
     flag = 2;
    }
   
    else
     if(flag==2)
     {
      img.setImageResource(R.drawable.gkbs_logo);
      flag = 0;
     }
  }
 });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
  Gallery Example in Android
   Open res -> layout ->  main.xml  :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
  	<Gallery 
	    android:id="@+id/myGallery" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:gravity="bottom" />
 
  	<TextView
  	    android:id="@+id/textView1"
  	    android:textColor="#FF0000"
  	    android:textSize="20dp"
  	    android:layout_width="wrap_content"
  	    android:layout_height="wrap_content"
  	    android:layout_below="@+id/myGallery"
  	    android:layout_centerHorizontal="true"
  	    android:layout_marginTop="25dp"
  	    android:text="Static Gallery Creation" />
 
  	<TextView
  	    android:id="@+id/textView2"
  	    android:layout_width="wrap_content"
  	    android:layout_height="wrap_content"
  	    android:layout_below="@+id/textView1"
  	    android:layout_centerHorizontal="true"
  	    android:layout_marginTop="30dp" />
 
  	<ImageView
  	    android:id="@+id/imageView1"
  	    android:layout_width="wrap_content"
  	    android:layout_height="wrap_content"
  	    android:layout_alignParentBottom="true"
  	    android:layout_marginBottom="30dp"
  	    android:layout_centerHorizontal="true" />
  	
</RelativeLayout>
  Open src -> package -> MainActivity.java  :
package com.example.gallery;
 
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
 
public class MainActivity extends Activity {
 
	TextView mySelection;
	Gallery myGallery;
	ImageView img;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		final int[] myImageIds = { 
				R.drawable.android, 
				R.drawable.cup, 
				R.drawable.donut, 
				R.drawable.eclair,
				R.drawable.froyo,
				R.drawable.ginger,
				R.drawable.honey,
				R.drawable.ics,
				R.drawable.jellybean
		};
		
		mySelection = (TextView) findViewById(R.id.textView2);		
		myGallery = (Gallery) findViewById(R.id.myGallery);
		img = (ImageView) findViewById(R.id.imageView1);
 
		myGallery.setAdapter(new ImageAdapter(this));
		
		myGallery.setOnItemSelectedListener(new OnItemSelectedListener() {
 
			public void onItemSelected(AdapterView<?> parent, View v,
					int position, long id) {
				
				mySelection.setText(" selected option: " + position );
				
				img.setImageResource(myImageIds[position]);
			}
 
			public void onNothingSelected(AdapterView<?> parent) {
				
				mySelection.setText("Nothing selected");
			}
		});
	}
 
	public class ImageAdapter extends BaseAdapter {
 
		private Context myContext;
		
		private int[] myImageIds = { 
				R.drawable.android, 
				R.drawable.cup, 
				R.drawable.donut, 
				R.drawable.eclair,
				R.drawable.froyo,
				R.drawable.ginger,
				R.drawable.honey,
				R.drawable.ics,
				R.drawable.jellybean
		};

		public ImageAdapter(Context c) {
			this.myContext = c;
		}
 
		public int getCount() {
			return this.myImageIds.length;
		}
 
		public Object getItem(int position) {
			return position;
		}
 
		public long getItemId(int position) {
			return position;
		}
		// Returns a new ImageView to be displayed,
		public View getView(int position, View convertView, 
				ViewGroup parent) {
 
			ImageView iv = new ImageView(this.myContext);
			iv.setImageResource(this.myImageIds[position]);
 
			iv.setScaleType(ImageView.ScaleType.FIT_END);
			
			// Set the Width & Height of the individual images
			iv.setLayoutParams(new Gallery.LayoutParams(95, 70));
 
			return iv;
		}
	}
}
  MediaPlayer (Audio) Example in Android
  Open res -> layout -> main.xml  :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="Start" />
 
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="120dp"
        android:text="Stop" />
 
</RelativeLayout>
   Open res -> raw and add beep.mp3 file
  Open src -> package -> MainActivity.java :
package com.example.mediaplayer;
 
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
	MediaPlayer media;
	Button start, stop;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        start = (Button) findViewById(R.id.button1);
        stop = (Button) findViewById(R.id.button2);
        
        media = MediaPlayer.create(getBaseContext(), R.raw.beep);
        
        start.setOnClickListener(new View.OnClickListener() {
			
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			if(media.isPlaying()==false)
			{
				media.start();
			}
			
			play();
		}
	});
        
        stop.setOnClickListener(new View.OnClickListener() {
			
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			if(media.isPlaying()==true)
			{
				media.stop();
			}
		
			play();
		}
	});        
    }
    
    public void play()
    {
    	boolean play = media.isPlaying();
    	
    	if(play==true)
	{
		Toast.makeText(getBaseContext(), "Song is Playing", 
				Toast.LENGTH_SHORT).show();
	}
    	
    	else 
    	{
    		Toast.makeText(getBaseContext(), "Song is not Playing", 
    				Toast.LENGTH_SHORT).show();
    	}
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
 Static VideoView Example in Android
   Open res -> layout ->  main.xml  :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <TextView
        android:id="@+id/textView1"
        android:textSize="18dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Static VideoView" />
    
    <VideoView
        android:id="@+id/videoView1"
        android:layout_marginTop="30dp"
        android:layout_below="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
</RelativeLayout>
    Open res -> raw and add ss.3gp file
   Open src -> package -> MainActivity.java  :
package com.example.videoview;
 
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.MediaController;
import android.widget.VideoView;
 
public class MainActivity extends Activity {
	
	VideoView video;
	MediaController controller;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Uri UriPath = Uri.parse
        		("android.resource://balaji.videoview_static/" + R.raw.ss);  
       		
        video = (VideoView) findViewById(R.id.videoView1);
        controller = new MediaController(MainActivity.this);
        
        video.setMediaController(controller); 
        video.setVideoURI(UriPath);
        
        video.requestFocus();
        video.start();
        
        /*MediaController already has all the features to control the Video.
         * If you want to stop the video in programatically, follow below code :
         * 
         * 		if(video.isPlaying()==true)
         * 		{
         * 			video.stopPlayback();
         * 			video.clearFocus();
         * 		}       	
         * 
         */        
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
Static TimePicker Example in Android
   Open res -> layout -> main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <TextView
        android:id="@+id/textView1"
        android:textSize="20dp"
        android:textColor="#FF0000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="Static TimePicker" />
 
    <TimePicker
        android:id="@+id/timePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="45dp"
        android:text="Pick" />
 
</RelativeLayout>
 Open src -> package -> MainActivity.java and add following code :
package com.example.timepicker;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.TimePicker.OnTimeChangedListener;
 
public class MainActivity extends Activity {
 
	Button pick;
	TimePicker time;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        pick = (Button) findViewById(R.id.button1);
        time = (TimePicker) findViewById(R.id.timePicker1);
        
        time.setOnTimeChangedListener(new OnTimeChangedListener() {
			
		@Override
		public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
			// TODO Auto-generated method stub
				
			Toast.makeText(getBaseContext(),"Time is "+hourOfDay+ " : "
				+minute, Toast.LENGTH_SHORT).show();
		}
	});
        
        pick.setOnClickListener(new View.OnClickListener() {
			
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
				
			Toast.makeText(getBaseContext(),"Time is "+time.getCurrentHour()+ 
				" : " +time.getCurrentMinute(), Toast.LENGTH_LONG).show();
		}
	});        
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
  Static CalendarView Example in Android
      Open res -> layout ->  main.xml  :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    
    <TextView
        android:id="@+id/textView1"
        android:textColor="#FF0000"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="Static CalendarView" />
 
    <CalendarView
        android:id="@+id/calendarView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="60dp" />
    
 
</RelativeLayout>
 Open src -> package -> MainActivity.java  :
package com.example.calendar;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CalendarView;
import android.widget.CalendarView.OnDateChangeListener;
import android.widget.Toast;
 
public class MainActivity extends Activity {
	
	CalendarView cal;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        cal = (CalendarView) findViewById(R.id.calendarView1);
        
        cal.setOnDateChangeListener(new OnDateChangeListener() {
			
		@Override
		public void onSelectedDayChange(CalendarView view, int year, int month,
				int dayOfMonth) {
			// TODO Auto-generated method stub
			
			Toast.makeText(getBaseContext(),"Selected Date is\n\n"
				+dayOfMonth+" : "+month+" : "+year , 
				Toast.LENGTH_LONG).show();
		}
	});
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
  Chronometer Example in Android
  Open res -> layout ->  main.xml  :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <TextView
        android:id="@+id/textView1"
        android:textColor="#FF0000"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Static Chronometer" />
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="Start" />
    
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="Stop" />
    
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="Restart" />
    
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button3"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="Set Format" />
    
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button4"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="Clear Format" />
 
    <Chronometer
        android:id="@+id/chronometer1"
        android:textColor="#4169E1"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button5"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Chronometer" />
 
</RelativeLayout>
Open src -> package -> MainActivity.java  :
package com.example.chronometerc;
 
import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
 
public class MainActivity extends Activity {
	
	Chronometer focus;
	Button start, stop, restart, set, clear;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        start = (Button) findViewById(R.id.button1);
        stop = (Button) findViewById(R.id.button2);
        restart = (Button) findViewById(R.id.button3);
        set = (Button) findViewById(R.id.button4);
        clear = (Button) findViewById(R.id.button5);
 
        focus = (Chronometer) findViewById(R.id.chronometer1);
        
        start.setOnClickListener(new View.OnClickListener() {
			
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			focus.start();
		}
	});
 
        
        stop.setOnClickListener(new View.OnClickListener() {
			
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			focus.stop();
		}
	});
        
        
        restart.setOnClickListener(new View.OnClickListener() {
			
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			focus.setBase(SystemClock.elapsedRealtime());
		}
	});
 
        
        set.setOnClickListener(new View.OnClickListener() {
			
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			focus.setFormat("Formated Time - %s");
		}
	});
        
        
        clear.setOnClickListener(new View.OnClickListener() {
			
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			focus.setFormat(null);
		}
	});
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
 Static Analog Clock Example in Android
      Open res -> layout -> main.xml  :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <TextView
        android:id="@+id/textView1"
        android:textSize="20dp"
        android:textColor="#FF0000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="Static Analog Clock" />
 
    <AnalogClock
        android:id="@+id/analogClock1"
        android:dial="@drawable/clock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
    
</RelativeLayout>
 Open src -> package -> MainActivity.java and add following code :
package com.example.analogclock;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AnalogClock;
import android.widget.Toast;
 
public class MainActivity extends Activity {
	
	AnalogClock clk;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        clk = (AnalogClock) findViewById(R.id.analogClock1);
        
        clk.setOnClickListener(new View.OnClickListener() {
			
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
				
			Toast.makeText(getBaseContext(), "This is Static AnalogClock",
				Toast.LENGTH_SHORT).show();
		}
	});
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
Static Digital Clock Example in Android
  Open res -> layout ->  main.xml  :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <TextView
        android:id="@+id/textView1"
        android:textSize="20dp"
        android:textColor="#FF0000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="Static DigitalClock" />
 
    <DigitalClock
        android:id="@+id/digitalClock1"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"  />
 
</RelativeLayout>
  Open src -> package -> MainActivity.java  :
package com.example.digitalclock;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.DigitalClock;
import android.widget.Toast;
 
public class MainActivity extends Activity {
	
	DigitalClock clk;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        clk = (DigitalClock) findViewById(R.id.digitalClock1);
        
        clk.setOnClickListener(new View.OnClickListener() {
			
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
				
			Toast.makeText(getBaseContext(), clk.getText().toString(),
				Toast.LENGTH_SHORT).show();
		}
	});
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Step 4 : Open AndroidManifest.xml and add following code :