Android TimePicker Dialog

Android TimePicker:

In this session we will learn to use TimePicker.

Create new project in eclipse (Android-First-Programm).

In res -> layout open “activity_main.xml” and use button.

activity_main.xml

<LinearLayout 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/btnTimePicker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
  </LinearLayout>

In MainActivity.java get button reference and using Calendar class assign current time to Button.

 

MainActivity.java

[sociallocker]

package com.example.androidtimepicker;
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class MainActivity extends Activity {
 Button mBtnTimePicker;
 int mHour;
 int mMinute;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mBtnTimePicker=(Button) findViewById(R.id.btnTimePicker);
  Calendar calendar=Calendar.getInstance();//Constructs a new instance of the Calendar

  mHour=calendar.get(Calendar.HOUR);
  mMinute=calendar.get(Calendar.MINUTE);
  
    String currentTime=mHour+":"
    +mMinute;  
   mBtnTimePicker.setText(currentTime);
 }       
}

Now Button click open TimePicker dialog and set that time to Button, So complete MainActivity.java will looks like this.

MainActivity.java

package com.example.androidtimepicker;
 import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
 public class MainActivity extends Activity {
 static final int TIME_PICKER_ID=2;
 Button mBtnTimePicker;
 int mHour;
 int mMinute;
   @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mBtnDatePicker=(Button) findViewById(R.id.btnTimePicker);
  Calendar calendar=Calendar.getInstance(); //Constructs a new instance of the Calendar
  mHour=calendar.get(Calendar.HOUR);
  mMinute=calendar.get(Calendar.MINUTE);
   String currentTime=mHour+":"
    +mMinute;  
   mBtnTimePicker.setText(currentTime);
   mBtnTimePicker.setOnClickListener(new View.OnClickListener() {
  @Override
   public void onClick(View v) {
    showDialog(TIME_PICKER_ID);
   }
  });
 }
   @Override
 protected Dialog onCreateDialog(int dialogId) {
  switch(dialogId){ 
  case TIME_PICKER_ID:  
   return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false);
  default:   return null; 
  }
 }
 private TimePickerDialog.OnTimeSetListener mTimeSetListener= new TimePickerDialog.OnTimeSetListener(){
  @Override

   public void onTimeSet(android.widget.TimePicker view, int hourOfDay, int minute) {  
   String time=String.valueOf(hourOfDay) + ":"  + String.valueOf(minute) ;
   mBtnTimePicker.setText(time);   
  }  };
}

[/sociallocker]

Leave a comment

Your email address will not be published.