Showing posts with label Android Table. Show all posts
Showing posts with label Android Table. Show all posts

Saturday 30 July 2016

Tabel Dengan Data Dinamis 2 -Android

Gambar 2 Tabel dengan data dinamis  contoh 2

Kebutuhan untuk menampilkan data ke dalam tabel ada beranekaragam.
Tutorial ini akan memberikan sourc code salah satu cara menampilkan data dengan tabel (hasil seperti gambar 1).

MainActivity.java

activty_main.xml

layout_row.xml



Sekian dan Trimakasih

Membuat Tabel Dinamis 1 -Android


Gambar 1. Tabel pada Android



1. Sebelum membuat tabel dinamis saya tampilkan dulu xml  dari tabel pada Android:

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">



    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tablelayoutid"
        android:layout_marginTop="10dp">

        <TableRow
            android:id="@+id/rowlay"
            android:background="@android:color/holo_blue_light"
            android:padding="5dp">
            <TextView
                android:layout_width="40dp"
                android:layout_height="wrap_content"

                android:text="No" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"

                android:text="Nama Perusahaan" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Pemasukan" />
        </TableRow>

        <TableRow
            android:background="#ECEFF1"
            android:padding="5dp">
            <TextView
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                android:text="1" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Perusahaan A" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Masuk" />
        </TableRow>

        <TableRow
            android:background="#ECEFF1"
            android:padding="5dp">
            <TextView
                android:layout_width="40dp"
                android:layout_height="wrap_content"

                android:text="2" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Perusahaan B" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Belum Masuk" />
        </TableRow>
        <TableRow
            android:background="#ECEFF1"
            android:padding="5dp">
            <TextView
                android:layout_width="40dp"
                android:layout_height="wrap_content"

                android:text="3" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Perusahaan C" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Masuk" />
        </TableRow>
    </TableLayout>

</RelativeLayout>


setelah activity_main.xml dijalankan maka hasilnya seperti Gambar 1.

2. Kemudian buat layout row nya

layout_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
        android:background="#ECEFF1"
        android:padding="5dp">
        <TextView
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:id="@+id/idnotabel"    
            android:text="3" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/idnamaperusahaantabel"
            android:text="Perusahaan C" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/idpemasukantabel"
            android:text="Masuk" />
    </TableRow>

3. Tambah kan baris/ row baru dengan source code java dibawah
MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        TableLayout tablelayoutid = (TableLayout)this.findViewById(R.id.tablelayoutid);
        // Inflate your row "template" and fill out the fields.
        TableRow row = (TableRow)getLayoutInflater().inflate(R.layout.layout_row, null);
        ((TextView)row.findViewById(R.id.idnotabel)).setText("4");
        ((TextView)row.findViewById(R.id.idnamaperusahaantabel)).setText("Perusahaan D");
        ((TextView)row.findViewById(R.id.idpemasukantabel)).setText("Masuk");
        tablelayoutid.addView(row);
        return true;
    }


}

4. Sehingga hasilnya menjadi gambar 2

Gambar 2. Hasil akhir tabel dinamis

5. Lalu bagaimana jika ingin menambahkan beberapa baris menggunakan looping java caranya bisa dilihat di pastebin berikut ( hasilnya gambar 3)


Gambar 3. Menambahkan beberapa nilai pada tabel


Sekian dan Trimakasih