How do I separate my code into different files and call them within the onCreate method in android

38 views Asked by At

I'm creating a basic android app to learn. below i have the main activity that creates a few textViews on create. I'm trying to clean my project up by putting that large chunk of code in another file called "CreateCategories.java" then call a function, class, or something that runs that code. how do i do this? below is my current program

package com.company.practice;

import ...
.
.
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RelativeLayout categoryLayout = findViewById(R.id.categoryContainer);

        ArrayList<Category> categories = new ArrayList<Category>();
        categories.add(new Category("rent", "0.35", "0.00"));
        categories.add(new Category("loan", "0.1", "0.00"));

        TextView[] catogoryTitleTextView = new TextView[categories.size()];
        TextView[] catogorypercentageTextView = new TextView[categories.size()];
        TextView[] catogoryAmountTextView = new TextView[categories.size()];
        for(int i =0; i < categories.size(); i++){
            //initialize textviews
            TextView title = new TextView(this);
            TextView percentage = new TextView(this);
            TextView amount = new TextView(this);

            //set text views text, id, and textsize
            title.setText(categories.get(i).title);
            //title.setTextSize(getResources().getDimension(R.dimen.textsize));
            title.setId(i + 100);
            percentage.setText(categories.get(i).percent);
            //percentage.setTextSize(getResources().getDimension(R.dimen.textsize));
            percentage.setId(i + 200);
            amount.setText(categories.get(i).amount);
            //amount.setTextSize(getResources().getDimension(R.dimen.textsize));
            amount.setId(i + 300);

            //set params for title textview
            RelativeLayout.LayoutParams titleParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            titleParams.addRule(RelativeLayout.ALIGN_END, R.id.salaryCategoryTextVeiw);
            titleParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
            titleParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;

            if(i==0){
                //set params for title textview if it the first one. it sets below the textveiw catagory, and has more margin
                titleParams.addRule(RelativeLayout.BELOW, R.id.salaryCategoryTextVeiw);
                titleParams.topMargin = 27;
            } else {
                //this will look up the id of teh last category text view
                titleParams.addRule(RelativeLayout.BELOW, catogoryTitleTextView[i-1].getId());
                titleParams.topMargin = 15;
            }

            title.setLayoutParams(titleParams);
            //set params for percentage textview
            RelativeLayout.LayoutParams PercentParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            PercentParams.addRule(RelativeLayout.ALIGN_START, R.id.salaryPercentTextVeiw);
            PercentParams.addRule(RelativeLayout.ALIGN_TOP, title.getId());
            PercentParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
            PercentParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;

            percentage.setLayoutParams(PercentParams);

            //set params for amount textview
            RelativeLayout.LayoutParams AmountParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            AmountParams.addRule(RelativeLayout.ALIGN_START, R.id.salaryAmountTextVeiw);
            AmountParams.addRule(RelativeLayout.ALIGN_TOP, percentage.getId());
            AmountParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
            AmountParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;

            amount.setLayoutParams(AmountParams);

            //add text views to layout
            categoryLayout.addView(title);
            categoryLayout.addView(percentage);
            categoryLayout.addView(amount);


            //save the views within the arrays
            catogoryTitleTextView[i] = title;
            catogorypercentageTextView[i] = percentage;
            catogoryAmountTextView[i] = amount;
        }
    }
}

i would like it to look something like this: MainActivty.java

package com.company.practice;

import ...
.
.
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

CreateCategories.java

Function CreateCategories() {
    RelativeLayout categoryLayout = findViewById(R.id.categoryContainer);

    ArrayList<Category> categories = new ArrayList<Category>();
        categories.add(new Category("rent", "0.35", "0.00"));
        categories.add(new Category("loan", "0.1", "0.00"));

    TextView[] catogoryTitleTextView = new TextView[categories.size()];
    TextView[] catogorypercentageTextView = new TextView[categories.size()];
    TextView[] catogoryAmountTextView = new TextView[categories.size()];
        for(int i =0; i < categories.size(); i++){
        //initialize textviews
        TextView title = new TextView(this);
        TextView percentage = new TextView(this);
        TextView amount = new TextView(this);

        //set text views text, id, and textsize
        title.setText(categories.get(i).title);
        //title.setTextSize(getResources().getDimension(R.dimen.textsize));
        title.setId(i + 100);
        percentage.setText(categories.get(i).percent);
        //percentage.setTextSize(getResources().getDimension(R.dimen.textsize));
        percentage.setId(i + 200);
        amount.setText(categories.get(i).amount);
        //amount.setTextSize(getResources().getDimension(R.dimen.textsize));
        amount.setId(i + 300);

        //set params for title textview
        RelativeLayout.LayoutParams titleParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        titleParams.addRule(RelativeLayout.ALIGN_END, R.id.salaryCategoryTextVeiw);
        titleParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
        titleParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;

        if(i==0){
            //set params for title textview if it the first one. it sets below the textveiw catagory, and has more margin
            titleParams.addRule(RelativeLayout.BELOW, R.id.salaryCategoryTextVeiw);
            titleParams.topMargin = 27;
        } else {
            //this will look up the id of teh last category text view
            titleParams.addRule(RelativeLayout.BELOW, catogoryTitleTextView[i-1].getId());
            titleParams.topMargin = 15;
        }

        title.setLayoutParams(titleParams);
        //set params for percentage textview
        RelativeLayout.LayoutParams PercentParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        PercentParams.addRule(RelativeLayout.ALIGN_START, R.id.salaryPercentTextVeiw);
        PercentParams.addRule(RelativeLayout.ALIGN_TOP, title.getId());
        PercentParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
        PercentParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;

        percentage.setLayoutParams(PercentParams);

        //set params for amount textview
        RelativeLayout.LayoutParams AmountParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        AmountParams.addRule(RelativeLayout.ALIGN_START, R.id.salaryAmountTextVeiw);
        AmountParams.addRule(RelativeLayout.ALIGN_TOP, percentage.getId());
        AmountParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
        AmountParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;

        amount.setLayoutParams(AmountParams);

        //add text views to layout
        categoryLayout.addView(title);
        categoryLayout.addView(percentage);
        categoryLayout.addView(amount);


        //save the views within the arrays
        catogoryTitleTextView[i] = title;
        catogorypercentageTextView[i] = percentage;
        catogoryAmountTextView[i] = amount;
    }

any helpful advice on how to do this would be greatly appreciated.

2

There are 2 answers

0
CuriousDeveloper On

You'll have to implement a class that will perform the job and all you have to do is initialize an object of the class that you created and call the specific function of that class. If you need to create some Toasts or other stuff you will pass the context to that function and make the toast in that function. But don't forget that you have to define a public static context to reach it from an outer class.

2
rguessford On

You're doing a lot of work to make your code "more organized" but far less readable.

To do what you want to do you would have to make a "library" class (never instantiated, only contains static methods) that contains a static function that you would call from your main onCreate, return EVERYTHING you instantiated somehow, which looks like an ArrayList, a RelativeLayout, and 3 TextView arrays, assign those to variables in your calling class so that you could work with those things after you've instantiated them...

If you want to clean up your onCreate, by all means refactor the code into another function, in the same class, hopefully near where the original code is so you don't have to look between two files when you've forgotten where the values that were created in your library function came from.

The only time that you should really move code into a static library function is if you need to access that code more than one time, from more than one class.