I have a TextView that is updated but I cannot see the refresh until i minimize and reopen the application. here is the code that performs that.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bleAdapter = ((BluetoothManager) getSystemService(BLUETOOTH_SERVICE)).getAdapter();
Set<BluetoothDevice> pairedDevices = bleAdapter.getBondedDevices();
for (BluetoothDevice device : pairedDevices) {
device.connectGatt(this, true, new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
TextView state;
switch (newState) {
case BluetoothProfile.STATE_CONNECTED:
state = (TextView)findViewById(R.id.state);
state.setText("Connected = True");
state.setTextColor(Color.GREEN);
break;
case BluetoothProfile.STATE_DISCONNECTED:
state = (TextView)findViewById(R.id.state);
state.setText("Connected = False");
state.setTextColor(Color.RED);
break;
}
}
});
}
}
What I need to refresh that TextView? I'm doing anything else wrong?
You should update the TextView in the main (ui) thread, like this
runOnUiThread(new Runnable() {
public void run() {
state.setText("Connected = False");
state.setTextColor(Color.RED);
}
});
or if you project is configured to support java 8 you can write it more concisely
runOnUiThread(() -> {
state.setText("Connected = False");
state.setTextColor(Color.RED);
});