Enabling and Disabling a View Based on Specific Conditions

14 Views Asked by At

I have implemented a download button (ImageView) that performs some validation processes when clicked. If all the validations pass successfully, the app navigates to their respective activities. Now, my requirement is to disable the download button when it is clicked, and I want to enable it again at specific points during link validation.

I need to enable the button within each 'if' or 'else' condition where the navigation to a new intent does not occur. I have added comments in the code to indicate where the button should be enabled.

Furthermore, after the link validation process is successfully completed and the app navigates to a new activity, I also want the button to be re-enabled when the user returns from that activity.

I attempted to achieve this functionality with the following code, but it didn't work as expected:

private boolean isButtonEnabled = true;

//Btn is in OnCreate 
     downloadBtnCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

              
                if (isButtonEnabled) {
                    editTextValue[0] = Objects.requireNonNull(editText.getText()).toString();

                    // Step 1: Check if the network is available
                    if (isNetworkAvailable(getApplicationContext())) {
                        // Step 2: Check the network speed
                        try {
                            linkValidationAndUrl(editTextValue[0]);
                            downloadBtnCard.setClickable(false);
                            isButtonEnabled = false;
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } else {
                        // Show a message to the user indicating no network connection
                        textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
                        textStatus.setText(R.string.noInternet);
                        Toast.makeText(getApplicationContext(), "No Internet", Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });

 public void linkValidationAndUrl(String url) throws IOException {
        textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.white));
        textStatus.setText(getString(R.string.validating_link));
        if (url.startsWith("https://www.threads.net") || url.startsWith("https://threads.net")) {
            MainNav(url, new AccountStatusCallback() {
                @Override
                public void onAccountStatusReceived(String[] accountStatus) {
                    // Regex pattern for profile link
                    Pattern profileLinkPattern = Pattern.compile("^https://(?:www\\.)?threads\\.net/@[^/]+$");
                    Pattern post1Pattern = Pattern.compile("(?<=.net\\/t\\/).*(?=\\/\\?)|(?<=post\\/).*");
                    Matcher profileMatcher = profileLinkPattern.matcher(url);
                    Matcher post1Matcher = post1Pattern.matcher(url);
                    if (accountStatus[0].equals("true")) {
                        textStatus.setText(null);
                        Toast.makeText(getApplicationContext(), "Broken Url or Private Profile", Toast.LENGTH_SHORT).show();
                        textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
                        textStatus.setText(getString(R.string.brokenorprivateStr));
                        //Enable the BTN
                        downloadBtnCard.setClickable(true);
                        isButtonEnabled = true;
                    } else if (accountStatus[0].equals("unexpected")) {
                        textStatus.setText(null);
                        Toast.makeText(getApplicationContext(), "Unexpected Error", Toast.LENGTH_SHORT).show();
                        textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
                        textStatus.setText(getString(R.string.unexpected_error));
                        //Enable the BTN
                        downloadBtnCard.setClickable(true);
                        isButtonEnabled = true;
                    } else if (accountStatus[0].equals("false")) {
                        Toast.makeText(getApplicationContext(), "Page Avail", Toast.LENGTH_SHORT).show();
                        if (profileMatcher.matches()) {
                            textStatus.setText(null);
                            textStatus.setText(getString(R.string.profile_link));
                            Intent iPr = new Intent(getApplicationContext(), ProfileActivity.class);
                            startActivity(iPr);

                        } else if (post1Matcher.find()) {
                            textStatus.setText(null);
                            if (accountStatus[1].equals("true")) {
                                //Navigate to comment page
                                textStatus.setText("Comment Link Provided");
                                Intent iC = new Intent(getApplicationContext(), CommentActivity.class);
                                startActivity(iC);
                            } else if (accountStatus[1].equals("false")) {
                                // Navigate to post page
                                textStatus.setText("Post Link Provided");
                                Intent iP = new Intent(getApplicationContext(), PostActivity.class);
                                startActivity(iP);
                            } else if (accountStatus[1].equals("unexpected")) {
                                textStatus.setText(null);
                                Toast.makeText(getApplicationContext(), "Unexpected Error", Toast.LENGTH_SHORT).show();
                                textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
                                textStatus.setText(getString(R.string.unexpected_error));

                            }
                        } else {
                            textStatus.setText(null);
                            textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
                            textStatus.setText(getString(R.string.threadlink_invaliddataurl));
                            //Enable the BTN
                            downloadBtnCard.setClickable(true);
                            isButtonEnabled = true;
                        }
                    } else {
                        textStatus.setText(null);
                        textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
                        textStatus.setText("Unable To Get Data");
                        //Enable the BTN
                        downloadBtnCard.setClickable(true);
                        isButtonEnabled = true;
                    }
                }

            });


        } else if ((!url.startsWith("https://")) && (url.contains("www.threads.net") || url.contains("threads.net"))) {

            textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
            textStatus.setText(getString(R.string.threadLink_NoHttps));
            //Enable the BTN
            downloadBtnCard.setClickable(true);
            isButtonEnabled = true;


        } else {
            Toast.makeText(MainActivity.this, "Invailed Url", Toast.LENGTH_SHORT).show();
            textStatus.setText(null);
            textStatus.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.orange));
            textStatus.setText(getString(R.string.threadLink_False));
            //Enable the BTN
            downloadBtnCard.setClickable(true);
            isButtonEnabled = true;
        }

    }

 @Override
    protected void onResume() {
        super.onResume();
        if (isButtonEnabled) {
            // If the button was clicked and the activity is resumed, enable the button again
            downloadBtnCard.setClickable(true);
            isButtonEnabled = true; // Reset the flag
        }
    }
0

There are 0 best solutions below