Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    }
}
like image 814
Travis Avatar asked Dec 09 '22 09:12

Travis


1 Answers

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);
}
like image 86
Lucifer Avatar answered Dec 15 '22 09:12

Lucifer