I've seen older posts and all the answers suggest to clearFocus.
I can't do that, because my TimePicker isn't inside a dialog, so I don't know when I should call clearFocus() function and probably it will crash my app if I would try to modify TimePicker after.
I'm sure this problem has a solution because the Alarm app which comes with Android do this feature without a dialog.
Any answer is welcome,
Regards!
You should use TimePicker.OnTimeChangedListener to handle onTimeChanged action if the user has changed hours or minutes values by tapping on plus or minus, or by editting the time manualy using keyboard.
And this is my code:
Activity:
public class MainActivity extends Activity
implements TimePicker.OnTimeChangedListener {
private TextView resultTime;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultTime = (TextView) findViewById(R.id.activity_main_textview_resulttime);
TimePicker timePicker = (TimePicker) findViewById(R.id.activity_main_timepicker);
{
timePicker.setIs24HourView(true);
timePicker.setOnTimeChangedListener(this);
}
}
@Override
public void onTimeChanged(TimePicker timePickerView, int hours, int minutes) {
final String stringNewTime = hours + " : " + minutes;
resultTime.setText(stringNewTime);
}
}
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical" >
<TimePicker
android:id="@+id/activity_main_timepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dip"
android:layout_marginRight="16dip"
android:focusable="true"
android:focusableInTouchMode="true" />
<TextView
android:id="@+id/activity_main_textview_resulttime"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Hope, it will help you!