Wednesday, 17 August 2016

Picker


Android provides controls for the user to pick a time or pick a date as ready-to-use dialogs. Each picker provides controls for selecting each part of the time (hour, minute, AM/PM) or date (month, day, year). Using these pickers helps ensure that your users can pick a time or date that is valid, formatted correctly, and adjusted to the user's locale.

We recommend that you use DialogFragment to host each time or date picker. The DialogFragment manages the dialog lifecycle for you and allows you to display the pickers in different layout configurations, such as in a basic dialog on handsets or as an embedded part of the layout on large screens.

Simple Example  - Without dialog_fragment
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:id="@+id/LinearLayout1"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical"
   
android:background="#FF550D"
   
>
    <
TextView
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:id="@+id/textView1"
       
android:text="hello"
       
android:textSize="30dp"
       
android:background="#ffffff"
       
/>
    <
TimePicker
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:id="@+id/timePicker" />


</
LinearLayout>

MainActivity.java
public class MainActivity extends Activity

{

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        TextView textView=(TextView)findViewById(R.id.textView1);

        TimePicker timePicker=(TimePicker)findViewById(R.id.timePicker);



        textView.setText(timePicker.getCurrentHour()+":"+timePicker.getCurrentMinute());

//@ depreacated .getCurrentHour()+":"+timePicker.getCurrentMinute()


    }

}

1. setCurrentHour(Integer currentHour):
This method is used to set the current hours in a time picker.
setHour(Integer hour): setCurrentHour() method was deprecated in API level 23. From api level 23 we have to use setHour(Integer hour). In this method there is only one parameter of integer type which is used to set the value for hours.
Code -
timePicker.setCurrentHour(5);
2. setCurrentMinute(Integer currentMinute):
This method is used to set the current minutes in a time picker.
setMinute(Integer minute): setCurrentMinute() method was deprecated in API level 23. From api level 23 we have to use setMinute(Integer minute). In this method there is only one parameter of integer type which set the value for minutes.
Code -
timePicker.setCurrentMinute(5);



3. getCurrentHour():
This method is used to get the current hours from a time picker.
getCurrentHour(): getCurrentHour() method was deprecated in API level 23. From api level 23 you have to use getHour(). This method returns  an integer value.
timePicker.getCurrentHour();
 
4. getCurrentMinute():
This method is used to get the current minutes from a time picker.
getMinute(): getCurrentMinute() method was deprecated in API level 23. From api level 23 we have to use getMinute(). This method returns  an integer value.
timePicker.getCurrentMinute()

5. setIs24HourView(Boolean is24HourView):
This method is used to set the mode of the Time picker either 24 hour mode or AM/PM mode. In this method we set a Boolean value either true or false. True value indicate 24 hour mode and false value indicate AM/PM mode.
timePicker.setIs24HourView(false);
 
6. is24HourView():
This method is used to check the current mode of the time picker. This method returns true if its 24 hour mode or false if AM/PM mode is set.
if(timePicker.is24HourView()) {

    Toast.makeText(getApplicationContext(), "24 hour format",Toast.LENGTH_LONG).show();

} else {

    Toast.makeText(getApplicationContext(), "Not 24 hour format",Toast.LENGTH_LONG).show();

}

Complete Code -
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:background="#FF550D"

    >

    <TextView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/textView1"

        android:text="hello"

        android:textSize="30dp"

        android:background="#ffffff"

        />

    <TimePicker

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/timePicker" />



    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Set Hour (5)"

        android:onClick="goHour"

        />

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Set Minute(5)"

        android:onClick="goMinute"

        />



    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Set Minute(5)"

        android:onClick="goCheckView"

        />

</LinearLayout>

MainActivity.java
public class MainActivity extends Activity

{

    TimePicker timePicker;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        TextView textView=(TextView)findViewById(R.id.textView1);

        timePicker=(TimePicker)findViewById(R.id.timePicker);



        timePicker.setIs24HourView(false);







        textView.setText(timePicker.getCurrentHour()+":"+timePicker.getCurrentMinute());



    }

    public void goHour(View view)

    {

        timePicker.setCurrentHour(5);

    }

    public void goMinute(View view)

{

    timePicker.setCurrentMinute(5);

}

    public void goCheckView(View view)

    {

        if(timePicker.is24HourView()) {

            Toast.makeText(getApplicationContext(), "24 hour format",Toast.LENGTH_LONG).show();

        } else {

            Toast.makeText(getApplicationContext(), "Not 24 hour format",Toast.LENGTH_LONG).show();

        }

    }

}
*************************************************************************

