Blog purpose for android basic example for android app developer. any query please my contact

Saturday 31 October 2020

how to view data table layout in android studio

how to view data table layout in android studio

 In this Article Today Learn how to view data table layout in android studio. Table Layout View Data Sqlite In Android Studio.


Follow The Full Source Code how to view data table layout in android studio:

1.All_Member .java:

package com.akash.dairyapp.ViewMember;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteException;

import android.graphics.Color;

import android.os.Bundle;

import android.view.Gravity;

import android.widget.TableLayout;

import android.widget.TableRow;

import android.widget.TextView;


import com.akash.dairyapp.R;

import com.akash.dairyapp.databasea.Controllerdb;


public class All_Member extends AppCompatActivity {

    private Context context;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_all__member);

        context = this;

        Controllerdb dataHelper = new Controllerdb(context);


        TableLayout tableLayout = (TableLayout) findViewById(R.id.tblallmember);


        TableRow rowHeader = new TableRow(context);

        rowHeader.setBackgroundColor(Color.parseColor("#c0c0c0"));

        rowHeader.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,

                TableLayout.LayoutParams.WRAP_CONTENT));

        String[] headerText = {"Date", "Cutomer ID", "Name","Animal Type","Time","Litter","Fat","Rate"};

        for (String c : headerText) {

            TextView tv = new TextView(this);

            tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,

                    TableRow.LayoutParams.WRAP_CONTENT));

            tv.setGravity(Gravity.CENTER);

            tv.setTextSize(18);

            tv.setPadding(5, 5, 5, 5);

            tv.setText(c);

            rowHeader.addView(tv);

        }

        tableLayout.addView(rowHeader);


        // Get data from sqlite database and add them to the table

        // Open the database for reading

        SQLiteDatabase db = dataHelper.getReadableDatabase();

        // Start the transaction.

        db.beginTransaction();


        try {

            String selectQuery = "select Dateme,Memb_Id,Member_Name,Time,animal,Litter,Fat,Rate from Member ";

            Cursor cursor = db.rawQuery(selectQuery, null);

            if (cursor.getCount() > 0) {

                while (cursor.moveToNext()) {

                    // Read columns data

                    String outlet_id = cursor.getString(cursor.getColumnIndex("Dateme"));

                    String outlet_name = cursor.getString(cursor.getColumnIndex("Memb_Id"));

                    String outlet_type = cursor.getString(cursor.getColumnIndex("Member_Name"));

                    String outlet_type1 = cursor.getString(cursor.getColumnIndex("Time"));

                    String outlet_type2 = cursor.getString(cursor.getColumnIndex("animal"));

                    String outlet_type3 = cursor.getString(cursor.getColumnIndex("Litter"));

                    String outlet_type4 = cursor.getString(cursor.getColumnIndex("Fat"));

                    String outlet_type5 = cursor.getString(cursor.getColumnIndex("Rate"));


                    // dara rows

                    TableRow row = new TableRow(context);

                    row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,

                            TableLayout.LayoutParams.WRAP_CONTENT));

                    String[] colText = {outlet_id + "", outlet_name, outlet_type,outlet_type1,outlet_type2,outlet_type3,outlet_type4,outlet_type5};

                    for (String text : colText) {

                        TextView tv = new TextView(this);

                        tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,

                                TableRow.LayoutParams.WRAP_CONTENT));

                        tv.setGravity(Gravity.CENTER);

                        tv.setTextSize(16);

                        tv.setPadding(5, 5, 5, 5);

                        tv.setText(text);

                        row.addView(tv);

                    }

                    tableLayout.addView(row);


                }


            }

            db.setTransactionSuccessful();


        } catch (SQLiteException e) {

            e.printStackTrace();


        } finally {

            db.endTransaction();

            // End the transaction.

            db.close();

            // Close database

        }


    }


}




2.activity_all__member.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:orientation="vertical"

    android:id="@+id/invoices_layout"

    tools:context=".MainActivity"

    android:scrollbars="horizontal">



    <ScrollView

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        >

        <HorizontalScrollView

            android:id="@+id/horizontalView"

            android:layout_height="wrap_content"

            android:scrollbars="horizontal|vertical"

            android:layout_width="wrap_content"

            android:layout_marginTop="5dip">

            <TableLayout

                android:id="@+id/tblallmember"

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:padding="0dp"

                android:stretchColumns="*"

                >

            </TableLayout>

        </HorizontalScrollView>

    </ScrollView>


</LinearLayout>



3.AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    package="com.akash.dairyapp">


    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:roundIcon="@mipmap/ic_launcher_round"

        android:supportsRtl="true"

        android:theme="@style/AppTheme">


 <activity android:name=".All_Member">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

  

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


</manifest>


4.dimens.xml:

<resources>

    <!-- Default screen margins, per the Android Design guidelines. -->

    <dimen name="activity_horizontal_margin">16dp</dimen>

    <dimen name="activity_vertical_margin">16dp</dimen>

    <dimen name="nav_header_vertical_spacing">8dp</dimen>

    <dimen name="nav_header_height">176dp</dimen>

    <dimen name="fab_margin">16dp</dimen>

</resources>


5.colors.xml:

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <color name="colorPrimary">#6200EE</color>

    <color name="colorPrimaryDark">#3700B3</color>

    <color name="colorAccent">#03DAC5</color>

</resources>



No comments:

Post a Comment