1

How do I display a Date and Time without java or a Picker, Like as simple as TimeView

 android:@+id/ETC
 android:TextSize="" and so on. 

and I'll get the time or date from the device. Time/Date pickers take up to much display room.

user229044
  • 232,980
  • 40
  • 330
  • 338
len372
  • 11
  • 2

1 Answers1

1

There is no built-in widget for that, but you can add one by extending TextView:

package com.example.widget;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

public class TimeView extends TextView {
  public TimeView(Context context) {
    super(context);
    updateTime();
  }

  public TimeView(Context context, AttributeSet attrs) {
    super(context, attrs);
    updateTime();
  }

  public TimeView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    updateTime();
  }

  public void updateTime() {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String now = format.format(new Date());
    setText(now);
  }
}

You can then use it in your xml files, customizing it with the standrad TextView parameters:

<?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:orientation="vertical" >
  <com.example.widget.TimeView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#0f0"
    android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
chiuki
  • 14,580
  • 4
  • 40
  • 38