Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7681663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:23:34+00:00 2026-05-31T18:23:34+00:00

Hurro! I’m trying my hand at writing my first Android app, and I’ve hit

  • 0

Hurro!
I’m trying my hand at writing my first Android app, and I’ve hit a brick wall.

I want to allow the user to set a ‘start’ and a ‘stop’ time, for a daily schedule. I have sucessfully implemented the TimePickerDialog, and the TextView next to it, but I can’t for the LIFE of me figure out how to save the user selected time to a SharedPreferences string.

Code is as follows (stripped down for clarity’s sake):

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setup);
        // Retrieve the shared preferences
        mUserSettings = getSharedPreferences(USER_PREFERENCES, MODE_PRIVATE);

        // capture our View elements
        mTimeDisplayStart = (TextView)  findViewById(R.id.TextView_ScheduleStart_Info);
        mPickTimeStart = (Button) findViewById(R.id.Button_ScheduleStart);

        // add a click listener to the button
        mPickTimeStart.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(TIME_DIALOG_START);
            }
        });

        // get the current time
        final Calendar c = Calendar.getInstance();
        mHourStart = c.get(Calendar.HOUR_OF_DAY);
        mMinuteStart = c.get(Calendar.MINUTE);

        // display the current date
        updateDisplayStart();
    }

    // updates the time we display in the TextView
    private void updateDisplayStart() {
        mTimeDisplayStart.setText(
                new StringBuilder()
                .append(padStart(mHourStart)).append(":")
                .append(padStart(mMinuteStart)));
    }

    private static String padStart(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }

    // the callback received when the user "sets" the time in the dialog
    private TimePickerDialog.OnTimeSetListener mTimeSetListenerStart =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHourStart = hourOfDay;
                mMinuteStart = minute;
                updateDisplayStart();
            }
        };

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case TIME_DIALOG_START:
            return new TimePickerDialog(this,
                    mTimeSetListenerStart, mHourStart, mMinuteStart, false);
        }
        return null;
    }

I assume that the time should be saved at the same time that the TextView is updated… but I have no idea how to do this -.-

I’ve now spent a good 5 or 6 hours looking for an answer, but I literally can’t find one. I’ve experimented with the SharedPreferences.Editor, but that won’t work because I need to store the time as a long, not a String.
I’m at the end of my rope, and I’d really appreciate some help!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:23:35+00:00Added an answer on May 31, 2026 at 6:23 pm

    You have to do like this

    private TimePickerDialog.OnTimeSetListener mTimeSetListenerStart =
            new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    mHourStart = hourOfDay;
                    mMinuteStart = minute;
                    updateDisplayStart();
    
                    Calendar cal = Calendar.getInstance();
                    cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
                    cal.set(Calendar.MINUTE, minute);
                    mUserSettings.edit().putLong("starttime", cal.getTimeInMillis()).commit();
    
                }
            };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a comma separated integers and I want to store them in std::vector<int>
How do I redefine a class method in ruby? say, for example, I want
I'm running a query in php/mysql that joins several tables and I'm trying to
I want to get the UCS-2 code points for a given UTF-8 string. For
I'm trying use the modulo operator (%) on long longs, and it seems to
I have a project which has a native android OpenGL implementation. MyView extends GLSurfaceView
Does someone used tinymce.dom.Selection class in tinyMCE? I'm unsuccessfully trying to apply a function
I'm developing an app and so far all cancel buttons have been on the
I know it's not a programming question but I'm in a hurry to choose
I usually type too much, so read bold copy if in a hurry .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.