Reputation: 7164
I'm trying to write such a code : I have a string, and if string is "stack", I want to print s.gif, t.gif, a.gif, c.gif and k.gif. Here is the code that I wrote :
String myString = "stack";
for(int i = 0; i < myString.length(); i++)
{
String source= null;
if(myString .charAt(i) == 'a')
{
source = "R.drawable.folder.a.gif";
}
else if (myString .charAt(i) == 'b')
{
source = "R.drawable.folder.b.gif";
}
...//it goes until z
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new AbsListView.LayoutParams(
AbsListView.LayoutParams.MATCH_PARENT,
AbsListView.LayoutParams.MATCH_PARENT));
ImageView imageView = new ImageView(this);
imageView.setImageResource();// I don't know what to write here
imageView.setLayoutParams(new AbsListView.LayoutParams(
AbsListView.LayoutParams.MATCH_PARENT,
AbsListView.LayoutParams.WRAP_CONTENT));
linearLayout.addView(imageView);
setContentView(linearLayout);
}
What I'm trying to do is set source string and give it to the setImageResource(), but I failed. Can you tell me how to fix this code? Thanks.
Upvotes: 0
Views: 230
Reputation: 4588
Change source
to int instead of String
int source;
Then use this
imageView.setImageDrawable(getResources().getDrawable(source))
Upvotes: 1
Reputation: 7082
First of all, Android has int-based resource pointers, so your source
variable must be an int
.
Then, simply assign it like this:
source = R.drawable.a
Put all your images in the res/drawable directory.
However, I am not sure if you will be able to use gifs like this without an library. Try it and if it does not work, use .pngs.
Upvotes: 2