It is necessary that the menu item on or off the day and night modes instead of the switche. Please help with the function. When the app is in the day mode, the user selects the item menu of the Day/Night mode from the menu. It is ok, night mode is on, but when the same item is selected from the night mode, there is no change, and it should return to the day mode. Please help me.
boolean isNight = true;
switch (item.getItemId()) {
case R.id.day_night:
if (Button.isChecked()) {
//display warning message
Toast.makeText(getApplicationContext(), getString(R.string.warning_message), Toast.LENGTH_LONG).show();
return false;
} else {
if (isNight) {
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
break;
}
}
Create a global variable (outside onOptionsItemSelected)
boolean isNight; // by default, isNight = false
Then in your onOptionsItemSelected
switch (item.getItemId()) {
case R.id.day_night:
if (!isNight) {
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
isNight = true;
} else {
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
isNight = false;
}
break;
}
}
Related
I made simple dark/light mode switcher. The application OnCreate detects the mode you are on and than changes the boolean. I'm toggling this boolean and theme with button but for some reason it changes to false after second click. It only does that when the boolean is true. On false it works fine. Here's my code:
ImageButton dark_light;
boolean isDarkMode;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dark_light = findViewById(R.id.dark_light);
int nightModeFlags =
getApplicationContext().getResources().getConfiguration().uiMode &
Configuration.UI_MODE_NIGHT_MASK;
switch (nightModeFlags) {
case Configuration.UI_MODE_NIGHT_YES:
dark_light.setImageResource(R.drawable.light);
isDarkMode = true;
break;
case Configuration.UI_MODE_NIGHT_NO:
dark_light.setImageResource(R.drawable.dark);
isDarkMode = false;
break;
}
dark_light.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!isDarkMode) {
dark_light.setImageResource(R.drawable.light);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
isDarkMode = true;
} else {
dark_light.setImageResource(R.drawable.dark);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
isDarkMode = false;
}
Log.d("Dark_Light", String.valueOf(isDarkMode));
}
});
}
Whenever night mode is toggled, onCreate will be called again.
getApplicationContext().getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; always gives you the initial configuration so you are always writing the same value to isDarkMode whenever the night mode is toggled.
To get the current night mode configuration, use AppCompatDelegate.getDefaultNightMode() instead
int nightModeFlags = AppCompatDelegate.getDefaultNightMode();
switch (nightModeFlags) {
case AppCompatDelegate.MODE_NIGHT_YES:
dark_light.setImageResource(R.drawable.light);
isDarkMode = true;
break;
case AppCompatDelegate.MODE_NIGHT_NO:
dark_light.setImageResource(R.drawable.dark);
isDarkMode = false;
break;
}
I've read that you have to store the item's state and set it again because it gets cleared from the RAM. For me, If I keep scrolling up and down in a bit longer list (10-15 item) random checks will appear and disappear. Below is how I store it and set it. What should I set apart from these? I'm using this inside a fragment.
#Override
public void onBindViewHolder(#NonNull BettingViewHolder holder, int position) {
BettingItem item = items.get(position);
holder.homeTextView.setText(item.homeTeam);
holder.awayTextView.setText(item.awayTeam);
holder.dateTextView.setText(item.date);
holder.leagueTextView.setText(item.league);
holder.sportsTextView.setText(item.sport);
holder.homeOddsTextView.setText(Double.toString(item.homeOdds));
holder.drawOddsTextView.setText(Double.toString(item.drawOdds));
holder.awayOddsTextView.setText(Double.toString(item.awayOdds));
switch (item.outcome) {
case HOME:
holder.homeCheckBox.setChecked(true);
break;
case DRAW:
holder.drawCheckBox.setChecked(true);
break;
case AWAY:
holder.awayCheckBox.setChecked(true);
break;
default:
holder.homeCheckBox.setChecked(false);
holder.drawCheckBox.setChecked(false);
holder.awayCheckBox.setChecked(false);
break;
}
holder.item = item;
}
//its a part from ViewHolder's constructor
homeCheckBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
if (item != null) {
homeCheckBox.setChecked(isChecked);
if (isChecked) {
item.outcome = BettingItem.Outcome.valueOf("HOME");
drawCheckBox.setChecked(false);
awayCheckBox.setChecked(false);
} else {
if (item.outcome == BettingItem.Outcome.valueOf("HOME"))
item.outcome = BettingItem.Outcome.valueOf("NONE");
}
listener.onEventSelected(item);
}
}
});
drawCheckBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
if (item != null) {
drawCheckBox.setChecked(isChecked);
if (isChecked) {
item.outcome = BettingItem.Outcome.valueOf("DRAW");
homeCheckBox.setChecked(false);
awayCheckBox.setChecked(false);
} else {
if (item.outcome == BettingItem.Outcome.valueOf("DRAW"))
item.outcome = BettingItem.Outcome.valueOf("NONE");
}
listener.onEventSelected(item);
}
}
});
awayCheckBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
if (item != null) {
awayCheckBox.setChecked(isChecked);
if (isChecked) {
item.outcome = BettingItem.Outcome.valueOf("AWAY");
drawCheckBox.setChecked(false);
homeCheckBox.setChecked(false);
} else {
if (item.outcome == BettingItem.Outcome.valueOf("AWAY"))
item.outcome = BettingItem.Outcome.valueOf("NONE");
}
listener.onEventSelected(item);
}
}
});
you need to uncheck the old checkboxes, because the viewHolders got recycled (if the old checkBoxes were selected, it'll remain selected).
switch (item.outcome) {
case HOME:
holder.homeCheckBox.setChecked(true);
holder.drawCheckBox.setChecked(false);
holder.awayCheckBox.setChecked(false);
break;
case DRAW:
holder.homeCheckBox.setChecked(false);
holder.drawCheckBox.setChecked(true);
holder.awayCheckBox.setChecked(false);
break;
case AWAY:
holder.homeCheckBox.setChecked(false);
holder.drawCheckBox.setChecked(false);
holder.awayCheckBox.setChecked(true);
break;
default:
holder.homeCheckBox.setChecked(false);
holder.drawCheckBox.setChecked(false);
holder.awayCheckBox.setChecked(false);
break;
}
I would only want this behaviour if the user taps on it. I don't know
yet how to seperate these.
in this case, do something like this in your onCheckedChangedListener
homeCheckBox.setOnCheckedChangedListener(buttonView, isChecked -> {
if(buttonView.isPressed()){
//user pressed the button. (handle user clicks)
} else {
//the program toggled the checkbox (do nothing)
}
});
You can basically create list of objects to store your checkbox status with default status (you can use Boolean) and you need to set your recycler view checkbox status from that list. By the way, your status list need to have same size as your total cell size to avoid index out of range error. Also, when your status change you need to change your list of object in your already created list.
I have 4 items in the menu and 1 button Rec / Stop. I want, when the Rec button is active and records, the other 4 items in the menu items are disabled. Please help me.
this is activity_main.xml
<ToggleButton
android:id="#+id/recStop"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:background="#drawable/tbutton"
android:text=""
android:textOff=""
android:textOn="" />
this is tbutton.xml
<item android:drawable="#drawable/rec"
android:state_checked="false" />
<item android:drawable="#drawable/stop"
android:state_checked="true" />
this is MainActivity.java
private ToggleButton toggleButton;
toggleButton = (ToggleButton) findViewById(R.id.recStop);
// Button Rec / Stop
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
speech.setRecognitionListener(VoiceRecognitionActivity.this);
progressBar.setVisibility(View.VISIBLE);
progressBar.setIndeterminate(true);
speech.startListening(recognizerIntent);
} else {
progressBar.setIndeterminate(false);
progressBar.setVisibility(View.INVISIBLE);
speech.stopListening();
speech.destroy();
}
}
});
this is MainActivity.java
// Menu items
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.copy:
break;
}
switch (item.getItemId()) {
case R.id.share:
break;
}
switch (item.getItemId()) {
case R.id.clear:
break;
}
switch (item.getItemId()) {
case R.id.about:
break;
}
return super.onOptionsItemSelected(item);
}
Inside each condition of your switch...case, check the status of togglebutton and if its checked avoid further actions in it. Also don't use multiple switch, you must define multiple cases inside it see the code below.
switch (item.getItemId()) {
case R.id.copy:
if(toggleButton.isChecked()) {
//display warning message
} else {
// your regular code here
}
break;
case R.id.share:
if(toggleButton.isChecked()) {
//display warning message
} else {
// your regular code here
}
break;
case R.id.clear:
if(toggleButton.isChecked()) {
//display warning message
} else {
// your regular code here
}
break;
case R.id.about:
if(toggleButton.isChecked()) {
//display warning message
} else {
// your regular code here
}
break;
}
You can achieve that with this code
switch (item.getItemId()) {
case R.id.copy:
if(toggleButton.isChecked()) {
menu.findItem(R.id.copy).setEnabled(false);
} else {
menu.findItem(R.id.copy).setEnabled(true);
}
break;
//you do the same for the rest of menu buttons
}
I have a switch in my setting dialog. I want to call reset() only when the switch is moved. How can I do it?
if (showSwitch.isChecked()) {
show = true;
editor.putBoolean("show", true);
reset();
} else {
show = false;
editor.putBoolean("show", false);
reset();
}
If show is different from the switch value, do something. No if-else is required.
if (show != showSwitch.isChecked()) {
show = showSwitch.isChecked();
editor.putBoolean("show", show);
reset();
}
I would like to create a toggle button that when pressed has about a 1 second delay and toggles off.
Any help would be appreciated.
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
// Relay On
} else {
// Relay Off
}
}
From what I have read setTimeout() function is your best bet. http://www.w3schools.com/js/js_timing.asp has a tutorial on it's use.
I imagine it could be used something like this.
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
// Relay On
setTimeout(toggle(){((ToggleButton) view).setChecked(false)},1000);
} else {
// Relay Off
}
}