0

Hi I'm writing an app to pair a Bluetooth device. While device pairing, I'm adding a loading bar (progressDialog) and the loading bar should timeout (disappear) after 5 seconds. And set a text box from invisible to visible displaying the device name. And set a pair button from invisible to visible in order to jump to the next activity. However, I don't know how to timeout the loading bar, the closest thing I can do is to do a onBackPress. Could someone show me how to do the time out for the loading bar please?

package com.example.ecyclebin;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SetUpScreen extends AppCompatActivity {
    // Initialization
    Button btStart; // start bluetooth button 
    Button prButton; // start pairing button
    TextView deviceName;
    ProgressDialog progressDialog;

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

        // set bt scan button 
        btStart = findViewById(R.id.scan_bt);
        
        // set pair button invisible 
        prButton = findViewById(R.id.pair);
        prButton.setVisibility(View.INVISIBLE);

        // set device name invisible 
        deviceName = findViewById(R.id.deviceName);
        deviceName.setVisibility(View.INVISIBLE);
        
        btStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Init progress dialog
                progressDialog = new ProgressDialog(SetUpScreen.this);
                // Show Dialog
                progressDialog.show();
                // set content view
                progressDialog.setContentView(R.layout.progress_dialog);
                // set transparent background
                progressDialog.getWindow().setBackgroundDrawableResource(
                        android.R.color.transparent
                );

            }
        });

    }

    @Override
    public void onBackPressed() {
        // Dismiss Progress Bar
        progressDialog.dismiss();
        
        prButton = findViewById(R.id.pair);
        prButton.setVisibility(View.VISIBLE);

        deviceName = findViewById(R.id.deviceName);
        deviceName.setVisibility(View.VISIBLE);
    }
}

Additional Attempt:

    public void start(View v){
        Timer t = new Timer();
        TimerTask tt = new TimerTask(){
            @Override
            public void run() {
                progressDialog.dismiss();

                deviceName = findViewById(R.id.deviceName);
                deviceName.setVisibility(View.VISIBLE);

                prButton = findViewById(R.id.pair);
                prButton.setVisibility(View.VISIBLE);
            }
        };

        t.schedule(tt, 0, 3000);
    }
DavidKanes
  • 183
  • 1
  • 1
  • 10
  • Have you tried this: https://stackoverflow.com/questions/3072173/how-to-call-a-method-after-a-delay-in-android?rq=1 ? – gioravered Nov 29 '21 at 21:43
  • @gioravered unfortunately I am not using threads – DavidKanes Nov 29 '21 at 22:13
  • @I did this but it does not seem to start the event: ``` public void start(View v){ Timer t = new Timer(); TimerTask tt = new TimerTask(){ @Override public void run() { progressDialog.dismiss(); deviceName = findViewById(R.id.deviceName); deviceName.setVisibility(View.VISIBLE); prButton = findViewById(R.id.pair); prButton.setVisibility(View.VISIBLE); } }; t.schedule(tt, 0, 3000); }``` – DavidKanes Nov 29 '21 at 22:28
  • Can you add a breakpoint to check if it reaches the dismiss() call? – gioravered Nov 30 '21 at 11:56

0 Answers0