Monday, August 9, 2021

How to Use Time Picker in Android to Pick Time By Hour and Minute

Time Picker functionality is a widget that allows users to pick a specific period of the day or AM or PM or 24 hours clock mode. In this segment, the display period in the widget consists of clock format such as hours, minutes. And, to show this widget in your Mobile App Development, you will have to use a TimePickerDialog class. Android Date Time picker is utilized in various android applications.

We will describe the use of a Date Picker and Timer Picker Dialog in the android app where these components have been used to select date and time in a customized user interface (UI) also we can utilize the function of  DatePickerDialog and TimePickerDialog classes with the accessible calendar class in android code to complete the target.

 

Before all that, Hire Android App Developer who has a good knowledge to implement your idea into reality.

 

If you are expert in Android Studio the create project and start:

 

create java class name “MainActivity”

 

public class MainActivity extends AppCompatActivity {

static final int TIME_DIALOG_ID = 1111;

private TextView view;

public Button btnClick;

private int hr;

private int min;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

view = (TextView) findViewById(R.id.output);

final Calendar c = Calendar.getInstance();

hr = c.get(Calendar.HOUR_OF_DAY);

min = c.get(Calendar.MINUTE);

updateTime(hr, min);

addButtonClickListener();

}

public void addButtonClickListener() {

btnClick = (Button) findViewById(R.id.btnClick);

btnClick.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

createdDialog(0).show();

}

});

}

protected Dialog createdDialog(int id) {

switch (id) {

case TIME_DIALOG_ID:

return new TimePickerDialog(this, timePickerListener, hr, min, false);

}

return null;

}

private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {

@Override

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

// TODO Auto-generated method stub

hr = hourOfDay;

min = minutes;

updateTime(hr, min);

}

};

private static String utilTime(int value) {

if (value < 10) return "0" + String.valueOf(value); else return String.valueOf(value); } private void updateTime(int hours, int mins) { String timeSet = ""; if (hours > 12) {

hours -= 12;

timeSet = "PM";

} else if (hours == 0) {

hours += 12;

timeSet = "AM";

} else if (hours == 12)

timeSet = "PM";

else

timeSet = "AM";

String minutes = "";

if (mins < 10)

minutes = "0" + mins;

else

minutes = String.valueOf(mins);

String aTime = new StringBuilder().append(hours).append(':').append(minutes).append(" ").append(timeSet).toString();

view.setText(aTime);

}

 

 activity_main.xml

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:gravity="center"

android:orientation="vertical" >

<Button

android:id="@+id/btnClick"

android:padding="20dp"

android:background="@color/colorPrimary"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Click Me &amp; Change Time" />

<TextView

android:id="@+id/lblTime"

android:layout_marginTop="20dp"

android:layout_marginBottom="20dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Current Time"

android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView

android:id="@+id/output"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=""

android:textColor="@color/colorAccent"

android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


After successfully doing this project, we can see the result of compiling.

  

When a mobile app is complex, then TimePickerDialog integration becomes a bit challenging, the further discussion you can consult with Android App Development Company to add a custom Time picker in the Android app perfectly.

Contact Us :

Website : https://deorwine.com

Company Name : Deorwine Infotech

Email id : info@deorwine.com

Skype : deorwineinfotech

 

For Any Query In India Call Us: +91-9950686795 OR  +91-9116115717

For Any Query In USA Call Us: +1 860-840-2896


No comments:

Post a Comment

Astrology App Development Costs Revealed: Unlocking the Secrets of Astrotalk

The astrology app industry is booming, with users seeking insights into their lives through horoscopes and astrological advice. This surge i...