Click here to Skip to main content
15,878,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wanted to add table layout in my app and i am facing some issues while doing it.all my headers are seen,but my text view is not seen and i am not able to find my mistake.

Java
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;
import android.widget.*;

public class CalActivity extends AppCompatActivity {
    TableLayout tl;
    private Button button;
    private TableRow tr;
    private TextView tendbal,tbegbal,ttax,tinterest,tyrs,twithdrawal,tperiod;
    private int coun;
    private double begbal[]=new double[coun];
    private double stax[]=new double[coun];
    private double swithdrawal[]=new double[coun];
    private double sinterest[]=new double[coun];
    private double sendbal[]=new double[coun];
    private double period[]=new double[coun];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cal);

        tl = (TableLayout) findViewById(R.id.maintable);

        addHeaders();

        addData();
    }
    public void start(View v) {
        EditText saving2 = (EditText) findViewById(R.id.savings);
        EditText rate2 = (EditText) findViewById(R.id.rate);
        EditText withdraw2 = (EditText) findViewById(R.id.withdraw);
        EditText withrate2 = (EditText) findViewById(R.id.withrate);
        EditText tax2 = (EditText) findViewById(R.id.tax);



        double s = Double.parseDouble(saving2.getText().toString());
        double r = Double.parseDouble(rate2.getText().toString());
        double t = Double.parseDouble(tax2.getText().toString());
        double w = Integer.parseInt(withdraw2.getText().toString());
        double wr = Integer.parseInt(withrate2.getText().toString());
        int c = 0;
        double w2 = w;
        double s1 = s;
        while (s1 > 0&& s1>=w) {

            double fr1 = (r / 100);
            double i1 = s1 * fr1;
            double taxs1 = i1 - (t / 100);
            double fi1 = i1 - taxs1;
            double fa1 = s1 + fi1;
            double fin1 = fa1 - w;
            w = w + (w * (wr / 100));

            c++;

        }
        coun=c;



        while (s > 0&&s>=w2) {

            double fr = (r / 100);
            double i2 = s * fr;
            double taxs = i2 - (t / 100);
            double fi = i2 - taxs;
            double fa = s + fi;
            double fin = fa - w;
            w2 = w2 + (w2 * (wr / 100));
            for(int q=0;q<coun;q++) {
                begbal[q]= s;
                swithdrawal[q]=w2;
                sinterest[q]=i2;
                sendbal[q]=fin;
                stax[q]=taxs;
                period[q]=q+1;
            }


        }
    }
    @SuppressLint("SetTextI18n")
    public void addHeaders(){



        tr = new TableRow(this);

        tr.setLayoutParams(new TableRow.LayoutParams(

                TableRow.LayoutParams.WRAP_CONTENT,

                TableRow.LayoutParams.WRAP_CONTENT));


        TextView period = new TextView(this);

        period.setText("Period");

        period.setTextColor(Color.GRAY);

        period.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));

        period.setPadding(5, 5, 5, 0);

        period.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

        tr.addView(period);

        TextView tbegbal = new TextView(this);

        tbegbal.setText("Beginning Balance");

        tbegbal.setTextColor(Color.GRAY);

        tbegbal.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

        tbegbal.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));

        tbegbal.setPadding(5, 5, 5, 0);

        tr.addView(tbegbal);


        TextView ttax = new TextView(this);

        ttax.setText("Taxes");

        ttax.setTextColor(Color.GRAY);

        ttax.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));

        ttax.setPadding(5, 5, 5, 0);

        ttax.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

        tr.addView(ttax); // Adding textView to tablerow.
        TextView twithdrawal = new TextView(this);

        twithdrawal.setText("Withdrawal");

        twithdrawal.setTextColor(Color.GRAY);

        twithdrawal.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));

        twithdrawal.setPadding(5, 5, 5, 0);

        twithdrawal.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

        tr.addView(twithdrawal);

        TextView tinterest = new TextView(this);

        tinterest.setText("Interest");

        tinterest.setTextColor(Color.GRAY);

        tinterest.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));

        tinterest.setPadding(5, 5, 5, 0);

        tinterest.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

        tr.addView(tinterest);

        TextView tendbal = new TextView(this);

        tendbal.setText("End balance");

        tendbal.setTextColor(Color.GRAY);

        tendbal.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));

        tendbal.setPadding(5, 5, 5, 0);

        tendbal.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

        tr.addView(tendbal);





        tl.addView(tr, new TableLayout.LayoutParams(

                TableRow.LayoutParams.WRAP_CONTENT,

                TableRow.LayoutParams.WRAP_CONTENT));


        tr = new TableRow(this);

        tr.setLayoutParams(new TableRow.LayoutParams(

                TableRow.LayoutParams.WRAP_CONTENT,

                TableRow.LayoutParams.WRAP_CONTENT));



        TextView divider = new TextView(this);

        divider.setText("-----------------");

        divider.setTextColor(Color.GREEN);

        divider.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));

        divider.setPadding(5, 0, 0, 0);

        divider.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

        tr.addView(divider); // Adding textView to tablerow.

        TextView divider2 = new TextView(this);

        divider2.setText("-------------------------");

        divider2.setTextColor(Color.GREEN);

        divider2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));

        divider2.setPadding(5, 0, 0, 0);

        divider2.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

        tr.addView(divider2); // Adding textView to tablerow.
        TextView divider3 = new TextView(this);

        divider3.setText("-------------------------");

        divider3.setTextColor(Color.GREEN);

        divider3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));

        divider3.setPadding(5, 0, 0, 0);

        divider3.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

        tr.addView(divider3);
        TextView divider4 = new TextView(this);

        divider4.setText("-------------------------");

        divider4.setTextColor(Color.GREEN);

        divider4.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));

        divider4.setPadding(5, 0, 0, 0);

        divider4.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

        tr.addView(divider4);

        TextView divider5 = new TextView(this);

        divider5.setText("-------------------------");

        divider5.setTextColor(Color.GREEN);

        divider5.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));

        divider5.setPadding(5, 0, 0, 0);

        divider5.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

        tr.addView(divider5);
        TextView divider6 = new TextView(this);

        divider6.setText("-------------------------");

        divider6.setTextColor(Color.GREEN);

        divider6.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));

        divider6.setPadding(5, 0, 0, 0);

        divider6.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

        tr.addView(divider6);


        tl.addView(tr, new TableLayout.LayoutParams(

                TableRow.LayoutParams.WRAP_CONTENT,

                TableRow.LayoutParams.WRAP_CONTENT));

    }
    @SuppressLint("DefaultLocale")
    public void addData(){

        for (int i = 0; i < begbal.length; i++)

        {


            tr = new TableRow(this);

            tr.setLayoutParams(new TableRow.LayoutParams(

                    TableRow.LayoutParams.WRAP_CONTENT,

                    TableRow.LayoutParams.WRAP_CONTENT));



            tperiod = new TextView(this);

            tperiod.setText(String.format("%.2f", period[i]));

            tperiod.setTextColor(Color.RED);

            tperiod.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

            tperiod.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));

            tperiod.setPadding(5, 5, 5, 5);

            tr.addView(tperiod);  // Adding textView to tablerow.



            tbegbal = new TextView(this);

            tbegbal.setText(String.format("%.2f", begbal[i]));

            tbegbal.setTextColor(Color.GREEN);

            tbegbal.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));

            tbegbal.setPadding(5, 5, 5, 5);

            tbegbal.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

            tr.addView(tbegbal); // Adding textView to tablerow.

            ttax = new TextView(this);

            ttax.setText(String.format("%.2f", stax[i]));

            ttax.setTextColor(Color.GREEN);

            ttax.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

            ttax.setPadding(5, 5, 5, 5);

            ttax.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

            tr.addView(ttax);

            twithdrawal = new TextView(this);

            twithdrawal.setText(String.format("%.2f", swithdrawal[i]));

            twithdrawal.setTextColor(Color.GREEN);

            twithdrawal.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));

            twithdrawal.setPadding(5, 5, 5, 5);

            twithdrawal.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

            tr.addView(twithdrawal);

            tinterest = new TextView(this);

            tinterest.setText(String.format("%.2f", sinterest[i]));

            tinterest.setTextColor(Color.GREEN);

            tinterest.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));

            tinterest.setPadding(5, 5, 5, 5);

            tinterest.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

            tr.addView(tinterest);

            tendbal = new TextView(this);

            tendbal.setText(String.format("%.2f", sendbal[i]));

            tendbal.setTextColor(Color.GREEN);

            tendbal.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));

            tendbal.setPadding(5, 5, 5, 5);

            tendbal.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

            tr.addView(tendbal);


            tl.addView(tr, new TableLayout.LayoutParams(

                    TableRow.LayoutParams.WRAP_CONTENT,

                    TableRow.LayoutParams.WRAP_CONTENT));

        }

    }
}

