i want to change my image, if i have this code:
ImageView img = new ImageView(this);
img.setImageResource(R.drawable.my_image);
that code works, but i want save R.drawable.my_image in one variable
let's say listofimage , it because i must change the name of image file
let's say i have "my_image", "my_image1", "my_image2"
i try to change the code like this
ImageView img = new ImageView(this);
string listofimage="";
if(result1.equals("my_image")){
listofimage="R.drawable.my_image";
} else if (result1.equals("my_image1")) {
listofimage="R.drawable.my_image1";
}else if (result1.equals("my_image2")) {
listofimage="R.drawable.my_image2";
}
img.setImageResource(listofimage);
but that code didn't work. can anyone help me?
With your code you could do like this:
ImageView img = new ImageView(this);
if(result1.equals("my_image")){
img.setImageResource(R.drawable.my_image);
} else if (result1.equals("my_image1")) {
img.setImageResource(R.drawable.my_image1);
}else if (result1.equals("my_image2")) {
img.setImageResource(R.drawable.my_image2);
}
You code contains three compilation errors:
String with lowercase "s"
R.drawable.my_image is of type int
setImageResource expects int as its parameter
Solution is to replace "string" with "int" in your code to look like this:
ImageView img = new ImageView(this);
int listofimage=R.drawable.my_image;
img.setImageResource(listofimage);
Related
I'm using bypass lib to parse a markdown string to show inside a TextView.
What basically this lib does is to parse all strings and build an SpannedString to show this inside a TextView. I've debuged all the lib code and aren't able to find what is doing wrong in a reasonable time. Is there someone that have been faced the same problem and can help me?
String markdownStr = "# Testing Markdown\n" +
"\n" +
"![surf](http://www.adesl.pt/images/outras-provas/surf.jpg)";
TextView markdownTxtView = (TextView) findViewById(R.id.markdown);
Bypass bypass = new Bypass(getApplicationContext());
CharSequence charSequence = bypass.markdownToSpannable(markdownStr, new Bypass.ImageGetter() {
#Override
public Drawable getDrawable(String source) {
//TODO: get drawable from source
Drawable drawable = getApplicationContext().getResources().getDrawable(R.drawable.test);
return drawable;
}
});
markdownTxtView.setText(charSequence);
by adding bellow code it works:
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
Picasso Image Getter
https://github.com/Commit451/BypassPicassoImageGetter
Glide Image Getter
https://github.com/victorlaerte/BypassGlideImageGetter
Hello I'm having problem reading random files on Eclipse, Android programming...
At assets folders I have a folder called "fytyra" and there are jpg pictures with mumbers like 1.jpg 2.jpg...
I tried to debug the app, list was null always.. ???
//Gets a random number from 0 to 431,.. I think it is !
private int nasiqim (){
int i = 0;
Random nasiqimi = new Random();
i=nasiqimi.nextInt(431);
return i;
}
//This need to fill list with integers and to not repeat the same number...
private void listaEFotove(){
int nse=-1,i=0,numriRandom;
while (nse<0){
numriRandom = nasiqim();
nse = Arrays.binarySearch(numrat, numriRandom);
if (nse <0 ){
numrat[i] = numriRandom;
i++;
}
if (i == 11)break;
}
}
This is to fill imageView with photos...
private void merrFytyrat(){
int fotoNr=0;
String emri;
listaEFotove();
for (int i=0; i<11;i++){
fotoNr=numrat[i];
if (i==0){
try
{
emri = Integer.toString(fotoNr);
// get input stream
InputStream ims = getAssets().open( "fytyra/" + emri +".jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
i11.setImageDrawable(d);
}
catch(IOException ex)
{
return;
};
}
else if (i==1){ ... Continues until 12...
I got the answer, thank you greenapps, for trying to help mee...
My mistake was on declaration of array... so my new code is this :
int[] numrat = new int[12];
it was like this :
int[] numrat = null;
another mistake was on the getAssets, like greenapps told me
getAssets().open( "/fytyra/" + emri +".jpg");
now thanks to him i changed that code to :
getAssets().open( "fytyra/" + emri +".jpg");
No everything works perfect,.,
This site is the best, Thank you...
I am designing a Image Puzzle(JigSaw) game. I am stuck in a problem that, when I start the game it shows the option "New Image" but, when I click on that, it gives an error message, means images from the drawable folder are not loading.
I did research and i came with this, i have implemented this in my code. but, it gives an error.. "Could not Load."
protected void newImage()
{
LinkedList<Uri> availableImages = new LinkedList<Uri>();
findInternalImages(availableImages);
if(availableImages.isEmpty())
{
Toast.makeText(this, getString(R.string.error_could_not_load_image), Toast.LENGTH_LONG).show();
return;
}
int index = new Random().nextInt(availableImages.size());
loadBitmap(availableImages.get(index));
}
protected void findInternalImages(List<Uri> list)
{
Field[] fields = R.drawable.class.getFields();
for(Field field: fields)
{
//String name = field.getName();
//if(name.startsWith(FILENAME_IMAGE_PREFIX))
//{
//int id = getResources().getIdentifier(name, "drawable", getPackageName());
for(int j=1;j<13;j++)
{
Drawable drawable = getResources().getDrawable(getResources()
.getIdentifier("image_"+j, "drawable", getPackageName()));
}
//}
}
}
My Problem is,
when i run the program it should show images from drawable folder and when i choose the one then actual scrambling should start. so, one can play the jig saw game.
Start by checking that your images are in a Android compatible format, that was causing me problems to begin with. I normally stick to .png now.
This is the code you will need to modify the image
ImageView pwrSwap = (ImageView) beaconitems.findViewById(R.id.yourImageID);
pwrSwap.setImageResource(R.drawable.yourImage);
http://developer.android.com/guide/appendix/media-formats.html
I'm writing a Robolectric unit test and I need to make an assertion that an ImageView had setImageResource(int) called on it with a certain resource ID. I'm using fest-android for assertions but it doesn't appear to contain this assertion.
I also tried to get the ShadowImageView from Robolectric for the ImageView because I know it used to give you access to this, but it's now gone.
Lastly, I tried to call setImageDrawable in my code instead of setImageResource, then in my test assert like this:
assertThat(imageView).hasDrawable(resources.getDrawable(R.drawable.some_drawable));
but this also fails, even though the failure message clearly shows it's the same Drawable being loaded.
For Background
ImageView imageView = (ImageView) activity.findViewById(R.id.imageview);
assertEquals(R.drawable.expected, Robolectric.shadowOf(imageView.getBackground()).getCreatedFromResId());
For Drawable
ImageView imageView = (ImageView) activity.findViewById(R.id.imageview);
assertEquals(R.drawable.expected, Robolectric.shadowOf(imageView.getDrawable()).getCreatedFromResId());
From Roboelectric 3.0+
This is how you can do:
int drawableResId = Shadows.shadowOf(errorImageView.getDrawable()).getCreatedFromResId();
assertThat("error image drawable", R.drawable.ic_sentiment_dissatisfied_white_144dp, is(equalTo(drawableResId)));
I ended up extending fest-android to solve this:
public class CustomImageViewAssert extends ImageViewAssert {
protected CustomImageViewAssert(ImageView actual) {
super(actual);
}
public CustomImageViewAssert hasDrawableWithId(int resId) {
boolean hasDrawable = hasDrawableResourceId(actual.getDrawable(), resId);
String errorMessage = String.format("Expected ImageView to have drawable with id <%d>", resId);
Assertions.assertThat(hasDrawable).overridingErrorMessage(errorMessage).isTrue();
return this;
}
private static boolean hasDrawableResourceId(Drawable drawable, int expectedResId) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();
ShadowBitmap shadowBitmap = (ShadowBitmap) shadowOf(bitmap);
int loadedFromResourceId = shadowBitmap.getCreatedFromResId();
return expectedResId == loadedFromResourceId;
}
}
The magic sauce is:
ShadowBitmap shadowBitmap = (ShadowBitmap) shadowOf(bitmap);
int loadedFromResourceId = shadowBitmap.getCreatedFromResId();
which is Robolectric specific, so I can't submit a pull request to fest-android with this.
With kotlin extensions:
fun ImageView.hasDrawable(resId: Int): Boolean =
Shadows.shadowOf(this.drawable).createdFromResId == resId
fun ImageView.hasBackground(resId: Int): Boolean =
Shadows.shadowOf(this.background).createdFromResId == resId
Usage:
assertTrue(image.hasDrawable(R.drawable.image_resource))
I'm currently trying to start a thread which fetches a drawable from its resources and posts it after a set amount of time. Then fetches the next drawable from resources and does the same thing all over.
final ImageView zImageView = (ImageView) findViewById(R.id.zzz_animation_view);
new Thread(new Runnable() {
public void run() {
for(int i = 1; i<=510; i++)
{
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.z_animation0001); // how do I increment this recourse to z_animation0002, 0003, ..., 0510 ?
zImageView.postDelayed(new Runnable() {
public void run() {
zImageView.setImageBitmap(bitmap);
}
},200);
}
}
}).start();
Firstly. Is this a proper way of approaching the problem? The drawables are too large for me to use animation-list and as such my goal is to load the images one at a time to ensure that I have enough memory. Secondly how do I solve the problem of iterating over my resources?
You can get resource id by its name using Resources.getIdentifier :
final int current = 5;
final String pattern = "z_animation%04d";
final String name = String.format(pattern, current);
final int id = getResources().getIdentifier(name, "drawable", getPackageName());
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), id);
Also, here you can find my answer for the same question with code sample that does exactly what you want.
I would put the images in the assets folder and load them from there as described here: https://xjaphx.wordpress.com/2011/10/02/store-and-use-files-in-assets/
In the assets folder you can just add a number to the file name.