0

How do I get an edittext field to display the date and another to display the time automatically on open?

I have the edittexts coded as follows:

XML Code:

<!-- Creating autocomplete text field for date -->
<EditText
    android:id="@+id/autoD8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/autoTime"
    android:layout_alignLeft="@+id/autoTime"
    android:ems="10"
    android:inputType="textAutoComplete" />

    <!-- Creating autocomplete text field for time -->
   <EditText
    android:id="@+id/autoTime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/txtTime"
    android:layout_alignLeft="@+id/inputPlace"
    android:ems="10"
    android:inputType="textAutoComplete" />

All I have in my java code section at the moment is as follows:

EditText autoD8, autoTime;
Roman C
  • 49,761
  • 33
  • 66
  • 176
Ashley Keating
  • 119
  • 1
  • 5
  • 12
  • 1
    I suggest going through the Android tutorial. Maybe I don't understand the question properly, but it seems like you'd be able to do this just by getting the date/time in your Activity's onCreate and formatting it. You can find how to format the date here: http://stackoverflow.com/questions/454315/how-do-you-format-date-and-time-in-android – Richard Fung Feb 20 '14 at 19:10
  • Please, show as the code of your Activity. What you are asking is hard (if not impossible) to do in XML. – Philipp Jahoda Feb 20 '14 at 19:11
  • If I changed it to a textview would it be much easier? – Ashley Keating Feb 20 '14 at 19:18

3 Answers3

2

That should be something like that in your onCreate() :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);

    EditText autoD8 = (EditText)findViewById(R.id.autoD8);
    EditText autoTime = (EditText)findViewById(R.id.autoTime);

    SimpleDateFormat dateF = new SimpleDateFormat("EEE, d MMM yyyy", Locale.getDefault());
    SimpleDateFormat timeF = new SimpleDateFormat("HH:mm", Locale.getDefault());
    String date = dateF.format(Calendar.getInstance().getTime());
    String time = timeF.format(Calendar.getInstance().getTime());

    autoD8.setText(time);
    autoTime.setText(time);
}

More formats here

Smile2Life
  • 1,921
  • 3
  • 16
  • 30
0

One way is override your Activity/Fragment's onStart()/onCreate() and set time yourself. I didn't see any attribute which can auto fill date/time in http://developer.android.com/reference/android/R.styleable.html#EditText

Prakash
  • 4,479
  • 30
  • 42
0

Have a look at http://developer.android.com/reference/android/text/format/Time.html Just get the date and time and set values in EditTexts

Martin
  • 2,146
  • 4
  • 21
  • 32