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 example: create a table

20.09.2018 (👁251)


 

This example creates a spreadsheet in Android that will be filled at runtime with weekdays of a month

 

Under Android Studio 3, Java

 

Here with days of the week in a month, after the weekday and description. The weekend is highlighted in color.

 

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.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 {

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

   
public void btnStart_onClick(View view) {
        create_Table();
    }

   
public void create_Table(){
       
//-------< create_Table() >--------
       
TableLayout tbl=findViewById(R.id.table_main);

       
//----< @Loop: Days >----
       
for (int iDay=1;iDay<=31;iDay++) {
           
//----< Day-Row >----
            //-< Date >-
           
Date dtRow= clsHelper.getDate(2018,9,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));
            }

           
//< 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 >----
       
}
       
//-------</ create_Table() >--------
   
}

}

 

 

 

Activity_main.xml

Under res / layout / 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="com.codedocu.test02.MainActivity">

    <
Button
       
android:id="@+id/button"
       
android:layout_width="wrap_content"
       
android:layout_height="50dp"
       
android:layout_marginLeft="2dp"
       
android:layout_marginTop="2dp"
       
android:onClick="btnStart_onClick"
       
android:text="START"
       
app:layout_constraintLeft_toLeftOf="parent"
       
app:layout_constraintTop_toTopOf="parent" />

    <
ScrollView
       
android:id="@+id/scrollView1"
       
android:layout_width="fill_parent"
       
android:layout_height="wrap_content"
       
android:layout_alignParentLeft="true"
       
android:layout_marginTop="50dp"
       
android:background="#3d455b"
        
app:layout_constraintEnd_toEndOf="parent"
       
app:layout_constraintHorizontal_bias="0.5"
       
app:layout_constraintStart_toStartOf="parent"
       
app:layout_constraintTop_toTopOf="parent">

            <
TableLayout
               
android:id="@+id/table_main"
               
android:layout_width="match_parent"
               
android:layout_height="match_parent"
               
android:layout_centerHorizontal="true"
               
android:background="#FFFF"
               
android:padding="10px"
               
>

                <
TableRow android:layout_width="match_parent"
                   
android:background="#000000"
                   
>

                    <
TextView
                       
android:id="@+id/tblMonth"
                        
android:layout_width="match_parent"
                       
android:layout_height="wrap_content"
                       
android:background="@drawable/table_layout"
                       
android:padding="10px"
                       
android:text="day"
                       
android:textColor="#FFFF"
                       
/>

                    <
TextView
                       
android:background="@drawable/table_layout"
                       
android:padding="10px"
                       
android:text=".." />
                </
TableRow>

            </
TableLayout>

    </
ScrollView>
</
android.support.constraint.ConstraintLayout>

 

 

 

Format the cell

Under Android, you can set a style to elements by creating a style file in resource drawable.

The file is called drawable under res / drawable / stylename

 

The style is assigned by simply welding the background.

android:background="@drawable/table_layout"