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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:08:15+00:00 2026-06-13T23:08:15+00:00

I am new to android word. I am doing a text view spinner with

  • 0

I am new to android word.

I am doing a text view spinner with time and date picker.

  1. How to set current time and date in the text view by default.
  2. After select time/date and click “set” button, why my time/date selection is empty

here is my activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >

<TextView
    android:id="@+id/startTime"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"                 
    style="@android:style/Widget.Holo.Spinner" 
    android:onClick="showTimePickerDialog"/>
<TextView
    android:id="@+id/endTime"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"                 
    style="@android:style/Widget.Holo.Spinner" 
    android:onClick="showTimePickerDialog"/>
<TextView
    android:id="@+id/date"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"                 
    style="@android:style/Widget.Holo.Spinner" 
    android:onClick="showDatePickerDialog"/>

here is my MainActivity.java

import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void showTimePickerDialog(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getFragmentManager(), "timePicker");
    }

    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
    }

    public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current time as the default values for the picker
            final Calendar c = Calendar.getInstance();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int minute = c.get(Calendar.MINUTE);

            // Create a new instance of TimePickerDialog and return it
            return new TimePickerDialog(getActivity(), this, hour, minute,
                    DateFormat.is24HourFormat(getActivity()));
        }

        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            // Do something with the time chosen by the user
        }
    }

    public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            // Create a new instance of DatePickerDialog and return it
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {
            // Do something with the date chosen by the user
        }
    }
}

Thank you.

  • 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-13T23:08:16+00:00Added an answer on June 13, 2026 at 11:08 pm

    The problem I see is that your DatePickerFragment and TimePickerFragments are static, so they cannot access your activity’s textviews. If they were not static, then all you’ll need to do is timeTextView.setText(...) inside of onTimeSet(...) method. (By the way, keeping them static is good because you can reuse in another activity if you need to.)

    One way to get around this would be to create an extra interface and then pass that to your picker dialog fragments when you create them and then make a callback. But why do that when OnDateSetListener and OnTimeSetListener interfaces already exist?

    I am going to refer only to the DatePickerFragment below, but all the same applies to the TimePickerFragment as well.

    Your MainActivity should implement OnDateSetListener, not your DatePickerFragment. One of the overriden methods from OnDateSetListener is onDateSet(...) where you set the date in your TextView. For example:

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        Calendar cal = new GregorianCalendar(year, monthOfYear, dayOfMonth);
        mDateTextView.setText(DATE_FORMATTER.format(cal.getTime()));
    }
    

    Your DatePickerFragment should have a constructor which takes an OnDateSetListener as a parameter and holds a reference to it. See how to implement your DatePickerFragment here https://gist.github.com/2935353

    Now inside your showDatePickerDialog(View v), pass your MainActivity (which is also an OnDateSetListener) like this:

    FragmentTransaction ft = getFragmentManager().beginTransaction();
    DialogFragment newFragment = new DatePickerDialogFragment(MainActivity.this);
    newFragment.show(ft, "date_dialog");
    

    Then when the time is set, your MainActivity which implements ‘OnDateSetListener’ will get a callback and your textview will be updated.

    Hope this clears things up a bit.

    P.S. You should also have empty constructor in the DatePickerFragment, it will prevent your app from crashing when the device is rotated.

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

Sidebar

Related Questions

I just installed the new Android SDK and the ADT 17. After the Installation
I'm trying to record audio this.recorder = new android.media.MediaRecorder(); this.recorder.setAudioSource(android.media.MediaRecorder.AudioSource.MIC); this.recorder.setOutputFormat(android.media.MediaRecorder.OutputFormat.DEFAULT); this.recorder.setAudioEncoder(android.media.MediaRecorder.AudioEncoder.DEFAULT); this.recorder.setOutputFile(pruebaAudioRecorder.mp4); **this.recorder.prepare();**
I am a new developer for Android. I wanna know about View transitions. For
So I started a new android project in my default workspace. Then I decided
The new Android 2.1 SDK (version 7) has a new class called SignalStrength: http://developer.android.com/reference/android/telephony/SignalStrength.html
when i am creating new android project from visual studio 2010 it gives the
i am creating a new android application.i am using the table layout. I have
I am relatively a new Android developer and I am not able to understand
I am a new android app developer and i want to use google map
I am new android app developer i want make app for tablets and phone

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.