Home > Enterprise >  Disable button for particular time
Disable button for particular time

Time:03-09

Hello actually my app is monthly basis app.. so i have button (paybtn) on which i should click and it should get disabled till end of the month. and then should get enabled on 1st of next month. i have tried something it is getting disabled when i'm clicking but when my app is reastarted it is again getting enabled please someone help me

till now i have tried this

Calendar today = Calendar.getInstance();
int dayofmonth = today.get(Calendar.DAY_OF_MONTH);

            if (dayofmonth>1) {
                holder.paybtn.setText("paid");
                holder.paybtn.setEnabled(false);
            }else if (dayofmonth == 1){
                holder.paybtn.setEnabled(true);
            }

someone please help me

CodePudding user response:

You have three problems to solve:

  • Remembering when the monthly action has been taken.
  • Checking for a roll-over to a new month, both on startup of app and in the background while app is running.
  • Switching from legacy date-time classes to modern java.time classes.

Remember action

When the user performs their monthly action, you need to record this fact. Keeping track in memory is not enough. You need to record this fact in storage in case the user quits your app or your app crashes.

This topic is covered on Android documentation. And Stack Overflow has many existing Questions and Answers on this topic.

Check for new month

On startup of your app, retrieve the stored moment when action was last taken.

Instant instant = Instant.parse( "2022-03-09T03:51:35.013744Z" ) ;

Adjust into the month by which you want to perceive the month. For any given moment, the date, and therefore the month, may vary around the globe by time zone.

ZoneId z = ZoneId.of( "America/Edmonton" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

Get the year-month of that moment. Use the YearMonth class.

YearMonth yearMonthRecorded = YearMonth.from( zdt ) ;

Compare to current year-month.

YearMonth yearMonthCurrent = YearMonth.now( z ) ;
if( yearMonthRecorded.isBefore( yearMonthCurrent ) ) {
    … enable your button
} else {
    … disable your button
}

You might also check for recorded year-month being after current year-month, as that would be symptomatic of a problem/bug.

Check current month on background thread

While your app is running, you need to run an asynchronous task in the background performing that same or similar check.

If button should be enabled, schedule some code to run on the GUI thread to enable that button. Never access or update the GUI widgets from any thread other than the GUI thread.

How you schedule code to run on the GUI thread on Android is beyond my skillset, as I do not do Android work. All GUI frameworks such as Swing, JavaFX, and Vaadin have their own technology for posting a request to the GUI thread to perform some work, and Android is no different. This topic seems to have been addressed many times on Stack Overflow.

Write to storage

When your user performs their monthly chore, clicking that button, record the moment in storage. When writing the moment as text, use standard ISO 8601 format. The Instant#toString method generates text in that format.

Instant instant = Instant.now() ;
String momentIso8601Format = instant.toString() ;

Avoid legacy date-time classes

Never use Date/Calendar classes. These terrible legacy classes were supplanted years ago by the modern java.time classes defined in Java 8 and later.

For Java 6 & 7, use the ThreeTen-Backport library to get most of the functionality.

Android 26 comes bundled with an implementation of java.time. For earlier Android, the latest tooling makes available most of the java.time functionality via "API desugaring".

CodePudding user response:

Is this code only there in button onclick? You have to also use it where the default view is inflated.

CodePudding user response:

Once a program ends all values are dumped, you need to save your value to a file so it can be opened in your new instance of your program. I would look into DataInputStream and DataOutputStream.

try {
    File tempFile = new File("date.dat");
    file = new FileOutputStream(tempFile);
    file.write(today.MONTH);
    file.close();           
}catch(IOException e) {
    System.err.println("Exception\n"   e.toString());
}

You could for example log the month you just did.

https://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html

EDIT: added code to answer

  • Related