| Index | Android apps - Pictures |
| Put picture files in your app |
| Display picture |
| Change picture |
| Generate View name |
| Generate Drawable name |
Before using a picture or image, you must put the picture file physically inside your app. Find the file on your computer, in Windows Explorer. Copy it (ctrl C or right-click/copy). Go to AndroidStudio and your app. Click on app/res/drawable and paste it (ctrl V or right-click/paste or Edit/paste). Do this for all the picture files you want. The name stays the same. What I do is (using Windows Explorer) create a folder called "My files" inside the app folder (so I can find it again!) Then I put all my image files in there, and rename them to sensible names, if they weren't already. That makes it easy to copy them across, or find them to change them (and then copy across). Remember, it's the version in app/res/drawable which are used by your app, so remember to copy again after changing the files!
In app/res/layout/activity_main.xml, code an ImageView:
Click here for more info about formatting the screen in app/res/layout/activity_main.xml.
This ImageView has "android:src" of "@drawable/star". This means that there must be a file called "star.png" in app/res/drawable.
So, design your picture on some picture producing software, and save it as "star.png" somewhere. Go into Windows Explorer (or My Computer), find the file, and copy it. Return to AndroidStudio. Click on app/res/drawable. Paste in the copied file. A message box will come up asking if you're sure, so say yes.
If you want to change this picture on the screen (perhaps in the method "changePix" called by the "onClick"), you do it like this, in app/java/com.(the project name)/MainActivity (in Java):
That has changed the picture to a different picture file called "empty.png". So you need to have that file in app/res/drawable as well.
In app/res/layout/activity_main.xml, code an "android:id" in the "RelativeLayout":
Also code an "android:tag" in the "ImageView". THis only makes sense if you do it in several of them! (Otherwise, why not use the id explicitly?)
In app/java/com.(the project name)/MainActivity (in Java), set up the name of the ImageView tag that you want to access, and do so, like this.
In app/java/com.(the project name)/MainActivity (in Java), code the above in order to get the ImageView you want. Then generate the drawable name you want and change the image, as below.
So this code defines and sets up "mapName".
"bit" was defined in the above section. This code followed on from it.
If the ImageView is inside a grid, then you need to be a bit cleverer. Here the generated file name is "fileName". "v" is the ImageView. You don't need the tag name business in this case, as grids kindly hand over the ImageView for you to use directly.
However, it may be easier to have an array shadowing the screen, and another array with possible images, and then reset up the whole grid!
© Jo Edkins 2016 - Index to Android app index