Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Created June 15, 2015 18:24
Show Gist options
  • Select an option

  • Save udacityandroid/bd550cc8fd37190d85a6 to your computer and use it in GitHub Desktop.

Select an option

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;
}
}
@Vitnere

Vitnere commented Nov 4, 2016

Copy link
Copy Markdown

@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

rejero commented Dec 31, 2016

Copy link
Copy Markdown

@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

ardakazanci commented Feb 3, 2017

Copy link
Copy Markdown

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

@krishsam02

Copy link
Copy Markdown

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

@TrentonMGT

TrentonMGT commented Jul 23, 2017

Copy link
Copy Markdown

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

Maxwe1 commented Feb 5, 2018

Copy link
Copy Markdown

helpful ..thanks

@badrddinb

Copy link
Copy Markdown

Simple example helpful!

@alsafi988

Copy link
Copy Markdown

nice

@Maensh

Maensh commented Mar 8, 2018

Copy link
Copy Markdown

nice

@sal77ama

Copy link
Copy Markdown

nice

@chayanbobra13

Copy link
Copy Markdown

helpful

@Eduese

Eduese commented May 21, 2018

Copy link
Copy Markdown

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

@tooptooptoop

Copy link
Copy Markdown

Thanks.

@moiyd

moiyd commented Sep 29, 2018

Copy link
Copy Markdown

thanks

@nurbijoy

Copy link
Copy Markdown

Thanks

@djalilo24

Copy link
Copy Markdown

TNX

@YassineOuardini

Copy link
Copy Markdown

thanks ♥

@avinabX

avinabX commented Aug 11, 2019

Copy link
Copy Markdown

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

**private** Context mContext;

@LaszloLajosT

Copy link
Copy Markdown

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

@felhamdi

felhamdi commented May 9, 2020

Copy link
Copy Markdown

Was here!

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