I have a imageView, button inside an activity, 10 pictures that are named from stage1 to stage9.
I need you help to solve some problem
I would like to use a button click to change picture that is on imageView to the next one.
I mean, I want to press on the button, and image view shows stage2 image, I press the button again and stage3 picture will be shown.
I have done this using counter var which count number of clicks and then running if statment to see which picture should go next, but it is too long and it is not possible to have more or less pictures.
I would like to know is there a way to do this, and if possible please show me how.
Thanks
code
private void changeImage(int counter) {
if (counter == 1) {
image.setImageResource(R.drawable.stage2);
} else if (counter == 2) {
image.setImageResource(R.drawable.stage3);
} else if (counter == 3) {
image.setImageResource(R.drawable.stage4);
} else if (counter == 4) {
image.setImageResource(R.drawable.stage5);
} else if (counter == 5) {
image.setImageResource(R.drawable.stage6);
} else if (counter == 6) {
image.setImageResource(R.drawable.stage7);
} else if (counter == 7) {
image.setImageResource(R.drawable.stage8);
} else if (counter == 8) {
image.setImageResource(R.drawable.stage9);
}
}
bassically this is the code that I am using right now.
It works, but if I want to do it to be more dynamic.
You'll need to tell the vm to update -> 'invalide()'.
Or use Picasso,
Picasso.with(Statics.context).load(R.drawable.stageX).error(R.drawable.error_img).resize(width, height).priority(Priority.HIGH).into(image);
Just get resource id by name, use like,
private void changeImage(int counter) {
if(counter >= 1 && counter <= 9) // Always check counter value before accessing as resource id
{
int counterValue = counter+1;
int resourceId = Resources.getSystem().getIdentifier("stage"+counterValue, "drawable", this.getPackageName()); // Use application context to get package name
image.setImageResource(resourceId);
}
}
Related
I am creating a matching card application. That only allows the user to select two cards per round.
Where you see the bricks are where the buttons are and so i have added them to an arraylist, but im not sure how to make it so that they can only press 2 buttons until they press the guess button where it checks if they are a match.
https://pastebin.com/65NqJ0GK
private void card1ButtonActionPerformed(java.awt.event.ActionEvent evt) {
String temp = cards.get(0);
if (temp.equals("0")) {
card1Button.setIcon(a);
} else if (temp.equals("1")) {
card1Button.setIcon(b);
} else if (temp.equals("2")) {
card1Button.setIcon(c);
} else if (temp.equals("3")) {
card1Button.setIcon(d);
} else if (temp.equals("4")) {
card1Button.setIcon(e);
} else if (temp.equals("5")) {
card1Button.setIcon(f);
} else if (temp.equals("6")) {
card1Button.setIcon(g);
} else if (temp.equals("7")) {
card1Button.setIcon(h);
}
count++;
if (count == 1) {
c1 = Integer.parseInt(temp);
change[0] = 0;
} else if (count == 2) {
c2 = Integer.parseInt(temp);
change[0] = 0;
}
}
^^Here is an example of a button code.
Could someone show me some way to do it.
When user click Next button, it suppose to show next record store in database, but when I run the app, user have to click previous button to view next entry and the Next button didn't perform any activity.
public void onClick(View v) {
int buttonId = v.getId();
if (buttonId == R.id.buttonViewPrevious)
if (currentBoothArrayIndex < boothArrayList.size() - 1) {
displayBoothInformation(--currentBoothArrayIndex);
} else if (buttonId == R.id.buttonViewNext)
if (currentBoothArrayIndex < boothArrayList.size() - 1) {
displayBoothInformation(++currentBoothArrayIndex);
}
}
your if else construct is wrong. Try this:
if (buttonId == R.id.buttonViewPrevious){
if (currentBoothArrayIndex < boothArrayList.size() - 1) {
displayBoothInformation(--currentBoothArrayIndex);
}
}else if (buttonId == R.id.buttonViewNext){
if (currentBoothArrayIndex < boothArrayList.size() - 1) {
displayBoothInformation(++currentBoothArrayIndex);
}
}
I am making a blackjack game and was wondering if I could make it so that I had two different actions on a single button.
This is what I have so far on the hit button, but instead of having the second card show on a double click, I want it to show the second card when you press the button again.
public void hit(MouseEvent event) {
if (event.getClickCount() == 1) {
card5 = deck.dealCard();
pcard3.setImage(card5.getImage());
}
if (event.getClickCount() == 2) {
card6 = deck.dealCard();
pcard4.setImage(card6.getImage());
}
}
You can have an iterator whose value can be increased on each click. And for different values set different functionalities. See the code
int i =0;
public void hit(MouseEvent event) {
if (i%2== 0) {
card5 = deck.dealCard();
pcard3.setImage(card5.getImage());
} else if (i%2 == 1) {
card6 = deck.dealCard();
pcard4.setImage(card6.getImage());
}
i++;
}
I want to get the value that I enter from edit text to a text view *2 but the app crashes.
public void sub (View v)
{
int val=Integer.parseInt( e1.getText().toString());
if(val <= 1)
{
t1.setText("you havn't ran any kilos");
}
else if(val > 1) {
t1.setText(val*2);
}
}
}
try this.
t1.setText(val*2 + "");
because val is Integer type and setText method's param is String type.
Try
public void sub (View v)
{
int val=Integer.parseInt( e1.getText().toString());
if(val <= 1)
{
t1.setText("you havn't ran any kilos");
}
else if(val > 1) {
t1.setText(String.valueOf(val*2));
}
I know that this question has been asked before but I think I am really close to my solution and therefore I wish to do this way.
I am using a ListView and populating it with some data. Now, I am trying to populate it with some more data when the user reaches the bottom. This should continue as long as there are less than 100 items in the ListView. But the code below add all the data at once when I scroll it to the bottom for the first time. I guess there are tens of calls to my async method that loads more data.
#Override
public void onScroll(AbsListView absListView, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
scrollTask myscrolltask = new scrollTask();
int lastVisibleIndex = absListView.getLastVisiblePosition();
if(lastVisibleIndex != totalItemCount - 1)
{
System.out.print("lastVisibleIndex "+lastVisibleIndex);
System.out.print("totalItemCount-1 " + (totalItemCount-1));
myscrolltask.cancel(true);
return;
}
if(moreNewsAvailable == 0)
return;
if(totalItemCount > 75)
return;
Log.v("Total Item Count", String.valueOf(totalItemCount));
int lastItem = firstVisibleItem + visibleItemCount;
if(lastItem == totalItemCount && totalItemCount<=75 && moreNewsAvailable==1 && lastVisibleIndex != -1 && totalItemCount!=0) //Means that you have reached the bottom
{
Log.v("LastVisibleItemPosition", String.valueOf(lastVisibleIndex));
setProgressBarIndeterminateVisibility(true);
myscrolltask.execute("");
}
}
Is there anyway around? How can I make sure that only one call to the async is made when reaching bottom?
create a Boolean variable and set that variable to true in task execution and again set that Boolean to true on task completion.
Check if task is in progress than dont execute on scroll method.
if(!isLoading){
if(lastVisibleIndex != totalItemCount - 1)
{
System.out.print("lastVisibleIndex "+lastVisibleIndex);
System.out.print("totalItemCount-1 " + (totalItemCount-1));
myscrolltask.cancel(true);
return;
}
if(moreNewsAvailable == 0)
return;
if(totalItemCount > 75)
return;
}