I am trying to make an app that lists all the predefined tasks a person has to do daily. To mark them I'm using checkboxes in vertical linearlayout. I'm using for loop to iterate through the layout. I want that if a checkbox is checked, an integer (total) is incremented by 1 and if it is unchecked then the integer remains same. Here is the method:
CheckBox checkBox151, checkBox152, checkBox153;
LinearLayout sundayLayout;
int total = 0;
int[] boxState = {2, 2, 2, 2, 2, 2, 2};
public int formIsValid(LinearLayout layout) {
boolean wasChecked = false;
for (int i = 0; i < layout.getChildCount(); i++) {
View v = layout.getChildAt(i);
if (v instanceof CheckBox) {
if (((CheckBox) v).isChecked() && boxState[i] == 2 && wasChecked == false) {
total++;
boxState[i] = 0;
wasChecked = true;
} else if (boxState[i] == 1 && wasChecked == true) {
total = total - 1;
boxState[i] = 2;
} else if (boxState[i] == 0 && wasChecked == false) {
boxState[i] = 2;
}
}
}
return total;
}
I tried various logic statements, but what I get eventually is either the number getting incremented ok but decremented when I check the box again (I want it to decrement when unchecked, and increment when checked ONLY), but when I try this the app crashes due to bad logic statements.
Instant help needed, And thanks in advance...
This is the main purpose of Listeners. They watch the changes you make on components.
In your case you should use a CompoundButton.OnCheckedChangeListener
In your case, using a listener would let you only track the relevant changes (check / uncheck). Since it would be only called when a check / uncheck event has been triggered.
You also don't need to "validate" the values of the checkboxes by looping every time on the LinearLayout to be able to increment / decrement the counter value.
Here's how you would use it in your case:
public class MyActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
private int counter;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
// ........
CheckBox checkBox1 = findViewById (R.id.checkbox1);
CheckBox checkBox2 = findViewById (R.id.checkbox2);
CheckBox checkBox3 = findViewById (R.id.checkbox3);
checkBox1.setOnCheckedChangeListener (this);
checkBox2.setOnCheckedChangeListener (this);
checkBox3.setOnCheckedChangeListener (this);
}
#Override
public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
counter ++;
} else {
counter --;
}
}
}
Then when submitting your form, just grab the counter variable and do whatever you want with it.
EDIT: Regarding the 175 requirements, the concept stays the same. You'll register a listener to all of them.
The OnCreate method becomes:
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
// ........
// For each one of the 7 layouts you'll call the following (better create a method containing the below)
for (int i = 0; i < layout.getChildCount(); i++) {
View v = layout.getChildAt(i);
if (v instanceof CheckBox) {
((CheckBox)v).setOnCheckedChangeListener (this);
}
}
Related
I am programming a darts counter app and at the moment I am trying to get an user input (which field on the dart board they hit) by pressing on a specific button.
Each button press will return an int which will be used to update list values that are used by my adapter to then update the views.
The method that should happen in looks like this:
private void startRound(MatchActivityPlayerAdapter adapter) {
for (int playerIndex = 0; playerIndex < getMatchParticipants().size(); playerIndex++) {
for (int currentDart = 1; currentDart <= maximumDartsToThrow; currentDart++) {
// Here I want the activity to "wait" until the user presses a button
if (pointsButtonClicked) {
setDartValue(playerIndex, currentDart);
setDartsCombinedValues(playerIndex, currentDart);
setRemainingPointsValues(playerIndex, currentDart);
adapter.notifyDataSetChanged();
}
}
}
setCurrentRound(getCurrentRound() + 1);
}
Since I cant really stop the activity at the point mentioned above until user has made an input I'll propably have to include a while loop. But in order to do this I think I'll have to create a second thread and handle things differently. That actually overwhelmed me, even though I've been reading through this.
Can anyone please explain to me how I have to design my code in order to achieve what i want?
Edit: Actually pointsButtonClicked is a boolean.
I have over 20 buttons in the global OnClick method and whenever one of them is clicked pointsButtonClicked will be set to true.
Button button1 = findViewById(R.id.btn1);
button1.setOnClickListener(v -> {
outputInt = 1;
pointsButtonClicked = true;
});
Button button2 = findViewById(R.id.btn2);
button2.setOnClickListener(v -> {
outputInt = 2;
pointsButtonClicked = true;
});
// [...]
I think that there should be a better way to this without the while-wait loop.
First of all I suppose that you used an android.widget.Button to implement that pointsButtonClicked
Here's what I would do:
//Global in your activity
int playerIndex = 0;
int currentDart = 1;
Then
private void startRound(MatchActivityPlayerAdapter adapter)
{
pointsButtonClicked.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
setDartValue(playerIndex, currentDart);
setDartsCombinedValues(playerIndex, currentDart);
setRemainingPointsValues(playerIndex, currentDart);
adapter.notifyDataSetChanged();
nextThrow();
}
});
}
And finally
//Also in your activity
private void nextThrow()
{
if (currentDart == maximumDartsToThrow)
{
currentDart = 1;
if (playerIndex == getMatchParticipants().size()-1)
{
playerIndex = 0;
setCurrentRound(getCurrentRound() + 1);
}
else
{
++playerIndex;
}
}
else
{
++currentDart;
}
}
It can be better
You can use private variables and access them with getter and setter
Explained
Using this approach you do not create a thread that waits for every single button pressed. You simply create a button that listen for every click event that it receives and then you apply the logic to cycle the players and the darts for every players.
I hope this could be helpful.
[EDIT]
In this case I would apply the same OnClickListener to every button you use.
First of all I would create an inner class that implements OnClickListener and allows you to manage outputInt variable.
//Also in Activity (it is an inner class)
public class MyOnClickListener implements View.OnClickListener
{
private int myOutputInt;
public MyOnClickListener (int outputInt)
{
this.myOutputInt = outputInt;
}
#Override
public void onClick(View v)
{
pointsButtonClicked = true;
outputInt = myOutputInt;
setDartValue(playerIndex, currentDart);
setDartsCombinedValues(playerIndex, currentDart);
setRemainingPointsValues(playerIndex, currentDart);
nextThrow();
}
}
And then you create 20 MyOnClickListener passing only the int you want to assign. For example:
button1.setOnClickListener(new MyOnClickListener(1));
button2.setOnClickListener(new MyOnClickListener(2));
button3.setOnClickListener(new MyOnClickListener(3));
etc etc...
I have a list of timezones that gets added to a recycler view. However, my main list in the activity checks properly but when I use the search and the list condenses and I click the checkbox it will not show the checkmark. However, in debug, the value is set to true when clicked and it will still add it into the recycler view properly.
I have tried looking online but there was no solution for this specific problem.
#Override
public void onBindViewHolder(#NonNull final TimezoneViewHolder holder, final int position) {
// Initialize tools
final Timezone_Item currentTimezoneItem = timezoneList.get(position);
int pos = currentTimezoneItem.getId();
final int tzID = --pos;
holder.mChkboxSelect.setText(currentTimezoneItem.getValue());
holder.mUTCCode.setText(currentTimezoneItem.getName());
// This is the solution for... Clicking the checkbox once would select multiple timezones. Not with this.
if(selectedTimezones.get(position)){
holder.mChkboxSelect.setChecked(true);
currentTimezoneItem.setIsSelected(true);
}else{
holder.mChkboxSelect.setChecked(false);
currentTimezoneItem.setIsSelected(false);
}
// Manually activate the clicks in checkbox
holder.mChkboxSelect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(currentTimezoneItem.getIsSelected()){
currentTimezoneItem.setIsSelected(false);
holder.mChkboxSelect.setChecked(false);
}else {
currentTimezoneItem.setIsSelected(true);
holder.mChkboxSelect.setChecked(true);
}
if(TimezonePickerActivity.isSearching){
selectedTimezones.put(currentTimezoneItem.getId() - 1, currentTimezoneItem.getIsSelected());
}else {
selectedTimezones.put(tzID, currentTimezoneItem.getIsSelected());
}
notifyDataSetChanged();
}
});
}
This is my Search filter...
private Filter SearchFilter = new Filter() {
#Override
protected FilterResults performFiltering(CharSequence searchText) {
List<Timezone_Item> filteredList = new ArrayList<>();
if (searchText == null || searchText.length() == 0) {
TimezonePickerActivity.isSearching = false;
filteredList.addAll(timezoneListFull);
} else {
String filterPattern = searchText.toString().toLowerCase().trim();
TimezonePickerActivity.isSearching = true;
for (Timezone_Item item : timezoneListFull) {
if (item.getName().toLowerCase().contains(filterPattern)) {
filteredList.add(item);
}
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = filteredList;
return filterResults;
}
#Override
protected void publishResults(CharSequence searchText, FilterResults results) {
timezoneList.clear();
timezoneList.addAll((List) results.values);
notifyDataSetChanged();
}
};
This is my code to add the selected timezone into the recycler view
fabAddTimezone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) { SparseBooleanArray selectedTimezones = Timezone_RVAdapter.selectedTimezones;
// Filter out false values
for (int i = 0; i < selectedTimezones.size(); i++) {
if(!selectedTimezones.valueAt(i)){
selectedTimezones.removeAt(i);
selectedTimezones.delete(i);
}
}
// Take filtered values and find its index to grab text and UTC code
for (int i = 0; i < selectedTimezones.size(); i++) {
// Get the position(Key) which is actually the Timezone_Item ID
int position = selectedTimezones.keyAt(i);
// Create new clock item to add into list
Clock_Item clockItem = new Clock_Item(
Timezone_RVAdapter.timezoneListFull.get(position).getName(),
Timezone_RVAdapter.timezoneListFull.get(position).getValue()
);
// Add clock to a list
mClockList.add(clockItem);
}
// Save clock list
sharedPrefs.SaveClockList(mClockList);
// Go back to main menu. Clock list should automatically load once activity boots
finish();
}
});
There is a possibility that the below block is always true
if(currentTimezoneItem.getIsSelected()){
currentTimezoneItem.setIsSelected(false);
//Calling the below statement is irrelevant inside onClick of itself
//because when inside here checkbox can never be checked
holder.mChkboxSelect.setChecked(false);
}
remove or comment out every line statement calling .setChecked on mChkboxSelect and allow android handle the state. You can control the state of a checkbox but not inside it's onClick event because clicking on a checkbox automatically changes the state.
I am trying to change background color in specific item(s) in a RecycleView.
Because I need to set text too, I have the following code that works fine:
protected void populateViewHolder(RankingViewHolder viewHolder, final Ranking model, int position)
{
final Context mContext = getActivity().getApplicationContext();
viewHolder.txt_name.setText(model.getUserName());
viewHolder.txt_score.setText(String.valueOf(model.getScore()));
viewHolder.txt_class.setText(model.getUser_class());
Picasso.with(mContext).load(model.getAvatarUrl()).error(R.drawable.ic_people_black_24dp).into(viewHolder.personPhoto);
int totalRanking = adapter.getItemCount();
int realRank = totalRanking - viewHolder.getAdapterPosition();
viewHolder.ranknumber.setText("# "+String.valueOf(realRank));
}
This works as I want and realRanktakes the correct values, and the viewHolder.ranknumber.setText("# "+String.valueOf(realRank));
Sets the right text with no problem.
Now I am trying (as I got a number/text result correct, to make an if statement like this:
if(adapter.getItemCount() -viewHolder.getAdapterPosition() == 0)
{
viewHolder.itemView.setBackgroundColor(Color.GREEN);
}
if(adapter.getItemCount() -viewHolder.getAdapterPosition() == 1)
{
viewHolder.itemView.setBackgroundColor(Color.YELLOW);
}
if(adapter.getItemCount() -viewHolder.getAdapterPosition() == 2)
{
viewHolder.itemView.setBackgroundColor(Color.BLUE);
}
(I tried with String.valueOf(realRank)equality, with realRankequality too)
In all cases I have the same result. The color changes as its should at positions 1,2,3 BUT it changes at positions 7,8,9 and 14,15,16 and 21,22,23 etc.
What am I missing here?
public class RankingViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
private ItemClickListener itemClickListener;
public TextView txt_name, txt_score, txt_class, ranknumber;
public ImageView personPhoto;
public RankingViewHolder(View itemView)
{
super(itemView);
txt_name = itemView.findViewById(R.id.txt_name);
txt_score = itemView.findViewById(R.id.txt_score);
personPhoto = itemView.findViewById(R.id.person_photo);
txt_class = itemView.findViewById(R.id.txt_class);
ranknumber = itemView.findViewById(R.id.ranknumber);
itemView.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
#Override
public void onClick(View view) {
itemClickListener.onClick(view , getAdapterPosition(),false);
}
}
The adapter:
adapter = new FirebaseRecyclerAdapter<Ranking, RankingViewHolder>(
Ranking.class,
R.layout.layout_ranking,
RankingViewHolder.class,
rankingTbl.orderByChild("score").limitToFirst(100)
)
This line of code int realRank = totalRanking - viewHolder.getAdapterPosition();gives a number (1,2,3,4,5,6 etc.) Why i cannot use this number to check equality?
Notice
Keeping this code for NOT working solution, just for future reference:
if(position == 0){
viewHolder.itemView.setBackgroundColor(Color.GREEN);
}
else if(position == 1){
viewHolder.itemView.setBackgroundColor(Color.YELLOW);
}
else if(position == 2){
viewHolder.itemView.setBackgroundColor(Color.BLUE);
}
else{
viewHolder.itemView.setBackgroundColor(Color.WHITE);
}
This changes the color BUT not for only 3 first items. As you scroll down, changes the color for every 3 first viewable items like before, meaning 1,2,3, 7,8,9, etc.
Update:
I dont use a custom adapter, i use FirebaseRecyclerAdapter.
Thanks to #Muhammad Haroon comment i checked that has getItemViewType. So now i m trying with it like
position =adapter.getItemViewType( 0);
if(position == 0){
viewHolder.itemView.setBackgroundColor(Color.GREEN);
}
Not working for now, but i think its the correct direction...
Update 2
With position its not possible as RecycleView recycles the views so i have the same result. The working code is
if (linearLayoutManager.findFirstVisibleItemPosition() > 0) {
viewHolder.itemView.setBackgroundResource(R.drawable.blackframe);
}
else{
viewHolder.itemView.setBackgroundResource(R.drawable.goldframe);
}
Works fine except that after scrolling loosing the change of background.
So as we want and need the perfection, any idea for keeping even after scroll?
hi try add this in your Adapater it may solve your problem.
#Override
public int getItemViewType(int position) {
return position;
}
Please give this a try
override in your custom adapter
#Override
public long getItemId(int position) {
return position;
}
and in in your adapter object:
myAdapter.setHasStableIds(true);
In populateViewHolder add these line of code
if(position == 0){
viewHolder.itemView.setBackgroundColor(Color.GREEN);
}
else if(position == 1){
viewHolder.itemView.setBackgroundColor(Color.YELLOW);
}
else if(position == 2){
viewHolder.itemView.setBackgroundColor(Color.BLUE);
}
else{
viewHolder.itemView.setBackgroundColor(Color.WHITE);
}
position is a parameter in populateViewHolder.
I have three checkboxes and if any two of them are selected I want to disable the third checkbox immediately. I am not getting how to proceed.
Manage your CheckBoxes in a List, so you can iterate over them.
List<CheckBox> boxes = new ArrayList<>();
Assign them like so (in onCreate() of your activity)
boxes.add((CheckBox) findViewById(R.id.checkbox1));
boxes.add((CheckBox) findViewById(R.id.checkbox2));
boxes.add((CheckBox) findViewById(R.id.checkbox3));
Add an onCheckedChangeListener like this:
for(CheckBox box : boxes){
box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
manageBoxes();
}
});
}
and finally your manageBoxes()
private void manageBoxes(){
int count = 0;
for(CheckBox box : boxes){
count += box.isChecked()?1:0;
}
if(count>=2){
for(CheckBox box : boxes){
box.setEnabled(box.isChecked());
box.setClickable(box.isChecked()); // not sure if needed
}
}else{
// reset all boxes
for(CheckBox box : boxes){
box.setEnabled(true);
box.setClickable(true);
}
}
}
Just a quick and dirty thought. Hope this works.
Plus: This is scalable, so you could include some more checkboxes if needed.
Try to implement this way :
private ArrayList<Integer> integers = null;
integers = new ArrayList<>();
chkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
int position = (int) yourview.getTag();
if (chkBox.isChecked()) {
integers.add(position);
chkBox.setChecked(true);
} else {
if (integers.size() > 0) {
for (int i=0; i<integers.size(); i++) {
if (integers.get(i) == position) {
integers.remove(i);
}
}
});
if (integers.size() > 2) {
//Disable your third checkbox
}
Add a change handler or a click handler on each of the checkboxes and let it call a small method that is responsible for checking the value of each checkbox: if two of them are true disable the third one.
For example:
checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
handleCheckBoxes();
}
});
//Do this for all the checkboxes
private void handleCheckBoxes(){
int checkedCount = 0;
CheckBox uncheckedBox = null;
if(checkBox1.isChecked()){
checkedCount++;
}
else{
uncheckedBox = checkBox1;
}
if(checkBox2.isChecked()){
checkedCount++;
}
else{
uncheckedBox = checkBox2;
}
if(checkBox3.isChecked()){
checkedCount++;
}
else{
uncheckedBox = checkBox3;
}
if(checkedCount == 2){
uncheckedBox .setEnabled(false);
}
else{
//enable all checkboxes
checkBox1.setEnables(true);
checkBox2.setEnables(true);
checkBox3.setEnables(true);
}
In pseudo code this would be:
Create variable to hold the number of checkboxes checked, let's call it amountChecked
When a checkbox gets checked => increment the amountChecked
When a checkbox gets checked => check if the amountChecked is equal or greater than the allowedAmountChecked (in your case this is 2)
=> if it is greater or equal to the allowedAmountChecked then disable the remaining checkboxes
I'm using this Slider.
This is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDemoSlider = (SliderLayout)findViewById(R.id.slider);
HashMap<String,Integer> file_maps = new HashMap<String, Integer>();
file_maps.put("Hannibal",R.drawable.hannibal);
file_maps.put("Big Bang Theory",R.drawable.bigbang);
file_maps.put("House of Cards",R.drawable.house);
file_maps.put("Game of Thrones", R.drawable.game_of_thrones);
for(String name : file_maps.keySet()){
TextSliderView textSliderView = new TextSliderView(this);
// initialize a SliderLayout
textSliderView
.description(name)
.image(file_maps.get(name))
.setScaleType(BaseSliderView.ScaleType.Fit)
.setOnSliderClickListener(this);
//add your extra information
textSliderView.bundle(new Bundle());
textSliderView.getBundle()
.putString("extra",name);
mDemoSlider.addSlider(textSliderView);
}
mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Accordion);
mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
mDemoSlider.setCustomAnimation(new DescriptionAnimation());
mDemoSlider.stopAutoCycle();
mDemoSlider.addOnPageChangeListener(this);
}
Is there any way to stop infinite loop on sliders. I read that I should add mDemoSlider.stopAutoCycle(); but this does not have any effect.
Sorry, but you can't. At least not with this lib.
I've looked into the code and I saw that this "infinite scrolling" is default behavior, and if you want to disabled it you have either to implement your own slider, or suggest and edit for the original author...
The "problem" is on those two methods of the SliderLayout.java class:
/**
* move to next slide.
*/
public void moveNextPosition(boolean smooth) {
if (getRealAdapter() == null)
throw new IllegalStateException("You did not set a slider adapter");
mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1, smooth);
}
// ...
/**
* move to prev slide.
*/
public void movePrevPosition(boolean smooth) {
if (getRealAdapter() == null)
throw new IllegalStateException("You did not set a slider adapter");
mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1, smooth);
}
And the setCurrentItem is located inside the ViewPagerEx.java class, where you can see that this infinite scrolling is default behavior. (Look at the setCurrentItemInternal method).
void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int velocity) {
if (mAdapter == null || mAdapter.getCount() <= 0) {
setScrollingCacheEnabled(false);
return;
}
if (!always && mCurItem == item && mItems.size() != 0) {
setScrollingCacheEnabled(false);
return;
}
if (item < 0) {
item = 0;
} else if (item >= mAdapter.getCount()) {
item = mAdapter.getCount() - 1;
}
final int pageLimit = mOffscreenPageLimit;
if (item > (mCurItem + pageLimit) || item < (mCurItem - pageLimit)) {
// We are doing a jump by more than one page. To avoid
// glitches, we want to keep all current pages in the view
// until the scroll ends.
for (int i=0; i<mItems.size(); i++) {
mItems.get(i).scrolling = true;
}
}
final boolean dispatchSelected = mCurItem != item;
if (mFirstLayout) {
// We don't have any idea how big we are yet and shouldn't have any pages either.
// Just set things up and let the pending layout handle things.
mCurItem = item;
triggerOnPageChangeEvent(item);
requestLayout();
} else {
populate(item);
scrollToItem(item, smoothScroll, velocity, dispatchSelected);
}
}