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 Beispiel: eine Tabelle erstellen

20.09.2018 (👁248)


Dieses Beispiel erstellt in Android eine Tabelle, welche zur Laufzeit gefüllt wird mit Wochentagen eines Monats

 

Unter Android Studio 3, Java

 

Hier mit Wochentagen in einem Monat, nach laufendem Wochentag und Bezeichnung. Dabei ist das Wochenende farblich hervorgehoben.

 

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

Unter 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>

 

 

 

Zelle formatieren

Unter Android kann man eine Style auf Elemente legen, indem man bei resourcen drawable ein style-Datei erstellt.

Die Datei heißt dann drawable unter res/drawable/stylename

 

Der Style wird zugeordnet, indem man den Background einfach zuweißt.

android:background="@drawable/table_layout"