0

I want to make a calendar application for my project, and I want to show the current date with the title "Month" and subtitle "Day and Year".

I have extended the action bar height and hide the action bar title, but how can I add the current date to the starting point of the action bar?

Zoe
  • 27,060
  • 21
  • 118
  • 148
iamnaran
  • 1,894
  • 2
  • 15
  • 24

4 Answers4

0

Adding TextView to ActionBar:

Dynamic TextView on ActionBar

Todays date in dd/MM/yyyy format:

Calendar c = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String date = dateFormat.format(c.getDate());

//if you want dd/MM/yyyy as separate units
String[] separatedDate = date.split("/");
Community
  • 1
  • 1
BiGGZ
  • 503
  • 4
  • 17
0

Add a TextView manually to the Action Bar:

TextView tv = new TextView(this);
        tv.setText(<Replace with DateTime Related Variable here>);
        tv.setTextColor(getResources().getColor(R.color.WHITE));
        tv.setOnClickListener(this);
        tv.setPadding(5, 0, 5, 0);
        tv.setTypeface(null, Typeface.BOLD);
        tv.setTextSize(14);
        menu.add(0, FILTER_ID, 1, R.string.matchmacking).setActionView(tv).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

And you're good to go.

This was adapted from Yashdeep Patel

Community
  • 1
  • 1
Nicholas
  • 1,883
  • 21
  • 39
0

get current date

Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"),Locale.getDefault());
            SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
            String currentDate = df.format(c.getTime());

and,you can set toolbar title or actionbar title

toolbar.setTitle(currentDate);

or

getActionBar().setTitle(currentDate);
0

in the layout folder create file called clock.xml with following contents;

<TextClock xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/textClock"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:format24Hour="dd/MM/yyyy   HH:mm:ss"
    android:gravity="center_horizontal"
    android:textAppearance="@style/TextAppearance.AppCompat.Small"
    android:textColor="@color/colorWhite"
    android:textSize="50sp"
    android:textStyle="bold"
    android:visibility="visible"
    tools:visibility="visible"/> 

In the Activity add the following in the onCreate;

Objects.requireNonNull(getSupportActionBar()).setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.clock);  
Zoe
  • 27,060
  • 21
  • 118
  • 148
Jro
  • 466
  • 2
  • 6
  • 14