Get the name of image used in ImageButton - android

I have an Integer array containing the id of finite number of images. I have an ImageButton in which I am embedding an image randomly from the above array once the ImageButton is clicked. Since the images are embedded randomly, I want to know the name of the image which is currently applied to the ImageButton every time the ImageButton is clicked.
minSDK=21 and Device API Level=29
Here is the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_game);
Integer[] imageIds={R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five,R.drawable.six};
final ImageButton btn= findViewById(R.id.dice);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Random gen = new Random();
int randomImg = imageIds[gen.nextInt(imageIds.length)];
btn.setImageResource(randomImg);
}
});
}

you can get the name of the resource from the resource id in this way:
String name = context.getResources().getResourceEntryName(randomImg);

Related

Randomly select one of two ImageViews and update ResourceImage

I have created in my layout two ImageViews, let's call them imageviewTop and imageviewBottom.
I saved two images into the drawable (green_image.png and red_image.png).
I also added a button and want I would like to do is, when the button is clicked, one of the ImageViews will get selected randomly and from the green_image it will change to the red_image.
I already tried with creating a switch/case statement and generating a random number, like 1 or 2.
Based on this number the case statement would update either the top or bottom image.
This is working fine for 2 ImageViews, but in case I would have 100, I would need to create 100 cases in code.
I am searching for a more dynamic option.
I know how to update the image for the ImageView, I am struggling with the part, on how to select one ImageView randomly, if it is possible.
Here is the code:
public class MainActivity extends Activity {
ImageView imagevieTop, imageviewBottom;
Button randomButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imagevieTop = (ImageView) findViewById(R.id.imageViewTop);
imageviewBottom = (ImageView) findViewById(R.id.imageViewBottom);
randomButton = (Button) findViewById(R.id.buttonRandom);
randomButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// randomly select one of the two imageviews
// for example: randomly selected imageviewTop
// set imageresource red_image to imageviewTop
//at next start up it would select either top or bottom, 50%-50% and then assign the image to it
}
});
}
}
You could just use one ImageView, and randomize the picture you draw.
Alternatively, you could adjust this to use an array of ImageViews. Your choice.
The line you want, though, is int index = random.nextInt(imgs.length); to get a random index from the list.
public class MainActivity extends Activity {
int[] imgs = new int[] { R.drawable.green_image, R.drawable.red_image };
Button randomButton;
private final Random random = new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imgView = (ImageView) findViewById(R.id.imageView);
randomButton = (Button) findViewById(R.id.buttonRandom);
randomButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int index = random.nextInt(imgs.length);
// randomly select one of the two drawables
int resId = imgs[index];
// set imageresource imgView
Drawable d = getResources().getDrawable(resId);
imgView.setImageDrawable(d);
}
});
}
}
To handle the "100 ImageViews" problem, I'd recommend not copying 100 lines of code, and instead looping over reasonable ID values.
List<ImageView> imgViews = new ArrayList<ImageView>();
for (int i = 0; i < 100; i++) {
int resId = getResources().getIdentifier("imgView" + i, "id", getPackageName());
ImageView nextImg = (ImageView) findViewById(resId);
imgViews.add(nextImg);
}
In case you want to do a dynamic selection of "n" ImageView elements, then you'll need to store them in a data structure (e.g. an array, a list, etc.). For example this code will select a random ImageView from a list:
public ImageView getRandomImageView(final List<ImageView> imageViewList) {
final Random random = new Random();
//The "nextInt" method works in the half-open range [0, n), so it'll never be equal to the list size.
final int randomElement = random.nextInt(imageViewList.size());
return imageViewList.get(randomElement);
}
In your case, with two ImageView's ("imagevieTop" and "imageviewBottom") declared in fixed variables then you would need to pass them to a list or something similar in order to select one of them dynamically.

How would I change my imageView src with one button but, a few images at random?

I want to make a simple app (in Android studio) that can change the image with one button and every button press a different images goes on top of it at random. I only know how to change one photo with only one button with this code
thatImage.setImageResource(R.drawable.myimage);
But I don't know how to have multiple images ready to replace it when the user presses the button and a random. So if anyone can help me out with this I would appreciate it and thank you for the support :)
You create an array of images, and then use random number to get one of them:
int[] myImageList = new int[]{R.drawable.image1, R.drawable.image2...};
Random random = new Random();
Integer rand = random.nextInt(myImageList.length - 1) + 0;
thatImage.setImageResource(myImageList[rand]);
Strings.xml
<array name="myImages">
<item>#drawable/a</item>
<item>#drawable/b</item>
<item>#drawable/c</item>
<item>#drawable/d</item>
<item>#drawable/e</item>
</array>
for the main activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button)findViewById(R.id.btn);
final RelativeLayout background = (RelativeLayout) findViewById(R.id.back);
Resources res = getResources();
final TypedArray myImages = res.obtainTypedArray(R.array.myImages);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Random random = new Random();
int randomInt = random.nextInt(myImages.length());
int drawableID = myImages.getResourceId(randomInt, -1);
background.setBackgroundResource(drawableID);
}
});
}

