0

I want to enable the button in Android from 9.00 AM to 9.30 AM in the morning and from 5.00 PM to 5.30 PM in the afternoon in Android. Rest of the time, I want the button to be disabled. How can I do this in Java? I have posted the code below... Please modify it.

import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.navigation.NavigationView;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class mark extends AppCompatActivity {
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
NavigationView navigationView;
int currenthour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
int currentminute = Calendar.getInstance().get(Calendar.MINUTE);
boolean isButtonEnabled = currentminute <=30 && (currenthour ==9 || currenthour ==17);
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mark);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
            WindowManager.LayoutParams.FLAG_SECURE);
    setUpToolbar();
    navigationView = (NavigationView) findViewById(R.id.navigation_menu);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            switch (menuItem.getItemId())
            {
                case  R.id.nav_home:
                    Intent intent = new Intent(mark.this, home.class);
                    startActivity(intent);
                    break;
                case R.id.mark:
                    Intent intent1 = new Intent(mark.this,mark.class);
                    startActivity(intent1);
                    break;
            }
            return false;
        }
    });
    b1.setEnabled(isButtonEnabled);

}
public void setUpToolbar() {
    drawerLayout = findViewById(R.id.drawerLayout);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
    drawerLayout.addDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();

}

}

2 Answers2

2

Here is a possible Kotlin implementation:

val currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY)
val currentMinute = Calendar.getInstance().get(Calendar.MINUTE)
val isButtonEnabled = currentMinute <= 30 && (currentHour == 9 || currentHour == 17)
button.isEnabled = isButtonEnabled

Here is a possible Java implementation:

int currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
int currentMinute = Calendar.getInstance().get(Calendar.MINUTE);
boolean isButtonEnabled = currentMinute <= 30 && (currentHour == 9 || currentHour == 17);
button.setEnabled(isButtonEnabled);

EDIT

The exception is thrown because you don't initialize the b1 button instance.
Also, you should define a separate function to handle the button toggle.
Finally, the class name should be uppercased.

public class Mark extends AppCompatActivity {

    DrawerLayout drawerLayout;
    ActionBarDrawerToggle actionBarDrawerToggle;
    NavigationView navigationView;
    Button b1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mark);
        
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,WindowManager.LayoutParams.FLAG_SECURE);
        setUpToolbar();

        // Initialize the button instance
        b1 = findViewById(R.id.b1);

        navigationView = (NavigationView) findViewById(R.id.navigation_menu);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId())
                {
                    case R.id.nav_home:
                        Intent intent = new Intent(mark.this, home.class);
                        startActivity(intent);
                        break;
                    case R.id.mark:
                        Intent intent1 = new Intent(mark.this,mark.class);
                        startActivity(intent1);
                        break;
                }
                return false;
            }
        });
        
        // Enable or disable the button
        enableDisableButton()
    }

    public void setUpToolbar() {
        drawerLayout = findViewById(R.id.drawerLayout);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
    }

    // Function that handles button state
    private void enableDisableButton() {
        int currenthour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        int currentminute = Calendar.getInstance().get(Calendar.MINUTE);
        boolean isButtonEnabled = currentminute <=30 && (currenthour ==9 || currenthour ==17);
        b1.setEnabled(isButtonEnabled);
    }
}

You can find a good example of an AlarmManager implementation here.

lpizzinidev
  • 12,741
  • 2
  • 10
  • 29
  • there should be also `AlarmManager` implementation or at least some local time checker for case when user enters app at 8:59 and button then will be disabled, but probably should auto-enable after 1 minute – snachmsm Feb 23 '22 at 08:09
  • Is there any way in JAVA? – 17. Vedant Kamat Feb 23 '22 at 08:22
  • check my answer in java – Adnan Bashir Feb 23 '22 at 08:47
  • Yes, I checked it but the button will be enabled only at 9.30 and 5.30... I want it to be enabled between 9.00 to 9.30 and 5.00 to 5.30... Please help me out – 17. Vedant Kamat Feb 23 '22 at 10:44
  • @17.VedantKamat Posted the equivalent code in Java – lpizzinidev Feb 23 '22 at 13:22
  • @LucaPizzini I have edited and added my code in the above question... Please check it and modify it... It is showing some null reference exceptions for setEnabled method... Please add the Alarm Manager also... I would never forget your help.... – 17. Vedant Kamat Feb 23 '22 at 17:39
  • @LucaPizzini Hi. I have one more Question... Please help me... How can I connect MySQL database to Android studio project and perform operations... Please send me the steps and code... Please Help me... – 17. Vedant Kamat Mar 02 '22 at 14:58
0

this is a function which is returning current time in 24hours formate


    public  String getCurrentHHMMSS_24() {
        String currentDateAndTime = new SimpleDateFormat("HH:mm:ss").format(new Date());
        return currentDateAndTime;


    }

// function which return true and false acording to your time

    public  boolean buttoncheck() {
        boolean enablebutton=false;
        String[] splittime=getCurrentDateHHMMSS_24().split(":");
        if ((splittime[0].equals("09") ||  (splittime[0].equals("17"))) && splittime[1].equals("30" )) 
        {
            enablebutton=true;
        }

        return enablebutton;
    }

Adnan Bashir
  • 645
  • 4
  • 8