• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

How to show the Date in Android Studio App

Does anyone know how to display a variable in Android Studio? I want the simplest code so I have an actual working example. This is ridiculous that it is this complex to display the value of a variable. I have a completely blank and new project. I just want to be able to display the value of a variable. I've researched enough to find that the variable should be defined in MainActivity.java but have found zero working examples that then allow the value to display on the screen. I've tried using TextView and findById but since they don't work I now consider them to be evil frauds. My eventual goal is to be able to display the date, but I can't even display a simple variable. Please show me the way for I am lost. Insert profanity here.
 
Last edited:
Well, after much profanity, I've cobbled this together from a few places and then had to guess my way through the rest. This code works. If you have red labels after pasting this in be sure to click on them, press ALT-Enter, and then choose import class (java.util). I've included the entire text of the two files, so if you create a new blank project (empty activity), be sure not to duplicate the default lines at the top of each file.

Now to figure out how to replace that string with date information. That should be some simple straightforward programming instead of having to divine the magic password to make simple variable display work. BTW the string abc is the important part down there.

Good Luck!


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.computerman.showvariable.MainActivity">

<TextView

android:id="@+id/textview1"
android:layout_column="0"
android:layout_row="0"
android:text= ""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"



/>

</android.support.constraint.ConstraintLayout>


_________________________________________________________________________________

MainActivity.java

package info.computerman.showvariable;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String abc ="Alphabet";
TextView textView1;
//in your OnCreate() method
textView1 = (TextView)findViewById(R.id.textview1);
textView1.setText(abc);

}


}
 
Ok, here are the minor changes to MainActivity.java that allowed me to display the date:

package info.computerman.showvariable;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

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


public class MainActivity extends AppCompatActivity {

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

String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());

String abc ="Alphabet";
TextView textView1;
//in your OnCreate() method
textView1 = (TextView)findViewById(R.id.textview1);
textView1.setText(date);

}

}

I left the now unused abc string just for comparison to the earlier code above.
The last line shows the date variable instead of the abc variable.
The line starting "String date" was added.
 
Back
Top Bottom