Skip to main content

Android TimePickerDialog

In this tutorial I want to explain about creating a  TimePickerDialog like,



In this I am having One Button and One EditText. On this example first it will load the current time to the EditText, While Tap on the button it will display the TimePickerDialog with the current time after tap on Done button TimePickerDialog it will update in the EditText.
 


activity_main.xml

This is a Layout of main page. This layout contain One Button and one EditText.



<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"
    android:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Time" />

    <EditText
         android:id="@+id/EdtTxtTime"
         android:layout_width="wrap_content"
          android:layout_height="wrap_content"
         android:ems="10"
         android:focusableInTouchMode="false"
         android:inputType="text" />

</LinearLayout>



MainActivity.java
 
public class MainActivity extends Activity {
       Button BtnSelectTime;
   private EditText EdtTxtTime;
   private static Context context;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

           context=MainActivity.this;
           EdtTxtTime = (EditText) findViewById(R.id.EdtTxtTime);

            String Time = getCurrentTime();
            if(Integer.parseInt(Time.substring(0, 2).toString())>12){
                Time = (Integer.parseInt(Time.substring(0, 2).toString())-12)+Time.substring(2);              
            }
            EdtTxtTime.setText(Time);
       
        BtnSelectTime = (Button) findViewById(R.id.BtnSelectTime);
        BtnSelectTime.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
              
                             Calendar mcurrentTime = Calendar.getInstance();
                              int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                              int minute = mcurrentTime.get(Calendar.MINUTE);
                              TimePickerDialog mTimePicker;
                              mTimePicker = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
                                  @Override
                                  public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                                      EdtTxtTime.setText( selectedHour + ":" + selectedMinute);
                                  }
                              }, hour, minute, true);//Yes 24 hour time
                              mTimePicker.setTitle("Select Time");
                              mTimePicker.show();

            }
        });
       
    }

//Function to get current time
    public static String getCurrentTime() {
            String DATE_FORMAT_NOW = "HH:mm";
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
            return sdf.format(new Date());
     }
   
}

Popular posts from this blog

Importing Image From Camera

In this tutorial I want to explain how to take a picture in camera ans import that picture to am imageview,

In this I am having One Button and One ImageView. While Tap on the button it will open Camera. After that the user can take photo After taking photo it will ask for save image. If the user save the image then it will display on the Imageview.

activity_main.xml

This is a Layout of main page. This layout contain One Button and one Imageview.



<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"
    android:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_con…

How to pass Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List with an intent.

In this blog I want to explain How to pass Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List with an intent.


Intent
                Intent is an object that is used to launch an activity or to return information  from an activity.  To start a new activity using startActivity(intent).  To get information from another activity we start activity using startActivityForResult(intent, requestCode).


               Here in  FromPage.java we are adding  Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List values to an intent. In ToPage.java We extract added values will retrieve from intent. 


FromPage.java

ArrayList<Integer>  IntArrayList = new  ArrayList<Integer>(Arrays.asList(100,13,18,22,44,13));
ArrayList<String>  StringArrayList = new  ArrayList<String>(Arrays.asList("String1", "String2", "String3", "String4"));

        int[] intList = {4, 6, 3, …

Importing Image From Gallery

In this tutorial I want to explain how to import images from Gallery to am imageview,

In this I am having One Button and One ImageView. While Tap on the button it will redirect to Gallery to Browse image. While we select an image it will display on the Imageview.

activity_main.xml

This is a Layout of main page. This layout contain One Button and one Imageview.



<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"
    android:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Insert Image" />

    <ImageView
        …