I'm trying to do something that should be simple, import png files, use them in a number of imageview widgets (as the background). As part of my code I would like to see that when widget a is clicked, widget b is updated with the background of widget a.
It seems like the type of info I should be able to pass without much effort, and all the help out there just says "import into Drawable folder", but earlier this year Android studio stopped putting imported image assets into the drawable folder, and started putting them into a mipmap folder. I'm able to use these images as backgrounds, as long as I define the background in the activity_mail.xml design view, but I'm unable to interact with the imageview backgrounds in my code, because all the methods are expecting references to type drawable, but my images are not interpreted as drawables, because i can't get them into that folder. What am I missing?
First I'm creating an array with the separate resource ids, in the hopes that I can use them in my destination imageview. I'm not sure if filling the array with the mipmap image resource is working, since I can't seem to work with any image that isn't a drawable in my code, but it works for audio files elsewhere in my code. I've put descriptions into the tag field that could also be used to reference the image used, but I couldn't get that working either.
public class MainActivity extends AppCompatActivity {
int[] letters = new int[26];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int i = 0;
for (char alphabet = 'a'; alphabet <= 'z'; alphabet++) {
String ltr = String.valueOf(alphabet);
ltr = "letter"+ltr;
letters[i] = this.getResources().getIdentifier(ltr,"mipmap",this.getPackageName());
i++;
}
}
}
The following method is called when an imageview is touched. I'm trying to take the description from the imageview background, and update the background of another imageview, using the png files I have in my mipmap folder, references to which I am (theoretically) storing in an array.
public void onClickLetter(View v) {
String actualLetter;
int letternumber;
String letterclicked = v.getContentDescription().toString();
letterclicked = letterclicked.replace("letter", ""); //this gives me a number, which I parse to an int below, which allows me to select a resource ID stored in my array, this is working perfectly elsewhere in my code
System.out.println(letterclicked);
actualLetter = (v.getTag()).toString();
System.out.println(actualLetter);
letternumber = parseInt(letterclicked); //I should be able to use the letters[letternumber] array reference now, but I can't seem to pass it to anything since it's not a drawable file.
how do I pass the reference to the image file in my source imageview widget (the View v parameter in this method), and update a second imageview widget with an image file as the background, whose reference is stored in an array?
Is there a setbackground option that doesn't expect a drawable file?
If I should be using the drawable folder, please tell me how to add png files to the drawable folder.
Is my problem just from using imageview rather than buttons, or some other type of widget?
Is my problem the way I'm storing the mipmap resource ids (which is where the image assets were placed by android studio automatically)? I know mipmap files are for icons only, but as I mentioned earlier this year android studio started putting all images into the mipmap folder.
There is an answer in here that solves the problem. Switch the view to project, and you can use the drawable folder. Are all images in Android apps considered Icons?