• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Change Text on Inactive tab with tabLayout, viewPager?

masterpda13

Lurker
May 14, 2017
8
3
I have an application with a Navigation Drawer that has 6 fragments. The first fragment is the MainFragment. MainFragment has 4 fragments. In the MainActivity, an AsyncTask(MyTask) is set up which changes the text in the OneFragment(Common) and in the ThirdFragment(Internal) of MainFragment. My question is: this AsyncTask can not change tex on TextView on this tabs(Common and Internal). How to fix it. What I did: changing text on TextView works if the Common(FragmentOne) tab is activated with method TabLayout.getTabAt(0).select () and the Internal(FragmentThree) tab with tabLayout.getTabAt(2).select(). But this is not acceptable. I do not know of other ways. How to do this without tabLayout.getTabAt(0).select() and tabLayout.getTabAt(2).select() The source code is attached. MainActivity MainFragment FragmentOne(Common) FragmentTwo(Battery) FragmentThree(Internal) FragmentFour(Internal) FragmentFirst FragmentSecond .... FragmentSix
https://yadi.sk/d/2IyWYe0f3J6Pmo Source code.

Java:
class MyTask extends AsyncTask<Void, Void, Integer> {

    @Override
    protected Integer doInBackground(Void... params) {
        //Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        while (!isCancelled()) {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            publishProgress();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);

        Log.d(TAG, "MainFragment do try");

        try {
            //tabLayout.getTabAt(0).select(); textV1.setText not work when tab(0) inactive

            TextView textV1 = (TextView) findViewById(R.id.TextName2);
            textV1.setText("InternalMemory");

            TextView textV2 = (TextView) findViewById(R.id.textParam6);
            textV2.setText("All: "+bytesToHuman(getTotalInternalSpace()));

            TextView textV3 = (TextView) findViewById(R.id.textParam7);
            textV3.setText("Path: "+getInternalMemoryPath());

            TextView textV4 = (TextView) findViewById(R.id.textParam9);
            textV4.setText("Free: "+bytesToHuman(getAvailableInternalSpaceSize()));

            int t4 = (int) Math.ceil(((getAvailableInternalSpaceSize()*100)/getTotalInternalSpace()));
            TextView textV5 = (TextView) findViewById(R.id.textParam8);
            textV5.setText(String.format("%d ",t4) + "% ");

            ProgressBar textV6 = (ProgressBar) findViewById(R.id.progressBar1);
            textV6.setProgress(t4);
            if (t4<=20) {
                textV6.getProgressDrawable().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
            } else if ((t4>20) && (t4<=40)){
                textV6.getProgressDrawable().setColorFilter(Color.MAGENTA, PorterDuff.Mode.SRC_IN);
            } else if ((t4>40) && (t4<=60)){
                textV6.getProgressDrawable().setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_IN);
            } else if ((t4>60) && (t4<=80)){
                textV6.getProgressDrawable().setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN);
            } else if ((t4>80) && (t4<=90)){
                textV6.getProgressDrawable().setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_IN);
            } else if ((t4>90) && (t4<=100)){
                textV6.getProgressDrawable().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
            } else textV6.getProgressDrawable().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
                Log.d(TAG, "MainFragment do internalMemoryInfo");


            //tabLayout.getTabAt(2).select();ThreeFragment.showText not work when tab(2) inactive

            ThreeFragment.showText(textV2.getText() + "\n" +
                                   textV3.getText() + "\n" +
                                   textV4.getText() + "\n" +
                                   "Percent: " + textV5.getText());

            Log.d(TAG, "MainFragment posle internalMemoryInfo");

            } catch (Exception e) {
                Log.d(TAG, "MainFragment in Exception");
                e.printStackTrace();
            }
    }
}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones