Skip to content
Advertisement

Accessing to TextView from class implementing Runnable – Android Studio

I have a class ExampleRunnable which is calling the method under certain restrictions to change value percentage and TextView’s which will display this after setting visibility one line aboce. I am trying to display this value till i wont stop this thread.

My application is freezing because i cant access to TextView. Is there any solution, to get access to operate on them?

Main class:

public class ScooterDashboard extends AppCompatActivity implements LocationListener {

   
    TextView textViewBatteryPercentage;


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

        
        textViewBatteryPercentage = findViewById(R.id.textViewBatteryPercentage);

        batBluetooth = findViewById(R.id.startBatteryMeasurement);


        batBluetooth.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                stopThread(view);
                return true;
            }
        });

        batBluetooth.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.R)
            @Override
            public void onClick(View view) {
                if(stopThread){
                    startThread(view);
                    textViewBatteryDistance.setText(Double.toString(batteryDistance));
                    textViewBatteryPercentage.setText(Double.toString(batteryPercentage));
                }
            }
        });

        
    }
...
public void startThread(View view) {
        stopThread = false;
        ExampleRunnable runnable = new ExampleRunnable();
        new Thread(runnable).start();

    }

    public void stopThread(View view) {
        stopThread = true;
    }



    class ExampleRunnable extends ScooterDashboard implements Runnable {
        @Override
        public void run() {
            while (!stopThread){
                
               //do while/try/catches...
                
                    ...
                    if(currentBatteryVoltage > 0 && batterySettingsDefined){
                        setTextBattery();    <- PROBLEM 
                    }
                //more try/catches...
            }
        }
    }

    private void setTextBattery() {
        setBatteryPercentage();
        ...
    }


    private void setBatteryPercentage() {
        double percentage = ((currentBatteryVoltage - bottomCutOffX) * 100) * (upperCutOffX - bottomCutOffX);

       
textViewBatteryPercentage.setText(Double.toString(batteryPercentage)); <- problem here
        textViewBatteryPercentage.setVisibility(View.VISIBLE); <- problem here
        
        if(percentage > 90){
            batBluetooth.setImageResource(R.drawable.bat100);
        }
        ...

        batteryPercentage = percentage; //not important, its value in ScooterDasboard, i omitted many in this code example
    }

}

Advertisement

Answer

runOnUiThread(()->{


textViewBatteryPercentage.setText(Double.toString(batteryPercentage));
 textViewBatteryPercentage.setVisibility(View.VISIBLE); 
})

replace your set text with above code.Ui components cannot access inside separate threads.UI only access inside the UI Thread. If you need access UI components inside the separate thread use above code.

if you are in the fragment use as bellow

getActivity().runOnUiThread(()->{

//put your UI access code here

});
Advertisement