Android Java Time Picker Dialog
hey I am trying to make a TimepickerDialog. But the app crashes soon as I press the Set Time button. Here is the code. MainActivity: package com.wifitimer; import java.text.DateFor
Solution 1:
I think you are calling chooseTime() from your xml layout file onClick attribute you need to change method signature to this
publicvoidchooseTime(View view){
new TimePickerDialog(this, t, dateTime.get(Calendar.HOUR_OF_DAY), dateTime.get(Calendar.MINUTE), true).show();
}
Solution 2:
change method like
publicvoidchooseTime(View view){
new TimePickerDialog(this, t, dateTime.get(Calendar.HOUR_OF_DAY), dateTime.get(Calendar.MINUTE), true).show();
}
and in You xml Control
Like
<ImageButton
android:id="@+id/imagestart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="chooseTime"
android:src="@drawable/ic_launcher" />
Solution 3:
For showing TimepickerDialog
, there's no need to add any type of permission.
mainActivity.java
publicclassMainActivityextendsActivity {
Button b1;
staticfinalintTIME_OPENDIALOG_ID=0;
privateint mHour,mMinute;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button)findViewById(R.id.b1);
b1.setOnClickListener(newOnClickListener()
{
@OverridepublicvoidonClick(View arg0) {
// TODO Auto-generated method stub
showDialog(TIME_OPENDIALOG_ID);
updateDisplay();
}
});
}
private TimePickerDialog.OnTimeSetListenermopenSetListener=newTimePickerDialog.OnTimeSetListener() {
publicvoidonTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
updateDisplay();
}
};
private Object pad(int mMinute2) {
if (mMinute2 >= 10)
return String.valueOf(mMinute2);
elsereturn"0" + String.valueOf(mMinute2);
}
privatevoidupdateDisplay() {
b1.setText(newStringBuilder().append(pad(mHour)).append(":")
.append(pad(mMinute)));
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_OPENDIALOG_ID:
returnnewTimePickerDialog(this, mopenSetListener, mHour, mMinute,
false);
}
returnnull;
}
}
main.xml
<RelativeLayout 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=".MainActivity" >
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp"
android:text="Time" />
</RelativeLayout>
Post a Comment for "Android Java Time Picker Dialog"