0

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!

user1205781
  • 40
  • 1
  • 9

1 Answers1

2

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!

JunR
  • 202
  • 1
  • 6
  • Thanks a lot, but it doesn't work for me. OntimeChanged doesn't register when I modify the time using the inputText. – user1205781 Sep 10 '12 at 22:01
  • You mean when the cursor focused on the hours or minutes field, and you are typing numbers, then it doesn't handle changes? Know, ones you typed something e.g. - minutes, then focus hours it will handle changes! – JunR Sep 10 '12 at 22:10
  • Well, I'e just realised that when I focus the cursor on the hours field and I edit something, the onTimeChanged function is called. However, when I do the same on the second NumberPicker (minutesField), nothing occurs. – user1205781 Sep 10 '12 at 22:27
  • Sorry, but why don't you use timePicker instead of NumberPicker?? NumberPicker is supported only after 11th api level! Anyway, it works when editting the values, as we see. But I'll think about how can we handle event on unfocus the timepicker. – JunR Sep 10 '12 at 22:35
  • There has been a confusion. I've said NumberPicker because TimePicker is implemented using two NumberPickers. Sorry. – user1205781 Sep 10 '12 at 22:49