Changing Imageview on button dynamically

In my application, I have button and ImageView.
Here when i press button i want to change ImageView. I have 5 images in my drawable folder. On press button ImageView changes images one by one based on button click. I want it's solution.
Grateful to anyone that can help.
Maintain an array of image ids and inside onClick, set images using id from the array, then increment index.
Eg:-
ArrayList<Integer> ids=new ArrayList<Integer>();
ids.add(R.drawable.image1);
ids.add(R.drawable.image2);
ids.add(R.drawable.image3);
ids.add(R.drawable.image4);
ids.add(R.drawable.image5);
Int index=0
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(index<ids.size()){
imageview.setImageResource(ids.get(index));
index++;
}
else index=0;
}
});
As #Nizam said just maintain an array of id and load dinamically the image in the onClick(). Instead of the Random use a field variable and increment it. Be careful to the array length!
final int[] ids = new int[] { R.drawable.img1, R.drawable.img2 };
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int randomId = new Random().nextInt(ids.length);
ImageView imageView = (ImageView) findViewById(R.id.imageview);
imageView.setImageDrawable(getResources().getDrawable(randomId));
}
});

How to change TextView multiple times on multiple Button clicks?

I'm kind of new when it comes to Android Application development and I'm developing an app at the moment. I'm trying to get my TextView change every time the user clicks the Button(NEXT) and when another Button (PREVIOUS) gets clicked on I want it to change back to the original TextView. So basically I'd like to set up a certain amount of TextViews and be able to browse through them with the two Buttons I mentioned.
So far I only know how to make the the TextView change one time on a Button(NEXT) click. I'm using this piece of code for that:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton Next = (ImageButton) findViewById(R.id.Next);
Next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView Text1= (TextView) findViewById(R.id.Text1);
Text1.setText("New Text");
}
});
NOTE: The Button "PREVIOUS" isn't included yet because I didn't know what to do with it yet.
I'm getting the feeling this code is only used when you want the TextView to change one time and you need a whole different method to make it change multiple times.
I hope I provided you with enough information and you are willing to help me out here.
Thanks in advance!
public class MyActivity extends Activity implements View.OnClickListener {
int stringIdList[] = {R.string.text1, R.string.text2, R.string.text3, R.string.text4}
int stringListCounter = 0;
TextView text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton next = (ImageButton) findViewById(R.id.Next);
ImageButton previous = (ImageButton) findViewById(R.id.Previous);
text1 = (TextView) findViewById(R.id.Text1);
Next.setOnClickListener(this);
previous.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int id = v.getId();
if(id == R.id.Next && stringListCounter < stringIdList.length - 1) {
stringListCounter++;
} else if (id == R.id.Previous && counter > 0) {
stringListCounter--;
}
Text1.setText(stringIdList[stringListCounter]);
}
What this does is assigns your Activity to an OnClickListener to handle the click events. If Next was pressed and the counter is within the range of the array list, it will increase the counter. The same for previous. At the end of the click, it will set the text to whatever the ID is. This assumes your strings are in a strings.xml file which is recommended in the Android spec and is static.
I think you can store history as List and have all states of textview in each moment.
Only thing that you have to do is to take previous value from this history stack after pressing previous button.

How to change specify previous clicked image button view when the 2nd click button is occur

I have doubt in when I click the 2nd time image button, I want to change the both button image view in simultaneously.
Example:
1. first time press the button 1 and change already the first time image view. (get work)
2. second time press the other button 2 and I want change image view for the both button 1 and 2 in simultaneously. But I can only get the ibutton as variable signal to change the button 2 image view and button 1 can't get.
Question:
1. How do I change the button image view for button 1 when I click the button 2?
2. How do I can keep the button variable in array?
My code like this:
public class CheckersTest extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
final ImageView iv_new_game = (ImageView) findViewById(R.id.new_game);
iv_new_game.setOnClickListener(welcome_listener);
}
OnClickListener welcome_listener = new View.OnClickListener() {
public void onClick(View v) {
final ImageView iv = (ImageView) v;
if (iv.getId() == R.id.new_game) {
setContentView(R.layout.checkers_board);
final ImageButton b2 = (ImageButton) findViewById(R.id.imageButton2);
final ImageButton b4 = (ImageButton) findViewById(R.id.imageButton4);
final ImageButton b6 = (ImageButton) findViewById(R.id.imageButton6);
// set the OnClickListeners.
b2.setOnClickListener(button_listener);
b4.setOnClickListener(button_listener);
b6.setOnClickListener(button_listener);
// Re-enable the Click-able property of buttons.
b2.setClickable(true);
b4.setClickable(true);
b6.setClickable(true);
}
};
};
OnClickListener button_listener = new View.OnClickListener() {
public void onClick(View v) {
ImageButton ibutton = (ImageButton) v;
ibutton.setImageResource(R.drawable.green_bol);
}
};
In the second Button's onclick method get instance of first button and set image.

Categories

Resources