Index

Android apps - Pictures


Put picture files in your app
Display picture
Change picture
Generate View name
Generate Drawable name

Put picture files in your app

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!


Display picture

In app/res/layout/activity_main.xml, code an ImageView:

<ImageView android:id="@+id/starPix" android:src="@drawable/star" android:onClick="changePix" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/descStar" android:layout_below="@id/instr" android:layout_centerHorizontal="true" />

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.


Change picture

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):

ImageView star = (ImageView) findViewById(R.id.starPix); star.setImageResource(R.drawable.empty);

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.


Generate View name

In app/res/layout/activity_main.xml, code an "android:id" in the "RelativeLayout":

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:id="@+id/brummap" >

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?)

<ImageView android:id="@+id/brum11" android:tag="brum11" android:src="@drawable/brum0101" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/mapDesc" android:onClick="moveLeftUp" />

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.

// set up ImageView tag name (called 'screenName' here) String screenName = getString(R.string.nameBrum); screenName = screenName + String.format("%01d", i+1); screenName = screenName + String.format("%01d", j+1); // use tag name RelativeLayout rl = (RelativeLayout) findViewById(R.id.brummap); ImageView bit = (ImageView) rl.findViewWithTag(screenName);

Generate Drawable name

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.

// set up Drawable name (called mapName here) String mapName = getString(R.string.nameBrum); mapName = mapName + String.format("%02d", i+x); mapName = mapName + String.format("%02d", j+y); // use Drawable name int id = getResources().getIdentifier(mapName, "drawable", getPackageName()); bit.setImageResource(id);

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.

int resrcId = getResources().getIdentifier(fileName, "drawable", getPackageName()); ((ImageView) v).setImageResource(resrcId);

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