download image and swap image



package com.quickplaysport.download2;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {
private long enqueue;
private DownloadManager dm;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {


// this broadcast reveiver means it has to be on button click
BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Query query = new Query();
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c

// if the download successfull then swap the image
if (DownloadManager.STATUS_SUCCESSFUL == c
.getInt(columnIndex)) {

ImageView view = (ImageView) findViewById(;
String uriString = c

Uri myDownloadedFile = dm.getUriForDownloadedFile(downloadId);

registerReceiver(receiver, new IntentFilter(

public void onClick(View view) {
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(
enqueue = dm.enqueue(request);


public void showDownload(View view) {
Intent i = new Intent();

// try getting the uri of the downloaded file and setting it to a textview

reading data from internal storage

public void read(View view){
String myFilename = “freekick”;
String readData = “”;



public  void openFile(String myFilename) {
String value = “”;
FileInputStream fis;

try {
fis = openFileInput(myFilename);
byte[] input = new byte[fis.available()];
while( != -1){
value += new String(input);
} catch (FileNotFoundException e) {
} catch (IOException e) {
TextView myTextView = (TextView) findViewById(;


Setting an image views background image from java

within some function


// get the view that will be changed
        ImageView myImageView = (ImageView)findViewById(;
        Drawable new_image= getResources().getDrawable(R.drawable.changed);



the first line:

     ImageView myImageView = (ImageView)findViewById(;

ImageView is creating a reference object of type Imageview

myImageView is the variable name for the new reference object

(ImageView) is a caster i.e. what the data will be put into

findViewById() gets the View referenced in the brackets


the second line:

Drawable new_image= getResources().getDrawable(R.drawable.changed);

Drawable – the type of reference object we’re creating

new_image – my name for the reference object


gets the object to reference from the repository


third line:


myImageView – my reference object

setBackgroundDrawable(new_image); – command to change the background image on the object referenced by myImageView, it changes it to new_image

reading data from tag

to read the data from a views tag use this code in bold:

public void passdata(View view) {

 String tag = (String) view.getTag();


*the first String defines the variable type

 tag is just the name of my variable

(String) – this is a caster it is the variable type the data from the tag is cast into

view in the example is the name of the view from the start of the function.


No need for anything in the brackets as it knows which tag to get because it is the tag that invoked the function


Setting text to a view from within an action

To change the text within a view from the java class do this:


// declairing the text view to change

        TextView textElement = (TextView) findViewById(;

//leave this line to assign a specific text


Alternatively use this to change it to a string from the resources folder

//leave this line to assign a string resource

Passing data to an action

To send data from one action to another you need to use extras on intents


In the first action add this

public void passdata(View view) {
String myText = “Some Text”;
// Do something in response to button
Intent myIntent = new Intent(this, Two.class);
  myIntent.putExtra(“name_of_data_to_retreive”, myText (The string or variable to send);



Then in the action you need the data in onCreate.

Intent intent = getIntent();
        String MyVariable= intent.getExtras().getString(“name_of_data_to_retreive“);


The string is now inside MyVariable


Now see setting the text within a view for one use of this method