0

I am new to Android programming and currently trying to understand implementing simple Java in it. What I am trying to do is to understand how to add 20 textViews in Android studio. I mean, what is the best choice here. Is it:

a) Manually add 20 textViews and when I click on button A all the textViews will update with a formula. I believe this will create unnecessary java code and repetition.

b) Create a for loop, add one textView and then update automatically via a method. What I have done so far. Code underneath (immagine there is a button connected to the MainCalculate ID:

double realPrice = 10;
double total = (realPrice * 2)+1;
double increase = 0.1;
TextView percentageTextView = (TextView) findViewById(R.id.percentageTextViewID);


public void MainCalculate (View view) {

    MarketValueAnswer.setText(Double.toString(marketValueAnswer));

    for (double i=realPrice; i<=total; i+=increase) {

        double formula = ((realPrice*increase) + realPrice);
        increase+=0.05;
        percentageTextView.append("\n" + formula);

    }

Thanks and best regards,

3 Answers 3

1

If you really want to add 20 TextViews you can do it like this. The function calculation() is called when a butten is clicked.

double realPrice = 10;
double total = (realPrice * 2)+1;
double increase = 0.1;

private void calculation() {

    for (double i=realPrice; i<=total; i+=increase) {

        double formula = ((realPrice*increase) + realPrice);
        increase+=0.05;
        try {
            TextView txtView = new TextView(this);
            txtView.setText(Double.toString(formula));
            ll.addView(txtView);
        } catch (Exception ignored) {}
    }
}

This will really add 20 TextViews, it may take a little bit depending on your device hardware (better implementing a AsynchTask for this). Did I understand your question correctly?

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, you asked my question. I wanted to understand if I should use a for loop or manually add 20 textViews.So the answer is "use loop" and then format them in xml / java. Thank you
Yes, it is. See my example postet below for the best solution.
1

Here is a better approach using AsynchTask. This will prevent your app from freezing.

calculation myCalc = new calculation(this);
myCalc.execute();

Here is the AsyncTask class

private class calculation extends AsyncTask<Void, Double, Void> {
        //we need this context for the TextView instantiation

    private Context mContext;

    private calculation(Context context){
        mContext = context;
    }

    @Override
    protected Void doInBackground(Void... voids) {
        for (double i=realPrice; i<=total; i+=increase) {
            double formula = ((realPrice*increase) + realPrice);
            increase+=0.05;
            publishProgress(formula);
        }
        return null;
    }

    protected void onProgressUpdate(Double... s) {
        try {
            TextView txtView = new TextView(mContext);
            txtView.setText(Double.toString(s[0]));
            ll.addView(txtView);
        } catch (Exception ignored) {}
    }
}

Best Solution use a ScrollView with a LineraLayout inside

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/rootLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </ScrollView>

Comments

0

Create text view through loops. Its a better approach.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.