Showing posts with label ImageView. Show all posts
Showing posts with label ImageView. Show all posts

How to Instantiate a New ImageView

ImageView was added in API Level 1

1. Declare an ImageView.

2. After you declare the ImageView object, add this line below to instantiate it. This will instantiate a new ImageView named myImageView.

myImageView = new ImageView(this);

3. Compile and run!

How to Declare a New ImageView, in Java

ImageView was added in API Level 1
Displays an arbitrary image, such as an icon. The ImageView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the image so that it can be used in any layout manager, and provides various display options such as scaling and tinting.

1. Add the below import to the imports section.

import android.widget.ImageView;

2. Add the below line to the onCreate method. This will create a new ImageView object named myImageView.

ImageView myImageView;

3. Compile and run!

Resources:
developer.android.com/reference/android/widget/ImageView.html

findViewById - Find an ImageView using ID

How to Find an ImageView using ID

findViewbyId was added in API Level 1

findViewById: Look for a child ImageView with the given id. If this view has a given id, return this ImageView. The findViewById() method is available to all activities in Android. findViewById allows you to find a view inside an activity layout.

1. Name an ImageView myImageView.

2. Declare an ImageView, named myImageView.

3. In MainActivity, java file, add the below code to the onCreate method. Where R.id.myImageView is the same as the android:id name in your main.xml file, and myImageView is the ImageView name referred to in Java. This returns a ImageView View class and is casted to a ImageView type so you can work with it.

myImageView = (ImageView) findViewById(R.id.myImageView); 

4. Compile and run!

Resources:

android:id - Name an ImageVIew

How to Name an ImageView

android:id was added in API Level 1
This is a unique identifier name for the ImageView, so you can refer to the ImageView later in your project.

1. Add a ImageView in your main.xml file.

2. In the main.xml file, add the code below.

  <ImageView
        ...
       android:id = "@+id/myImageView"
  />

3. The "+" character is required if you have not yet defined that id, if perhaps the element you reference is lower in the .xml file.

Otherwise you if it's already defined above (or included) then you can omit the "+" char:

4. Compile and run!

Next Recommended Article: Find an ImageView using ID

Resources:
http://developer.android.com/reference/android/view/View.html#attr_android:id
http://developer.android.com/reference/android/R.attr.html#id
http://android-wtf.com/2012/11/difference-between-at-plus-id-and-at-id-in-android/
http://stackoverflow.com/questions/5025910/difference-between-id-and-id-in-android
http://stackoverflow.com/questions/5048586/can-you-set-tab-order-in-xml-layout

Add an ImageView to an Activity

ImageView was added in API Level 1

1. Create an Android project, if you don't already have one.

2. Add a Drawable to your Project named myimage

3. Open the main.xml file and add the below code to add an ImageView to the XML file between the Layout tags.

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/myimage" />

</LinearLayout>
4. Compile and run!

Next Recommended Article: Name an ImageView

Resources:
http://developer.android.com/reference/android/widget/ImageView.html