skip to Main Content

So I have two activities and in the second one there is a edittext and a button. When you type in some number/s in the edittext and when you press the button it should change the fontsize of all the textviews in the first activity corresponding to the number/s you type in. I did in a certain way but my mentor said it shouldnt be like I did it. Can you please help me with another way of doing this ? Probably more simpler than this. I will post the code.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Switch buttonChangeColors = findViewById(R.id.buttonChangeColors);
        Button buttonToGoBackToMainActivity = findViewById(R.id.buttonToGoBackToMainActivity);
        Button buttonChangeFontSizeOfCalculator = findViewById(R.id.buttonChangeFontSizeOfCalculator);
        editTextInsertSize = findViewById(R.id.editTextInsertSize);
buttonChangeFontSizeOfCalculator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                float fontSize = Float.parseFloat(editTextInsertSize.getText().toString());

                textViewNumberZero = MainActivity.mainActivity.findViewById(R.id.textViewNumberZero);
                textViewNumberOne = MainActivity.mainActivity.findViewById(R.id.textViewNumberOne);
                textViewNumberTwo = MainActivity.mainActivity.findViewById(R.id.textViewNumberTwo);
                textViewNumberThree = MainActivity.mainActivity.findViewById(R.id.textViewNumberThree);
                textViewNumberFour = MainActivity.mainActivity.findViewById(R.id.textViewNumberFour);
                textViewNumberFive = MainActivity.mainActivity.findViewById(R.id.textViewNumberFive);
                textViewNumberSix = MainActivity.mainActivity.findViewById(R.id.textViewNumberSix);
                textViewNumberSeven = MainActivity.mainActivity.findViewById(R.id.textViewNumberSeven);
                textViewNumberEight = MainActivity.mainActivity.findViewById(R.id.textViewNumberEight);
                textViewNumberNine = MainActivity.mainActivity.findViewById(R.id.textViewNumberNine);

                textViewOperationEquals = MainActivity.mainActivity.findViewById(R.id.textViewOperationEquals);
                textViewOperationAdd = MainActivity.mainActivity.findViewById(R.id.textViewOperationAdd);
                textViewOperationSubtract = MainActivity.mainActivity.findViewById(R.id.textViewOperationSubtract);
                textViewOperationMultiply = MainActivity.mainActivity.findViewById(R.id.textViewOperationMultiply);
                textViewOperationDivide = MainActivity.mainActivity.findViewById(R.id.textViewOperationDivide);
                textViewOperationClear = MainActivity.mainActivity.findViewById(R.id.textViewOperationClear);
                textViewOperationClearAll = MainActivity.mainActivity.findViewById(R.id.textViewOperationClearAll);

                textViewNumberZero.setTextSize(fontSize);
                textViewNumberOne.setTextSize(fontSize);
                textViewNumberTwo.setTextSize(fontSize);
                textViewNumberThree.setTextSize(fontSize);
                textViewNumberFour.setTextSize(fontSize);
                textViewNumberFive.setTextSize(fontSize);
                textViewNumberSix.setTextSize(fontSize);
                textViewNumberSeven.setTextSize(fontSize);
                textViewNumberEight.setTextSize(fontSize);
                textViewNumberNine.setTextSize(fontSize);

                textViewOperationAdd.setTextSize(fontSize);
                textViewOperationSubtract.setTextSize(fontSize);
                textViewOperationMultiply.setTextSize(fontSize);
                textViewOperationDivide.setTextSize(fontSize);
                textViewOperationEquals.setTextSize(fontSize);

                textViewOperationClear.setTextSize(fontSize);
                textViewOperationClearAll.setTextSize(fontSize);
            }
        });
    }

2

Answers


  1. A simple idea you can consider:

    • Create a middleware to listen event from activity2 and notify to activity1.
    • Activity1 will observe to Middleware.

    You can also read Guide to app architecture to clarify how to do it.

    Login or Signup to reply.
  2. To solve this problem, you’d better review data transfer with intent.And using that many findViewById may cause the application to run slower. You should use ViewBinding instead of findViewById.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search