How To Pick A Second Using Timepicker, Android
Solution 1:
I've published an open source project on GitHub which has TimePicker with seconds:
https://github.com/IvanKovac/TimePickerWithSeconds
Have a look.
Solution 2:
I think the best solution is to create your own TimePicker, by using the Time-class and three NumberPickers.
Solution 3:
One quick and dirty way is to use two TimePickers, One for hours,minutes, and use the minutes on the other one as secunds. Hide the unused hours under the first minutes. This only works in 24 hour mode. Must declare timepicker for seconds first so locates under.
<RelativeLayout
<TimePickerandroid:id="@+id/timePicker_Sec"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="76dp" /><TimePickerandroid:id="@+id/timePicker"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignTop="@+id/timePicker_Sec"android:layout_marginLeft="0dp" /></RelativeLayout>
Solution 4:
The best way around this would be to create three separate 'number pickers' instead of timepicker... I placed them within a linear layout and its working fine.
Then all you do is store the values inside three separate variables. int Hours, int Minutes, int Seconds and do your calculating.
This was my way around it without downloading any external sources which may contain viruses.
<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"><NumberPickerandroid:id="@+id/numpicker_hours"android:layout_width="wrap_content"android:layout_height="wrap_content"></NumberPicker><NumberPickerandroid:id="@+id/numpicker_minutes"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="8dp"android:layout_marginRight="8dp"></NumberPicker><NumberPickerandroid:id="@+id/numpicker_seconds"android:layout_width="wrap_content"android:layout_height="wrap_content"></NumberPicker></LinearLayout>
Solution 5:
Here is a custom TimePickerDialog with seconds.
MainActivity.java
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.TextView;
publicclassMainActivityextendsAppCompatActivity {
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
finalSharedPreferencessharedPreferences= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
finalTextViewtimeTV= findViewById(R.id.time_text_view);
timeTV.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
Viewview= View.inflate(MainActivity.this, R.layout.time_dialog, null);
finalNumberPickernumberPickerHour= view.findViewById(R.id.numpicker_hours);
numberPickerHour.setMaxValue(23);
numberPickerHour.setValue(sharedPreferences.getInt("Hours", 0));
finalNumberPickernumberPickerMinutes= view.findViewById(R.id.numpicker_minutes);
numberPickerMinutes.setMaxValue(59);
numberPickerMinutes.setValue(sharedPreferences.getInt("Minutes", 0));
finalNumberPickernumberPickerSeconds= view.findViewById(R.id.numpicker_seconds);
numberPickerSeconds.setMaxValue(59);
numberPickerSeconds.setValue(sharedPreferences.getInt("Seconds", 0));
Buttoncancel= view.findViewById(R.id.cancel);
Buttonok= view.findViewById(R.id.ok);
AlertDialog.Builderbuilder=newAlertDialog.Builder(MainActivity.this);
builder.setView(view);
finalAlertDialogalertDialog= builder.create();
cancel.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
alertDialog.dismiss();
}
});
ok.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
timeTV.setText(numberPickerHour.getValue() + ":" + numberPickerMinutes.getValue() + ":" + numberPickerSeconds.getValue());
// timeTV.setText(String.format("%1$d:%2$02d:%3$02d", numberPickerHour.getValue(), numberPickerMinutes.getValue(), numberPickerSeconds.getValue()));
SharedPreferences.Editoreditor= sharedPreferences.edit();
editor.putInt("Hours", numberPickerHour.getValue());
editor.putInt("Minutes", numberPickerMinutes.getValue());
editor.putInt("Seconds", numberPickerSeconds.getValue());
editor.apply();
alertDialog.dismiss();
}
});
alertDialog.show();
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextViewandroid:id="@+id/time_text_view"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Click Me"android:textSize="24sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /></android.support.constraint.ConstraintLayout>
time_dialog.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_margin="16dp"android:gravity="center"android:orientation="horizontal"><NumberPickerandroid:id="@+id/numpicker_hours"android:layout_width="wrap_content"android:layout_height="wrap_content" /><NumberPickerandroid:id="@+id/numpicker_minutes"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="16dp"android:layout_marginRight="16dp" /><NumberPickerandroid:id="@+id/numpicker_seconds"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout><Viewandroid:id="@+id/view"android:layout_width="match_parent"android:layout_height="1dp"android:background="#ffF0F0F0" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"><Buttonandroid:id="@+id/cancel"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:background="@android:color/transparent"android:text="Cancel"android:textAllCaps="false" /><Viewandroid:id="@+id/view2"android:layout_width="1dp"android:layout_height="match_parent"android:background="#ffF0F0F0" /><Buttonandroid:id="@+id/ok"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:background="@android:color/transparent"android:text="OK"android:textAllCaps="true" /></LinearLayout></LinearLayout>
Post a Comment for "How To Pick A Second Using Timepicker, Android"