my xml file:-
XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            tools:context="com.retirement.planning1.CalActivity" tools:ignore="ScrollViewCount">
    <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content">



        <HorizontalScrollView
                android:id="@+id/hscrll1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >



            <TableLayout
                    android:id="@+id/maintable"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
            >
            </TableLayout>

        </HorizontalScrollView>

    </RelativeLayout>
</ScrollView>


What I have tried:

i thought that i should change my LayoutParams.WRAP_CONTENT, to FILL_PARENT but that gives me an error- This inspection reports where deprecated code is used in the specified inspection scope
Posted
Comments
Mike V Baker 23-Jul-18 10:43am    
I haven't used the TableLayout, all my tabular data is shown in RecylerView with RecyclerView.Adapter to handle the data. One suggestion that I can make is that OnCreate might be too early in the life cycle for AddData. You will probably want to move that to OnResume. You also want it in OnResume because if you change screens and then change back again, OnResume is called but OnCreate is not.
I don't see in your code the place where your 'start' function is called. Is that supposed to be OnStart?
Have you put in breakpoints and debugged the app at the end of 'start' and the end of 'addData' to verify what you have to work with?
David Crow 23-Jul-18 11:04am    
Since start() is not being used, you can simplify your code snippet above by removing it.

Lastly, had you bothered to step through addData() using your debugger, you would have quickly seen that begbal.length equals 0 thus no for() loop.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900