How to implement TimePicker in any android studio project
Time Picker in Android is a widget that lets users to select the time of the day in either AM/PM or 24 hours mode. The displayed time in the widget consists of hours, minutes, and the clock format. And, in order to show add this widget in your mobile app, you'll have to use a TimePickerDialog class.
activity_main.xml
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=".MainActivity" >
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="Selected Time:"
android:textColor="@android:color/background_dark"
android:textSize="18sp" />
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/timePicker"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="Save" />
MainActivity.java
package com.friendvilla.simplecode;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends Activity {
TextView textview;
TimePicker timepicker;
Button changetime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview=(TextView)findViewById(R.id.textView);
timepicker=(TimePicker)findViewById(R.id.timePicker);
timepicker.setIs24HourView(true);
changetime=(Button)findViewById(R.id.button);
textview1.setText(getCurrentTime());
changetime.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
textview1.setText(getCurrentTime());
}
});
}
public String getCurrentTime() {
String currentTime = "Selected Time: " + timepicker.getCurrentHour() + ":" + timepicker.getCurrentMinute();
return currentTime;
}
}
Tutorial/Demo Video: not Available