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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:06:26+00:00 2026-06-12T06:06:26+00:00

I am trying to make the following projekt: Start Activity has 4 buttons. 1.

  • 0

I am trying to make the following projekt:
Start Activity has 4 buttons.
1. First Button just opens an Information Dialog (onClick0)
2. Second Button opens a DatePickerDialog and allows me to Set a Date. (onClick1)
3. Third Button opens a Dialog that shows me the date i entered (onClick2).
4. Fourth Button just opens an About Dialog. (onClick3)

The problem is that after the first Date set, the Dialog that i open pressing the Third Button show me always the First Date set i did.

What am i doing wrong?
Please help me 🙂

MainActivity code:

package boy.girl;

import java.text.DateFormat;
import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.widget.DatePicker;
import android.widget.Toast;
import android.content.DialogInterface;
import android.app.DatePickerDialog;
import android.app.AlertDialog;

public class MainActivity extends Activity {

DateFormat fmtDateAndTime=DateFormat.getDateTimeInstance();
Calendar dateAndTime=Calendar.getInstance();

int mYear,mMonth,mDay;

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

public void onClick0(View v) 
{ 
    showDialog(0); 
}

public void onClick1(View v)
{ 
     new DatePickerDialog(this, d,
             dateAndTime.get(Calendar.YEAR),
             dateAndTime.get(Calendar.MONTH),
             dateAndTime.get(Calendar.DAY_OF_MONTH))
     .show();
}

DatePickerDialog.OnDateSetListener d=new DatePickerDialog.OnDateSetListener() 
{
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {

        Toast.makeText(getBaseContext(), "Set", Toast.LENGTH_SHORT).show();

        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        dateAndTime.set(Calendar.YEAR, year);
        dateAndTime.set(Calendar.MONTH, monthOfYear);
        dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);

        Toast.makeText(getBaseContext(), 
Integer.toString(mDay)+" "+Integer.toString(mMonth)+" "+Integer.toString(mYear),
Toast.LENGTH_SHORT).show();
    }   
};

public void onClick2(View v) 
{ 
    showDialog(2); 
}

public void onClick3(View v) { showDialog(3); }


@Override
protected Dialog onCreateDialog(int id)
{
    switch(id)
    {
    case 0:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.infobox);
        builder.setTitle("Title Information");
        builder.setMessage("Information text...");
        builder.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog,    int which) {
        Toast.makeText(getBaseContext(), "Ok clicked", Toast.LENGTH_SHORT)
                        .show();
                    }
                });
    return builder.create();

            case 2: 

        return new AlertDialog.Builder(this)
    .setTitle("Results")
    .setMessage(Integer.toString(mDay)+" "+Integer.toString(mMonth)+
" "+Integer.toString(mYear))
        .setPositiveButton("OK",
        new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(getBaseContext(), "Ok clicked", Toast.LENGTH_SHORT)
                .show();
            }
        }
    ).create();

    case 3: 
        return new AlertDialog.Builder(this)
        .setTitle("About")
        .setMessage("About this company and App version")
        .setPositiveButton("OK",
                new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(getBaseContext(), "Ok clicked", Toast.LENGTH_SHORT)
                .show();
            }
        }
    ).create();
    }
    return null;
}
}
  • 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-12T06:06:28+00:00Added an answer on June 12, 2026 at 6:06 am

    onCreateDialog is called only once. Therefore it is always showing the data from the first time you opened it.

    You need to update the message in onPrepareDialog. That should get it working for you.

    PS: onCreateDialog and onPrepareDialog are deprecated in ICS, and also a little buggy in ICS. Try using the new APIs if possible.

    Edit:

    Keep your code as it is, just add the following –

     @Override
      protected void onPrepareDialog(int dialogId, Dialog dialog, Bundle args) {
        if(dialogId==2){
          ((AlertDialog)dialog).setMessage(Integer.toString(mDay)+" "+Integer.toString(mMonth)+
            " "+Integer.toString(mYear));
        }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a total beginner with OpenCL and I'm trying to make the following kernel
I trying to make a schema for the following xml. <root> <allow_any_name id=string> <required_tag_1>string</required_tag_1>
I am trying to make a makefile which does the following: gets source files
I'm trying to make a draggable popup with Jquery and I'm following the advice
I am trying to make a chatroom layout like the following: Now my problem
I am trying to make a very simple web-service which does the following: The
i'm trying to make a php tcp/ip server with the following code: <?php //
I'm trying to make a border fade in around an element. Following the answer
I'm trying to make a graph with highcharts from a csv file. Following is
I am trying to compile my gcc code using the following make command .

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.