3

I am getting an extra header for DatePickerDialog as shown in the below snap shot, i don want that, What i am doing wrong here?I Need the DatePickerDialog without that header, please look into the code and let me know what part of code went wrong. Your help highly appreciated.

My Activity:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private CalendarView cal;
private EditText fromDateEtxt;
private DatePickerDialog fromDatePickerDialog;
private SimpleDateFormat dateFormatter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    fromDateEtxt = (EditText) findViewById(R.id.editDig);
    final InputMethodManager imm = (InputMethodManager)getSystemService(
            Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(fromDateEtxt.getWindowToken(), 0);
    cal = (CalendarView) findViewById(R.id.calendarView1);
    cal.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

        @Override
        public void onSelectedDayChange(CalendarView view, int year, int         month,
                                        int dayOfMonth) {
            // TODO Auto-generated method stub

            Toast.makeText(getBaseContext(), "Selected Date is\n\n"
                            + dayOfMonth + " : " + month + " : " + year,
                    Toast.LENGTH_LONG).show();
        }
    });

    dateFormatter = new SimpleDateFormat("dd-MM-yyyy");
    setDateTimeField();

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}



private void setDateTimeField() {
    fromDateEtxt.setOnClickListener(this);

    Calendar newCalendar = Calendar.getInstance();
    fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar newDate = Calendar.getInstance();
            newDate.set(year, monthOfYear, dayOfMonth);
            fromDateEtxt.setText(dateFormatter.format(newDate.getTime()));
        }


    }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

    Log.d("Date", "Current Date " + dateFormatter.format(new Date().getTime()) + "  Month  " + newCalendar.get(Calendar.MONTH));
   int maxYear =  newCalendar.get(Calendar.YEAR) - 18;

 /*   Calendar c2 = Calendar.getInstance();
    c2.set(Calendar.YEAR, maxYear);
    c2.set(Calendar.MONTH, newCalendar.get(Calendar.MONTH));
    c2.set(Calendar.DAY_OF_MONTH, newCalendar.get(Calendar.DAY_OF_MONTH));*/

    newCalendar.set(Calendar.YEAR, maxYear);
    Date d= new Date();
    d.setTime(newCalendar.getTimeInMillis());
    fromDatePickerDialog.getDatePicker().setMaxDate(d.getTime());
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View view) {
    if(view == fromDateEtxt) {
        fromDatePickerDialog.show();
    }

}
}

Snapshot:

enter image description here

Community
  • 1
  • 1
Naveen Shriyan
  • 1,266
  • 1
  • 9
  • 15

2 Answers2

4

The following line of code sets your dialog title value:

fromDatePickerDialog.getDatePicker().setMaxDate(d.getTime());

In order to clear this extra header you need to call setTitle just after that.

fromDatePickerDialog.getDatePicker().setMaxDate(d.getTime());    
fromDatePickerDialog.setTitle(null);

As a result you'll have dialog with max date and no title displayed.

Fiil
  • 1,680
  • 11
  • 11
0

Use anyone of below 2 method, it will help you.

1) Try to set Title Emopty like

     fromDatePickerDialog.setTitle("");

2) Try to set no title for date picker like

 fromDatePickerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Vickyexpert
  • 3,147
  • 5
  • 21
  • 34