AdSense

AdSense3

Tuesday, 4 August 2015

Android TimePicker Example

android time picker
Android TimePicker widget is used to select date. It allows you to select time by hour and minute. You cannot select time by seconds.
The android.widget.TimePicker is the subclass of FrameLayout class.

Android TimePicker Example

Let's see a simple example of android time picker.

activity_main.xml

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <TimePicker  
  8.         android:id="@+id/timePicker1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentTop="true"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:layout_marginTop="86dp" />  
  14.   
  15.     <TextView  
  16.         android:id="@+id/textView1"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_alignLeft="@+id/timePicker1"  
  20.         android:layout_alignParentTop="true"  
  21.         android:layout_marginTop="17dp"  
  22.         android:text="Current Time:" />  
  23.   
  24.     <Button  
  25.         android:id="@+id/button1"  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_alignLeft="@+id/timePicker1"  
  29.         android:layout_below="@+id/timePicker1"  
  30.         android:layout_marginLeft="37dp"  
  31.         android:layout_marginTop="55dp"  
  32.         android:text="Change Time" />  
  33.   
  34. </RelativeLayout>  

Activity class

File: MainActivity.java
  1. package com.example.timepicker1;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.TextView;  
  10. import android.widget.TimePicker;  
  11. import android.widget.Toast;  
  12.   
  13. public class MainActivity extends Activity {  
  14.     TextView textview1;  
  15.     TimePicker timepicker1;  
  16.     Button changetime;  
  17.       
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.           
  23.         textview1=(TextView)findViewById(R.id.textView1);  
  24.         timepicker1=(TimePicker)findViewById(R.id.timePicker1);  
  25.         //Uncomment the below line of code for 24 hour view  
  26.         timepicker1.setIs24HourView(true);  
  27.         changetime=(Button)findViewById(R.id.button1);  
  28.           
  29.         textview1.setText(getCurrentTime());  
  30.           
  31.         changetime.setOnClickListener(new OnClickListener(){  
  32.             @Override  
  33.             public void onClick(View view) {  
  34.                  textview1.setText(getCurrentTime());  
  35.             }  
  36.         });  
  37.           
  38.     }  
  39.   
  40.     public String getCurrentTime(){  
  41.         String currentTime="Current Time: "+timepicker1.getCurrentHour()+":"+timepicker1.getCurrentMinute();  
  42.         return currentTime;  
  43.     }  
  44.     @Override  
  45.     public boolean onCreateOptionsMenu(Menu menu) {  
  46.         // Inflate the menu; this adds items to the action bar if it is present.  
  47.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  48.         return true;  
  49.     }  
  50.   
  51. }  

Monday, 3 August 2015

Android DatePicker Example

android datepicker
Android DatePicker is a widget to select date. It allows you to select date by day, month and year. Like DatePicker, android also provides TimePicker to select time.
The android.widget.DatePicker is the subclass of FrameLayout class.

Android DatePicker Example

Let's see the simple example of datepicker widget in android.

activity_main.xml

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/textView1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginLeft="50dp"  
  14.         android:layout_marginTop="36dp"  
  15.         android:text="Current Date:" />  
  16.   
  17.     <Button  
  18.         android:id="@+id/button1"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_alignParentBottom="true"  
  22.         android:layout_centerHorizontal="true"  
  23.         android:layout_marginBottom="140dp"  
  24.         android:text="Change Date" />  
  25.   
  26.     <DatePicker  
  27.         android:id="@+id/datePicker1"  
  28.         android:layout_width="wrap_content"  
  29.         android:layout_height="wrap_content"  
  30.         android:layout_above="@+id/button1"  
  31.         android:layout_centerHorizontal="true"  
  32.         android:layout_marginBottom="30dp" />  
  33.   
  34. </RelativeLayout>  

Activity class

