Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Created June 15, 2015 18:24
Show Gist options
  • Star 49 You must be signed in to star a gist
  • Fork 29 You must be signed in to fork a gist
  • Save udacityandroid/bd550cc8fd37190d85a6 to your computer and use it in GitHub Desktop.
Save udacityandroid/bd550cc8fd37190d85a6 to your computer and use it in GitHub Desktop.
Android for Beginners : Simplified ImageView class
/**
* Displays an image, such as an icon.
*/
public class ImageView extends View {
// Resource ID for the source image that should be displayed in the ImageView.
private int mImageId;
// Context of the app
private Context mContext;
/**
* Constructs a new ImageView.
*/
public ImageView(Context context) {
mImageId = 0;
mContext = context;
}
/**
* Sets the source image in the ImageView.
*
* @param imageId is the resource ID of the image to be displayed.
*/
public void setImage(int imageId) {
mImageId = imageId;
}
}
@Sam1301
Copy link

Sam1301 commented Jan 31, 2016

I think there is no setImage() method in the ImageView class. Its setImageResource() .

@Stannisseaworth
Copy link

Stannisseaworth commented Oct 11, 2016

They finally gave up on explaining stuff and started using jargons, like what is a constructor dude?! you never mentioned it before...
I knew this would happen at some point.

@Vitnere
Copy link

Vitnere commented Nov 4, 2016

@Stannisseaworth In class-based object-oriented programming, a constructor (abbreviation: ctor) in a class is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.

@rejero
Copy link

rejero commented Dec 31, 2016

@Stannisseaworth I know how it feels, had the same issue when I first started learning programming. Will try to explain it non-programming way. A constructor is like the laws of a society. Before you create the society (objects), you have to define the laws what you can eat, do, etc etc (parameters and arguments) and write them down on an official piece of paper (magic methods). The society would then run and revolved based on those pieces of paper and laws. This is why they are called constructors. Hope I got this right.

@ardakazanci
Copy link

ardakazanci commented Feb 3, 2017

Correction ;
public ImageView(Context context) { super(context); mImageId = 0; mContext = context; }

@krishsam02
Copy link

in place of setImage method there is src to set image.

@TrentonMGT
Copy link

TrentonMGT commented Jul 23, 2017

A constructor is a method that is called or invoked when the object is created. A class is just the blueprint of an object.

@Maxwe1
Copy link

Maxwe1 commented Feb 5, 2018

helpful ..thanks

@badrddinb
Copy link

Simple example helpful!

@alsafi988
Copy link

nice

@Maensh
Copy link

Maensh commented Mar 8, 2018

nice

@sal77ama
Copy link

nice

@chayanbobra13
Copy link

helpful

@Eduese
Copy link

Eduese commented May 21, 2018

Seems errors noticed by our predecessors have been corrected. Nice work

@tooptooptoop
Copy link

Thanks.

@moiyd
Copy link

moiyd commented Sep 29, 2018

thanks

@nurbijoy
Copy link

Thanks

@djalilo24
Copy link

TNX

@YassineOuardini
Copy link

thanks ♥

@avinabX
Copy link

avinabX commented Aug 11, 2019

please can anyone explain why the member variables they used were declared private access modifier
private int mImageId;

**private** Context mContext;

@LaszloLajosT
Copy link

please can anyone explain why the member variables they used were declared private access modifier
private int mImageId;

**private** Context mContext;

I copied the answer from the other site:

What is difference between Private and Protected?

Private get only used in his class , but protected can used in his class and in the same package and derived class

@faroukelhamdi
Copy link

Was here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment