Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Android Code: Dynamic Year -Calendar, School-Calendar Code

24.09.2018 (👁207)

Dynamic calendar

 

 

Android java code to create a dynamic calendar sheet.

 

features:

·       Each month is organized in columns like the school calendars

·       The calendar month can be scrolled up and down.

·       The table header month stops here

·       The calendar sheet can be left-right after months.

 

 

 

Page Code

Res / layout / activity_main.xml

code

<?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="com.codedocu.test02.MainActivity">


<
HorizontalScrollView
   
android:layout_width="match_parent"
   
android:layout_height="match_parent">
<
LinearLayout
   
android:id="@+id/stack_Horizontal"
   
android:layout_width="wrap_content"
   
android:layout_height="match_parent"
   
android:orientation="horizontal"
   
>

   
</
LinearLayout>
</
HorizontalScrollView>
</
android.support.constraint.ConstraintLayout>

 

 

 

 

Code-behind file

App / java / project / clsHelper

 

MainActivity Java

package com.codedocu.test02;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class MainActivity extends AppCompatActivity {

   
public Date dtToday;
   
public Calendar today;

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

        init();
        create_Month_Tables();
    }

   
public void init() {
       
//-------< init() >--------
        //< current Date >
       
today = Calendar.getInstance();
        
today.set(Calendar.HOUR_OF_DAY, 0);
       
today.set(Calendar.MINUTE, 0);
       
today.set(Calendar.SECOND, 0);
       
today.set(Calendar.MILLISECOND, 0);
        Date dtToday =
today.getTime();
       
//</ current Date >
        //-------< init() >--------
   
}

   
public void create_Month_Tables() {
       
//-------< create_Month_Tables() >--------
       
Calendar cal_Month = clsHelper.getCalendar_Month_Current();
        cal_Month.add(Calendar.
MONTH, -3);

       
//--< @Loop: Months >--
        
for (int iMonth = 0; iMonth <= 13; iMonth++) {

            cal_Month.add(Calendar.
MONTH, 1);

           
int cYear = 0;
            cYear = cal_Month.get(Calendar.
YEAR);
           
int cMonth = 0;
            cMonth = cal_Month.get(Calendar.
MONTH);

           
//< add Month >
           
create_Table(cYear, cMonth);
           
//</ add Month >
       
}
       
//--</ @Loop: Months >--
        //-------</ create_Month_Tables() >--------
   
}


   
public void create_Table(int Calendar_Year, int Calendar_Month) {
       
//-------< create_Table() >------
       
LinearLayout stack_Horizontal = findViewById(R.id.stack_Horizontal);

        LinearLayout stack_Month_Header_Detail=
new LinearLayout(this);
        stack_Month_Header_Detail.setOrientation(LinearLayout.
VERTICAL );


       
//scroller_Vertical.setMinimumWidth(500);
       
TableLayout tblHeader = new TableLayout(this);
        tblHeader.setMinimumWidth(
300);

       
//< Header >
       
TableRow rowHeader = new TableRow(this);
        rowHeader.setBackgroundColor(Color.
BLACK);

        TextView spacer =
new TextView(this);
        rowHeader.addView(spacer);

        TextView lblHeaderMonth =
new TextView(this);
        lblHeaderMonth.setText(Calendar_Year +
"-" + Calendar_Month);    //*cast integer to string
       
lblHeaderMonth.setTextColor(Color.WHITE);
        rowHeader.addView(lblHeaderMonth);
        tblHeader.addView(rowHeader);
        stack_Month_Header_Detail.addView(tblHeader);


        TableLayout tbl =
new TableLayout(this);
        tbl.setMinimumWidth(
300);
       
//</ Header >


        //----< @Loop: Days >----
       
for (int iDay = 1; iDay <= 31; iDay++) {
           
//----< Day-Row >----
            //-< Date >-
           
Date dtRow = clsHelper.getDate(Calendar_Year, Calendar_Month, iDay);

           
//< weekday >
            //*as string
           
SimpleDateFormat fmtWeekday = new SimpleDateFormat("EE");
            String weekday = fmtWeekday.format(dtRow);
           
//</ weekday >

            //*as weekday_number
           
Calendar calendarDay = Calendar.getInstance();
            calendarDay.setTime(dtRow);
           
int NrWeekday = calendarDay.get(Calendar.DAY_OF_WEEK);//  fmtWeekdayNr.format(dtRow);
            //-</ Date >-

            //--< Row >--
           
TableRow row = new TableRow(this);
           
if (NrWeekday == Calendar.SATURDAY || NrWeekday == Calendar.SUNDAY) {
                row.setBackgroundColor(Color.rgb(
200, 200, 200));
            }

           
if (dtRow.equals(dtToday)) {
                row.setBackgroundColor(Color.rgb(
200, 0, 0));
            }

           
//< dayNr >
           
TextView lblDayNr = new TextView(this);
            lblDayNr.setText(String.valueOf(iDay));   
//*cast integer to string
           
row.addView(lblDayNr);
           
//</ dayNr >

            //--< weekday >--
           
TextView lblWeekday = new TextView(this);
            lblWeekday.setText(weekday);
            row.addView(lblWeekday);
           
//--</ weekday >--

            //--</ Row >--

            //< add to table >
           
tbl.addView(row);
           
//</ add to table >
            //----</ Day-Row >----
       
}

       
//< add MonthDetails to placeholder >
       
ScrollView scroll_Month_UpDown = new ScrollView(this);
        scroll_Month_UpDown.addView(tbl);
        stack_Month_Header_Detail.addView(scroll_Month_UpDown);
       
//</ add MonthDetails to placeholder >

       
stack_Horizontal.addView(stack_Month_Header_Detail);
       
//-------</ create_Table() >--------
   
}

}

 

 

 

Auxiliary files

clsHelper

A method for creating a calendar date

package com.codedocu.test02;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by Raimund on 20.09.2018.
 */

public class clsHelper {
   
public static Date getDate(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        month=month-
1 ;//*Months are indexed on 0
       
cal.set(year,month,day);

        cal.set(Calendar.
HOUR_OF_DAY, 0);
        cal.set(Calendar.
MINUTE, 0);
        cal.set(Calendar.
SECOND, 0);
        cal.set(Calendar.
MILLISECOND, 0);
       
return cal.getTime();
    }

   
public static Calendar getCalendar_Month_Current() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.
DAY_OF_MONTH, 1);
        cal.set(Calendar.
HOUR_OF_DAY, 0);
        cal.set(Calendar.
MINUTE, 0);
        cal.set(Calendar.
SECOND, 0);
        cal.set(Calendar.
MILLISECOND, 0);
       
return cal;
    }

   
public static Calendar getCalendar_Month(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(year,month,
1);
        cal.set(Calendar.
HOUR_OF_DAY, 0);
        cal.set(Calendar.
MINUTE, 0);
        cal.set(Calendar.
SECOND, 0);
        cal.set(Calendar.
MILLISECOND, 0);
       
return cal;
    }
}