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();
}
}