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 6824247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:52:44+00:00 2026-05-26T21:52:44+00:00

This little thing started to frustrate me a lot. I thought it was something

  • 0

This little thing started to frustrate me a lot. I thought it was something simle to do, but I think I haven’t understood clearly how android works.

Anyway, the problem is that my android application will have some buttons that will open a date or time picker dialog box when pressed. I know how to implement them directly in the same class as the main activity itself, but I don’t think that’s good solution for maintenance reasons and also if I’m going to develop that application further some day.

I tried to do this by several different ways and failed every time I tried to do it. That left me pretty clueless of what to do and what’s the problem.

Here’s one way I tried to do this:

the main activity (I simplified it and tried to provide only the necessary code, because it’s long). This is following the singleton design pattern.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    dateButton = (Button) findViewById(R.id.editDateButton);

    dateButton.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    if(view.getId() == R.id.editStartDateButton) {
        DatePickers.getInstance().setDate(dateButton);
    }
}

The date pickers class (extends Activity; also simplified code and all the unnecessary things removed):

private static DatePickers self = null;

public static DatePickers getInstance() {
    if (null == self) {
        self = new DatePickers();
    }
    return self;
}

public void setDate(Button button) {
    theButtonUsed = button;
    showDialog(DATE_DIALOG_ID);
}

After the showDialog call the class is similar to the example Hello-DatePicker in android developer site.

Also I tried to do it pretty much like in here, but with the differnece that when the dateButton is clicked it starts the new activity that’s the date picker dialog. In this case the datePicker class was like this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.datePicker);

    showDialog(DATE_DIALOG_ID);
}

And the rest as it is in the Hello-DatePicker example in android dev site. Both these solutions lead to a problem where my application crashed and was forced to quit.

I hope you understand what’s the problem and that you could guide me and show what I’m doing wrong. I tried to make this short and provide only the necessary information.

Thanks
-Z

Edit:

As I have mentioned in one of the comments, the real problem with this was that I has simply forgotten to add the new activity to the manifest file. There might have been some other problems that was solved with the accepted answer. I hope this is still helpful to anyone facing similar issues.

  • 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-26T21:52:45+00:00Added an answer on May 26, 2026 at 9:52 pm

    I think I can help you out there.

    I know my solution isn’t going to follow your singleton approach but it definitely separates DatePicker code from the calling Activity or Activities – which in return becomes modular and clean.

    So here it goes:
    Below is the designated DatePicker code (I named it DateSelector). You start this activityForResult and it will return a bundle with Day, Month, and Year to the calling Activity.

    I’m also going to pasting the simple layout DateSelector uses right below it; it’s just a transparent layout…

    package com.cyberfabric.historicise.activities;
    
    import java.util.Calendar;
    
    import com.cyberfabric.historicise.R;
    import android.app.Activity;
    import android.app.DatePickerDialog;
    import android.app.Dialog;
    import android.content.Intent;
    import android.os.Bundle;
    
    import android.view.Window;
    
    
    public class DateSelector extends Activity{
    
        private final static int DIALOG_DATE_PICKER = 0;
    
        private int setYear;
        private int setMonth;
        private int setDay;
    
    
        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.date_picker);
    
            Calendar today = Calendar.getInstance();
            setYear = today.get(Calendar.YEAR);
            setMonth = today.get(Calendar.MONDAY);
            setDay = today.get(Calendar.DAY_OF_MONTH);
    
            showDialog(DIALOG_DATE_PICKER); 
        }
    
        @Override
        protected Dialog onCreateDialog(int id){
            switch(id){
                case DIALOG_DATE_PICKER:
                    return new DatePickerDialog(this, mDateSetListener, setYear, setMonth, setDay);
                }
            return null;
        }
    
        private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener(){
            @Override
            public void onDateSet(android.widget.DatePicker view, int year, int month, int day) {
                setYear = year;
                setMonth = month;
                setDay = day;
                returnDate();
            }
        };
    
    
        /*
         * Package up the data and return it back to the calling intent
         */
        private void returnDate(){
            Intent intent = getIntent();    // calling/parent intent //
    
            Bundle bundle = new Bundle();
            bundle.putInt("year", setYear);
            bundle.putInt("month", setMonth);
            bundle.putInt("day", setDay);
            intent.putExtra("set_date", bundle);
    
            setResult(Activity.RESULT_OK, intent);
    
            finish();
        }
    }  
    

    Here is the layout DateSelector uses:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/transparent"
        />
    

    Your calling Activity or Activities should launch DateSelector as such:

    // some global variable declared in order to start activityForResult and to catch 
    // it back on onActivityResult
    private final static int REQUEST_GET_DATE = 3;
    

    Starting the DateSelector from the calling Activity:

    Intent dp = new Intent(EventForm.this, DateSelector.class);
    EventForm.this.startActivityForResult(dp, REQUEST_GET_DATE);
    

    And finally in your calling Activity just catch it onActivityResult:

    protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
        super.onActivityResult(requestCode, resultCode, data); 
    
        switch(requestCode){ 
            case REQUEST_GET_DATE:
                if(resultCode == Activity.RESULT_OK){
                    Bundle setDate = data.getBundleExtra("set_date");
                    int setDay = setDate.getInt("day");
                    int setMonth = setDate.getInt("month");
                    int setYear = setDate.getInt("year");
                    System.out.println(setDay + " " + setMonth + " " + setYear);
                }
                break;
        }
    }
    

    I hope this helps, hope you like it,

    Best,

    -serkan

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm going a little mad I think. This is the VERY first thing I
I think I am overthinking this issue a little and I'm hoping someone can
Just a quick little thing, but i can't get past it. So, say you
I realise this might be a little vague but I honestly don't know where
Hi I'm studying Python and I've started my first little project. The first thing
This is just one of those little things I did all the time in
This little bit of syntax has been a bit of a confusion for me
Given this little piece. http://jsfiddle.net/4gb6K/7/ I am attempting to align the bot element at
Given this little fiddle: http://jsfiddle.net/5Sw3h/8/ I have a navigation panel to the left, a
I have this little rake task: namespace :db do namespace :test do task :reset

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.