Example 2 - Time picker with Dialog
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:background="#FF550D"

    >

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Pick Time"

        android:onClick="showTimePickerDialog" />

    <TextView

        android:id="@+id/textView1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Time Show"

        android:textSize="30dp"

        android:background="#ffffff"

        />



</LinearLayout>

MainActivity.java
public class MainActivity extends Activity

{

    static TextView textView1;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        textView1=(TextView)findViewById(R.id.textView1);

    }

    public void showTimePickerDialog(View v) {

        DialogFragment newFragment = new TimePickerFragment();

        newFragment.show(getFragmentManager(), "timePicker");

    }

    public static class TimePickerFragment extends DialogFragment

            implements TimePickerDialog.OnTimeSetListener {



        @Override

        public Dialog onCreateDialog(Bundle savedInstanceState) {

            // Use the current time as the default values for the picker

            final Calendar c = Calendar.getInstance();

            int hour = c.get(Calendar.HOUR_OF_DAY);

            int minute = c.get(Calendar.MINUTE);



            // Create a new instance of TimePickerDialog and return it

            return new TimePickerDialog(getActivity(), this, hour, minute,

                    DateFormat.is24HourFormat(getActivity()));

        }



        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

            // Do something with the time chosen by the user

            String sHour=String.valueOf(hourOfDay);

            String sMinute=String.valueOf(minute);

            textView1.setText(sHour+":"+sMinute);

        }

    }

}

--------------------------------------------------------------------------------------------
Above program Description
1. TimePicker - android.widget.TimePicker is a view in which we can select hour and minute of the day using 24 hour view or 12 hour view with AM and PM. We can select hour and minute using vertical spinner.
2.  Dialog Fragment - A fragment that displays a dialog window, floating on top of its activity’s window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment’s state. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.
3. TimePicker Dialog - TimePickerDialog is displays a dialog for TimePicker.
4. getFragmentManager -  Return the FragmentManager for interacting with fragments associated with this activity.
5. onCreateDialog - Override to build your own custom Dialog container.
6. Calender - Calendar is an abstract base class for converting between a Date object and a set of integer fields such as YEAR, MONTH, DAY, HOUR, and so on. (A Dateobject represents a specific instant in time with millisecond precision.
7. HOUR_OF_DAY -  It shows the the current Hour that’s visible when the dialog pops up and return int value.
8. Minute - It shows the the current minute that’s visible when the dialog pops up and return int value.
9. DateFormat -  Formats and parses dates in a locale-sensitive manner. Formatting turns a Date into a String, and parsing turns a String into a Date.
10. is24HourFormat : This method returns true if this is in 24 hour view else false.


Style the timepicker
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>

<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:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="sichlearning.example.com.timepickermd.MainActivity">



    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Show TimePicker Dialog"

        />



</LinearLayout>

MainActivity.java


import android.app.Dialog;

import android.app.TimePickerDialog;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TimePicker;

import android.widget.Toast;



public class MainActivity extends AppCompatActivity {



    Button b1;

    static final int DIALOG_ID=0;

    int mHour,mMinute;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        showTimePiacker();



    }







   public void showTimePiacker(){

       b1=(Button)findViewById(R.id.button1);

       b1.setOnClickListener(new View.OnClickListener() {

           @Override

           public void onClick(View v) {

               showDialog(DIALOG_ID);

           }

       });

   }



    @Override

    protected Dialog onCreateDialog(int id) {

        if (id == DIALOG_ID)



            return new TimePickerDialog(MainActivity.this,TimePickerDialog.THEME_DEVICE_DEFAULT_DARK, kTimePickerListener, mHour, mMinute, false);

        return null;



    }





    public void showTimePickerDialog() {

        b1 = (Button) findViewById(R.id.button1);

        b1.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                showDialog(DIALOG_ID);

            }

        });

    }



    protected TimePickerDialog.OnTimeSetListener kTimePickerListener = new TimePickerDialog.OnTimeSetListener() {

        @Override

        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {



            mHour = hourOfDay;

            mMinute = minute;

            Toast.makeText(getApplicationContext(), "" + mHour + ":" + mMinute + "", Toast.LENGTH_LONG).show();



        }

    };

}















































22


No comments:

Post a Comment