Travis
Travis

Reputation: 135

Why is the date not showing up when I launch android?

This is what I all have so far. I researched the command in the Date_Class.java and it said that was supposed to show the current date. But when I launch the android application, nothing shows up. Why is this? Do I have the wrong code? What did I do wrong?

Any help with this would be greatly appreciated. Thank you.

activity_main.xml

<?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" >

<!-- Header -->

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:textColor="#FFFFFF"
    android:gravity="center"
    android:layout_marginTop="20dp"
    android:id="@+id/Header"
    android:text=""
/>

<!-- Name -->

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:id="@+id/Name"
    android:text="" 
/>

<!-- Project Name -->

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:id="@+id/ProjectName"
    android:text=""    
/>

<!-- Digital Clock -->

<DigitalClock
    android:id="@+id/DigitalClock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:text="" 
/>

<!-- Calendar -->

<TextView
    android:id="@+id/stringCalendar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:text="" 
/>

<!-- End of Program -->

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF"
    android:background="#000000"
    android:gravity="center"
    android:id="@+id/EndOfProgram"
    android:text=""
/>

</LinearLayout>

MainActivity.java

package com.example.d1project1;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.widget.DigitalClock;

import android.widget.TextView;

@SuppressWarnings({ "deprecation", "unused" })
public class MainActivity extends Activity {
    //Declare GUI objects

    TextView stringHeader;
    TextView stringName;        
    TextView stringProject;
    TextView digitalClock1;
    TextView stringEnd;

    TextView stringCalendar;
    TextView stringDisplay;
    DigitalClock DigitalClock;

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

        Init_GUI();
        DigitalClock();
        Nonevent_Event();
    }

    private void DigitalClock() {
        //DigitalClock
        DigitalClock = (DigitalClock)findViewById(R.id.DigitalClock);
    }
    private void Nonevent_Event() {
        String stringDate = "";

        Date_Class.date_time(stringCalendar);

        stringCalendar.setText(stringDate);
    }

    //method to link GUI items to code 
    private void Init_GUI() {
        stringHeader = (TextView)findViewById(R.id.Header);
        stringName = (TextView)findViewById(R.id.Name);
        stringProject = (TextView)findViewById(R.id.ProjectName);
        stringEnd = (TextView)findViewById(R.id.EndOfProgram);

        stringCalendar = (TextView)findViewById(R.id.stringCalendar);

        stringHeader.setText("Day 1 Project 1 - Travis Finlan");
        stringName.setText("Travis Finlan");
        stringProject.setText("Day 1 Project 1");
        stringEnd.setText("End of Program");
    }
}

Date_Class.java

package com.example.d1project1;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.widget.TextView;

public class Date_Class {
    public static String date_time(TextView stringCalendar) {
        //Calendar
        Calendar c = Calendar.getInstance();
        System.out.println("Current time => " + c.getTime());

        SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
        String formattedDate = df.format(c.getTime());

        return formattedDate;
    }
}

Upvotes: 4

Views: 232

Answers (5)

Vinoth
Vinoth

Reputation: 81

try this.

Calendar c = Calendar.getInstance(); 
int seconds = c.get(Calendar.SECOND);

Sometimes running in emulator also does not return the current date and time.

Upvotes: 0

Jitesh Upadhyay
Jitesh Upadhyay

Reputation: 5260

please use the following code

private void Nonevent_Event() 
{
        String stringDate = "";
       //please have a look on these lines and use it
        stringDate = Date_Class.date_time(stringCalendar);
       //now do the operation
        stringCalendar.setText(stringDate);
}

Upvotes: 2

M D
M D

Reputation: 47817

You just do like

private void Nonevent_Event() {
    String stringDate=Date_Class.date_time(stringCalendar);
    stringCalendar.setText(stringDate);
}

You defined stringDate but forget to returned value Nonevent_Event() to store on it.

Upvotes: 3

Code-Apprentice
Code-Apprentice

Reputation: 83557

String stringDate = "";
Date_Class.date_time(stringCalendar);

This calls your method, but ignores the returned value. You need to do

String stringDate = Date_Class.date_time(stringCalendar);

Upvotes: 2

Lucifer
Lucifer

Reputation: 29672

You need to modify your following code,

private void Nonevent_Event() 
{
        String stringDate = "";
        Date_Class.date_time(stringCalendar);
        stringCalendar.setText(stringDate);             // here you forgot to assign the string value
}

to below code,

private void Nonevent_Event() 
{
        String stringDate = "";
        stringDate = Date_Class.date_time(stringCalendar);
        stringCalendar.setText(stringDate);
}

Upvotes: 7

Related Questions