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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:57:01+00:00 2026-06-03T14:57:01+00:00

I crated a datetime component but it’s constructed automatically (I have it in a

  • 0

I crated a datetime component but it’s constructed automatically (I have it in a XML layout and I don’t want to create it manually) but I need to pass a reference to an Activity in order to create dialogs. How can I achieve that? I tried a setter after findViewById but it’s not a good solution…

public class DateTimeComponent extends RelativeLayout {

    private Activity activity;

    public DateComponent(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        // rest ommited
        initFields();
    }

    private void initFields() {
        dateEditText = (EditText) findViewById(R.id.dateEditText);
        dateEditText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.showDialog(DATE_PICKER_DIALOG);
            }
        });

        timeEditText = (EditText) findViewById(R.id.timeEditText);
        timeEditText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.showDialog(TIME_PICKER_DIALOG);
            }
        });
    }

    // rest ommited

    public Dialog getDatePickerDialog() {
        int year = selectedDateTime.get(YEAR);
        int month = selectedDateTime.get(MONTH);
        int day = selectedDateTime.get(DAY_OF_MONTH);
        return new DatePickerDialog(activity, onDateSetListener, year, month, day);
    }

    public Dialog getTimePickerDialog() {
        int hour = selectedDateTime.get(HOUR_OF_DAY);
        int minute = selectedDateTime.get(MINUTE);
        return new TimePickerDialog(activity, onTimeSetListener, hour, minute, true);
    }

    private final OnDateSetListener onDateSetListener = new OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            // do something
        }
    };

    private final OnTimeSetListener onTimeSetListener = new OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            // do something
        }
    };

}
  • 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-06-03T14:57:02+00:00Added an answer on June 3, 2026 at 2:57 pm

    Perhaps this may help you:

    Option 1:

    public class DateTimeComponent extends RelativeLayout {
        private Activity activity;
    
        public DateTimeComponent(Activity act){
            activity = act;
        }
    
        public void someListener() {
            activity.showDialog(...);
        }
    
    }
    

    Option 2:

    public class DateTimeComponent extends RelativeLayout {
    
        public void someListener(Activity act) {
            act.showDialog(...);
        }
    
    }
    

    Option 3:

    ...
        private Activity activity;
    
        public DateComponent(Context context, AttributeSet attributeSet) {
            super(context, attributeSet);
    
            activity = (Activity) getContext();
    
            // rest ommited
            initFields();
        }
    
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a procedure with this structure but it doesn't work for datetime
I have a 'created_date' DATETIME field which I would like to be populated with
I have a model with a datetime field: class MyModel(models.Model): created = models.DateTimeField(auto_now =
Currently I'm writing a unit test for a component that does datetime specific validation.
In C# I need to compare the value of DateTime.Today /6pm, to a field
I'm using Rails 3 to create a project that will need a model called
I have a table with columns: id and created(datetime) id created 6 2011-11-04 20:32:09.673
setup: create table main(id integer unsigned); create table test(id integer unsigned,created datetime,text text); insert
I want to create a table, which name something like : name_thisyear_thisweekofyear, e.g ->
Using Entity Framework CodeFirst, how do I create a created datetime column that gets

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.