How to reset only when the switch is moved? - java

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

Related

change the background color of three textInputlayouts by using listener

I have three text input layouts in my activity, I apply a listener on them and it changes background color when I click on it .but need to click again if I want to click the other two .my question is that how I implement such type of logic that when it 1st clicked and I click on one of the other two, the first one clickable color disappear and 2nd one or third one clicked and its background color change and same for others
View.OnClickListener listener = new View.OnClickListener() {
#SuppressLint("NonConstantResourceId")
#Override
public void onClick(View v) {
if (v.getId() == R.id.textViewLoseWeightSubtitle) {
if (mStateChanged) {
v.setBackgroundResource(R.drawable.textview_after_click);
// mLoseWgt.setTextColor(Color.WHITE);
mStateChanged = false;
mFittedToned.setClickable(false);
mBuildMuscle.setClickable(false);
} else {
v.setBackgroundResource(R.drawable.textview_outline_style);
//mLoseWgt.setTextColor(Color.parseColor("#363C60"));
mStateChanged = true;
mFittedToned.setClickable(true);
mBuildMuscle.setClickable(true);
}
}
if (v.getId() == R.id.textViewBuildMusclesSubtitle) {
if (mStateChanged) {
v.setBackgroundResource(R.drawable.textview_after_click);
// mLoseWgt.setTextColor(Color.WHITE);
mStateChanged = false;
mLoseWgt.setClickable(false);
mFittedToned.setClickable(false);
} else {
v.setBackgroundResource(R.drawable.textview_outline_style);
//mLoseWgt.setTextColor(Color.parseColor("#363C60"));
mStateChanged = true;
mLoseWgt.setClickable(true);
mFittedToned.setClickable(true);
}
}
if (v.getId() == R.id.textViewFittedAndTonedSubtitle) {
if (mStateChanged) {
v.setBackgroundResource(R.drawable.textview_after_click);
// mLoseWgt.setTextColor(Color.WHITE);
mStateChanged = false;
mLoseWgt.setClickable(false);
mBuildMuscle.setClickable(false);
} else {
v.setBackgroundResource(R.drawable.textview_outline_style);
//mLoseWgt.setTextColor(Color.parseColor("#363C60"));
mStateChanged = true;
mLoseWgt.setClickable(true);
mBuildMuscle.setClickable(true);
}
}
}
};
mLoseWgt.setOnClickListener(listener);
mBuildMuscle.setOnClickListener(listener);
mFittedToned.setOnClickListener(listener);
}
What you need is a onFocusChangedListener(). It gives a callback with a boolean which can be used to identify whether the current view is selected or not.
Declare it as:
View.OnFocusChangeListener listener = new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
v.setBackgroundResource(R.drawable.textview_after_click);
// mLoseWgt.setTextColor(Color.WHITE);
} else {
v.setBackgroundResource(R.drawable.textview_outline_style);
//mLoseWgt.setTextColor(Color.parseColor("#363C60"));
}
}
});
Set it as:
mLoseWgt.setOnFocusChangeListener(listener);
mBuildMuscle.setOnFocusChangeListener(listener);
mFittedToned.setOnFocusChangeListener(listener);
That's it. Your other code seems redundant. For EditText specific functions, you can typecast the view provided by onFocusChange().

recyclerview-selection how to listen event onSelectionEnter and event onSelectionExit

I use following to use recyclerview-selection:
selectionTracker = new SelectionTracker.Builder<>("lazy-load-img-list-selection", this.recyclerView, new KeyProvider(1), new Lookup(), StorageStrategy.createLongStorage()).build();
I expect
selectionTracker.setOnSelectionEnter
selectionTracker.setOnSelectionExit
to toggle buttons "remove selection" "clear selection" visibility
how to do?
update:
I can detect selection status recycler adapter #onBindViewHolder, but it's not good way, when determine onSelectionExit, I must detect all items status each rendering each item
I find solution:
selectionTracker.addObserver(new SelectionTracker.SelectionObserver() {
#Override
public void onItemStateChanged(#NonNull Object key, boolean selected) {
super.onItemStateChanged(key, selected);
Selection selection = selectionTracker.getSelection();
if (selected && selection.size() == 1) {
if (onSelectionEnter != null) {
onSelectionEnter.run();
}
} else if (!selected && selection.size() == 0) {
if (onSelectionExit != null) {
onSelectionExit.run();
}
}
}
});

RecyclerView losing Checkbox status despite storing it

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.

Menu Item on/off day night mode

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

Edittext have to be focus always

I have an EditText. I want this edittext to always be in focus and writeable. How can I do this without touching it every time? Because barcode scanner machine sends data successive to this app.
anasayfa_barkod.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if ((keyEvent.getAction() == KeyEvent.ACTION_DOWN) &&
(i == KeyEvent.KEYCODE_ENTER)) {
adapter.clear();
adapter.notifyDataSetChanged();
geciciListeyeEkle();
listeyiYukle();
adapter.notifyDataSetChanged();
tutarYazdir();
if(anasayfa_verilenUcret.getText().length()>0){
try{
String ucret=anasayfa_verilenUcret.toString();
String paraustu=String.valueOf(Double.parseDouble(ucret)-gelenSatis);
anasayfa_paraustu.setText(paraustu);
}catch (NumberFormatException e){
e.printStackTrace();
}
}
anasayfa_barkod.requestFocus(); //not working
return true;
}
return false;
}
});
I tried a lot of method.but all of them is not working. I can set the keyboard is visible but cursor not on edittext.
I solved my problem with enter link description here
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if ( anasayfa_barkod!= null) {
anasayfa_barkod.requestFocus();
}
}
}, 1000);
If you are in activity add this in your onCreate method:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT‌​_INPUT_STATE_ALWAYS_‌​VISIBLE);
if you are in fragment use this:
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT‌​_INPUT_STATE_ALWAYS_‌​VISIBLE);
I think may I can help. Use this in onCreate method
editText.requestFocus();
it works for me.

Categories