PeakGen
PeakGen

Reputation: 23025

Displaying text whenever the date is changed from DatePicker

Please have a look at the following code

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical"
    >

    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnText"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/heading"
        />

    <TextView 
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <DatePicker
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>

MainActivity.java

package com.example.datepicker;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog.OnDateSetListener;
import android.view.Menu;
import android.widget.DatePicker;
import android.widget.Toast;

public class MainActivity extends Activity {

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

        DatePicker dp = (DatePicker)findViewById(R.id.date);
    }

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

    private class DatePickerMe implements OnDateSetListener
    {

        public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) 
        {
            Toast.makeText(MainActivity.this, "Click", Toast.LENGTH_LONG);

        }

    }
}

Here, I want to display the "Toast" message whenever the date is changed. I have attempted it under "DatePickerMe" inner class. But however, I don't know how to register this "OnDateSetListener" to date picker (I assumed onDateSetListener is the correct interface). Please help me to display the text, whenever the date is changed.

Thanks

Upvotes: 0

Views: 308

Answers (2)

Sam
Sam

Reputation: 86948

I noticed a few things:

  • You need to use DatePicker#init() to set your OnDateChangedListener.
  • Also you must call show() on your Toast, otherwise it will never display.
  • Last you don't need to extend the OnDateChangeListener class, you can just use it like below.

Try this:

OnDateChangedListener listener = new OnDateChangedListener() {
    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        // Show your Toast by calling show()!
        Toast.makeText(MainActivity.this, "Click", Toast.LENGTH_LONG).show();
    }
};
DatePicker dp = (DatePicker)findViewById(R.id.date);

// Use Calendar to set the date to now
Calendar calendar = Calendar.getInstance();
dp.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), listener);

Upvotes: 1

Khantahr
Khantahr

Reputation: 8528

You register the listener when you call the init method.

dp.init( year, month, day, new DatePickerMe() );

Upvotes: 1

Related Questions