File: MainActivity.java
  1. package com.example.datepicker2;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.DatePicker;  
  10. import android.widget.TextView;  
  11. import android.widget.Toast;  
  12.   
  13. public class MainActivity extends Activity {  
  14.     DatePicker picker;  
  15.     Button displayDate;  
  16.     TextView textview1;  
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.           
  22.         textview1=(TextView)findViewById(R.id.textView1);  
  23.         picker=(DatePicker)findViewById(R.id.datePicker1);  
  24.         displayDate=(Button)findViewById(R.id.button1);  
  25.           
  26.         textview1.setText(getCurrentDate());  
  27.           
  28.         displayDate.setOnClickListener(new OnClickListener(){  
  29.             @Override  
  30.             public void onClick(View view) {  
  31.                 textview1.setText(getCurrentDate());  
  32.             }  
  33.               
  34.         });  
  35.     }  
  36.     public String getCurrentDate(){  
  37.         StringBuilder builder=new StringBuilder();  
  38.         builder.append("Current Date: ");  
  39.         builder.append((picker.getMonth() + 1)+"/");//month is 0 based  
  40.         builder.append(picker.getDayOfMonth()+"/");  
  41.         builder.append(picker.getYear());  
  42.         return builder.toString();  
  43.     }  
  44.     @Override  
  45.     public boolean onCreateOptionsMenu(Menu menu) {  
  46.         // Inflate the menu; this adds items to the action bar if it is present.  
  47.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  48.         return true;  
  49.     }  
  50.   
  51. }  

Saturday, 1 August 2015

Android SeekBar Example

android seekbar
Android SeekBar is a kind of ProgressBar with draggable thumb. The end user can drag the thum left and right to move the progress of song, file download etc.
The SeekBar.OnSeekBarChangeListener interface provides methods to perform even handling for seek bar.
Android SeekBar and RatingBar classes are the sub classes of AbsSeekBar.

Android SeekBar Example

activity_main.xml

Drag the seek bar from the pallete, now activity_main.xml will look like this:
File: activity_main.xml
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <SeekBar  
  12.         android:id="@+id/seekBar1"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentTop="true"  
  16.         android:layout_centerHorizontal="true"  
  17.         android:layout_marginTop="39dp" />  
  18.   
  19. </RelativeLayout>  

Activity class

Let's see the Activity class displaying seek bar and performing event handling.
File: MainActivity.java
  1. package com.example.seekbar;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.view.Menu;  
  5. import android.widget.SeekBar;  
  6. import android.widget.SeekBar.OnSeekBarChangeListener;  
  7. import android.widget.Toast;  
  8. public class MainActivity extends Activity implements OnSeekBarChangeListener{  
  9.     SeekBar seekBar1;  
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_main);  
  14.           
  15.         seekBar1=(SeekBar)findViewById(R.id.seekBar1);  
  16.         seekBar1.setOnSeekBarChangeListener(this);  
  17.     }  
  18.     @Override  
  19.     public void onProgressChanged(SeekBar seekBar, int progress,  
  20.             boolean fromUser) {  
  21.         Toast.makeText(getApplicationContext(),"seekbar progress: "+progress, Toast.LENGTH_SHORT).show();  
  22.     }  
  23.     @Override  
  24.     public void onStartTrackingTouch(SeekBar seekBar) {  
  25.         Toast.makeText(getApplicationContext(),"seekbar touch started!", Toast.LENGTH_SHORT).show();  
  26.     }  
  27.     @Override  
  28.     public void onStopTrackingTouch(SeekBar seekBar) {  
  29.         Toast.makeText(getApplicationContext(),"seekbar touch stopped!", Toast.LENGTH_SHORT).show();  
  30.     }  
  31.     @Override  
  32.     public boolean onCreateOptionsMenu(Menu menu) {  
  33.         // Inflate the menu; this adds items to the action bar if it is present.  
  34.         getMenuInflater().inflate(R.menu.main, menu);  
  35.         return true;  
  36.     }  
  37. }  

Output:
android seekbar example output 1 android seekbar example output 2