Eclipse Displays Image (EDI)


EDI:   SOLD       




In this chapter we display an image using the ImageView widget. What will the result be like? Look at Figure 4.1 below.


Figure 4.2

1. Run Enclipse, create a new Project (Figure 4.2).


Figure 4.3

2. Fill in the parameters as follows

| Project name     | GambarAndroid      |
|------------------|--------------------|
| Build Target     | Android 2.2        |
| Application name | Menampilkan Gambar |
| Package name     | com.gb             |
| Create Activity  | TampilanGambar     |
| Min SDK version  | 8                  |

3. Create a new folder named drawable in the /res folder. To do this, right-click the res folder > new > folder (Figure 4.4)


Figure 4.4. Adding a new folder

4. Copy a PNG or JPG image and paste it into the drawable folder. Right-click the drawable folder > paste. This project uses a PNG format image measuring (250 x 275) px (Figure 4.6).


Figure 4.5. image file.png


Figure 4.6. /res/drawable folder hierarchy

4. Type the following code in the main.xml layout.

<?xml version="1.0" encoding="utf-8"?>
     <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/imageView"
     android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:src="@drawable/gambar">
</ImageView>

5. Make sure the code in the image display.java is as follows

package contoh.tampilanGambar;
import android.app.Activity;
import android.os.Bundle;
public class tampilanGambar extends Activity {
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
     }
 }

6. Do RUN and see the results. Easy right?

Program Explanation

Note the main.xml, the ImageVIew widget has an attribute android:src="@drawable/image" . This attribute is used to point to the location where the image is stored, usually in the drawable folder.

In this way, we don't need to change the line of code in the Imageview.java because by default it calls the XML layout that we have created via setContentView(R.layout.main).

Now we will change the icon of this simple application without coding. Look at figure 4.7


Figure 4.7. Changing the launcher icon


Figure 4.8. Icon.png


Figure 4.9

Prepare a 70px x 72px icon.png image (image 4.8), then paste this icon.png into the res/drawable-lpi folder. Run the application, at this point the icon has changed. You may also insert images into the res/drawablehpi and res/drawable-mpi folders. Yup! It's very easy and you can definitely do it.


Post a Comment

Previous Next

نموذج الاتصال