0

I'm newbie to Android Application development, so really appreciate guidance on this.

When I have connection to my server, the main page will load instantly with all text and buttons. However, if im not connected to the network, the page will shows blank white screen for about 10sec after I open the application before the whole screen showing.

I think this is because the app are trying to connect to the server, before sending error message as a textview. how do i make it so that the screen shows 1st and the connecting to server run in the background and later show the result whether its connected or not. below are the code that i used, in onCreate.

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

    TrackingNo = findViewById(R.id.TrackingNoTxt);
    ConnectionStatus = findViewById(R.id.connectionStatTxt);

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PackageManager.PERMISSION_GRANTED);

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    try {
        Class.forName(Classes);
        connection = DriverManager.getConnection(url, username, password);
        ConnectionStatus.setText("Connection Established!");
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
        ConnectionStatus.setText("Error connecting to the server. Please check your connection!");
    }

}

Any suggestions will gladly be appreciated. Thanks

fahmijaafar
  • 175
  • 5
  • 14
  • First Check for [Internet connection availability](https://stackoverflow.com/a/33256406/8244632) and if it's not available, show a message, second, if internet is available, show a [loading progress bar](https://stackoverflow.com/questions/12559461/how-to-show-progress-barcircle-in-an-activity-having-a-listview-before-loading), so user can be made aware that something is loading and UI is not stuck/frozen and call your function where you try to establish the connection with server. Third, hide the loading screen and show your message whether connection is established or not. – Lalit Fauzdar May 27 '20 at 04:52
  • ive set message to be shown if connection is not available. however, the message will also appears after like 10sec of blank screen. so i think the problem is because the code is trying to finish 'try' before loading the screen. How do I set the screen to load first before the code finish 'try' – fahmijaafar May 27 '20 at 06:03

0 Answers0