activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" tools:context=".MainActivity" android:background="#e4e5f0" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp" android:textColor="#3c4ffa" android:fontFamily="sans-serif-condensed" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Set Time" android:layout_below="@id/tv" /> </RelativeLayout> MainActivity.java
package com.cfsuman.me.androidcodesnippets; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.app.DialogFragment; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get the widgets reference from XML layout jawaban RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl); Button btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Initialize a new time picker dialog fragment DialogFragment dFragment = new TimePickerFragment(); // Show the time picker dialog fragment dFragment.show(getFragmentManager(),"Time Picker"); } }); } } TimePickerFragment.java
package com.cfsuman.me.androidcodesnippets; import android.app.AlertDialog; import android.app.TimePickerDialog; import android.os.Bundle; import android.widget.TextView; import android.app.DialogFragment; import android.app.Dialog; import java.util.Calendar; import android.widget.TimePicker; public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{ @Override public Dialog onCreateDialog(Bundle savedInstanceState){ // Get a Calendar instance jawaban Calendar calendar = Calendar.getInstance(); // Get the current hour and minute int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); /* Creates a new time picker dialog with the specified theme. TimePickerDialog(Context context, int themeResId, TimePickerDialog.OnTimeSetListener listener, int hourOfDay, int minute, boolean is24HourView) */ // TimePickerDialog Theme : THEME_DEVICE_DEFAULT_LIGHT TimePickerDialog tpd = new TimePickerDialog(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,this,hour,minute,false); // TimePickerDialog Theme : THEME_DEVICE_DEFAULT_DARK TimePickerDialog tpd2 = new TimePickerDialog(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_DARK,this,hour,minute,false); // TimePickerDialog Theme : THEME_HOLO_DARK TimePickerDialog tpd3 = new TimePickerDialog(getActivity(), AlertDialog.THEME_HOLO_DARK,this,hour,minute,false); // TimePickerDialog Theme : THEME_HOLO_LIGHT TimePickerDialog tpd4 = new TimePickerDialog(getActivity(), AlertDialog.THEME_HOLO_LIGHT,this,hour,minute,false); // TimePickerDialog Theme : THEME_TRADITIONAL TimePickerDialog tpd5 = new TimePickerDialog(getActivity(), AlertDialog.THEME_TRADITIONAL,this,hour,minute,false); // Return the TimePickerDialog return tpd; } public void onTimeSet(TimePicker view, int hourOfDay, int minute){ // Do something with the returned time TextView tv = (TextView) getActivity().findViewById(R.id.tv); tv.setText("HH:MM\n" + hourOfDay + ":" + minute); } }
- TimePickerDialog example
- How to get AM PM value from TimePickerDialog
- DatePickerDialog example
- How to format DatePickerDialog selected date
- How to use theme in DatePickerDialog
- How to set DatePickerDialog max date and min date
- How to create a custom Title for DatePickerDialog
- DatePicker example
- How to create a DatePicker without calendar
- TimePicker example
Komentar
Posting Komentar