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

Eine Tabelle mit Android zur Laufzeit erstellen

19.09.2018 (👁187)

Eine Tabelle mit Android Studio erstellen

die App soll beim Klick auf einen Button neue Zeilen in eine leere Tabelle anfügen

 

 

 

 

activity_main

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="49dp"
       
android:layout_marginLeft="16dp"
       
android:layout_marginTop="16dp"
       
android:onClick="btnStart_onClick"
       
android:text="START"
        
app:layout_constraintLeft_toLeftOf="parent"
       
app:layout_constraintTop_toTopOf="parent" />

    <
TableLayout
       
android:id="@+id/table_main"
       
android:layout_width="368dp"
       
android:layout_height="167dp"
       
android:layout_marginTop="96dp"
       
app:layout_constraintEnd_toEndOf="parent"
       
app:layout_constraintHorizontal_bias="0.5"
       
app:layout_constraintStart_toStartOf="parent"
       
app:layout_constraintTop_toTopOf="parent"
       
android:background="#E0E0E0"
       
>

        <
TableRow>
            <
TextView
               
android:text="Col_1:50"
               
android:layout_weight="50"
               
/>
            <
TextView
               
android:text="Col_2:50"
               
android:layout_weight="50"
               
/>
        </
TableRow>

    </
TableLayout>

</
android.support.constraint.ConstraintLayout>

 

 

 

Code-Behind

 

Java/com.codedocu.test02/mainActivity

Hier ist nur der Button Click Event wichtig

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

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

       
//--< Row >--
       
TableRow row=new TableRow(this);
        row.setPadding(
2,2,2,2);


       
//< cell1 >
       
TextView lbl1=new TextView(this);
        lbl1.setText(
"Cell1");
        lbl1.setTextColor(Color.
RED);
        lbl1.setBackgroundColor(Color.
YELLOW);

        row.addView(lbl1);
       
//</ cell1 >

        //< cell2 >
       
TextView lbl2=new TextView(this);
        lbl2.setText(
"Cell2");
        row.addView(lbl2);
       
//</ cell1 >
        //--</ Row >--

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

}

 

 

 

 

package com.codedocu.test02;

import android.graphics.Color;
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;

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);

       
//--< Row >--
       
TableRow row=new TableRow(this);
        row.setPadding(
2,2,2,2);


       
//< cell1 >
       
TextView lbl1=new TextView(this);
        lbl1.setText(
"Cell1");
        lbl1.setTextColor(Color.
RED);
        lbl1.setBackgroundColor(Color.
YELLOW);

        row.addView(lbl1);
       
//</ cell1 >

        //< cell2 >
       
TextView lbl2=new TextView(this);
        lbl2.setText(
"Cell2");
        row.addView(lbl2);
       
//</ cell1 >
        //--</ Row >--

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

}

 

 

 

Beim Run oder Debug