Home >> Miscellaneous
In this article I shall be writing about my experience with Android SDK
and Platform APIs. How I have used ImageView to show a user defined
Image on screen with LinearLayout.
Before starting to write code for this example I shall be referring to
Google's Official web-site for setting up environment for Android SDK
and Graphical user interface for designing screen layout and showing-up
Views.
|
|  |
|
If an image is to be read from project resource folder and shown on screen,
then one way is to place the desired image under the res/drawable folder.
On building of this project, one can observe a static class that is generated
within class file "R" as drawable and in this class file, there will be a
static int field generate with a name same as the name of the image file itself.
For example, if the image file name is cupboards.png and this image file is
placed in the drawable folder, then one can use corresponding id
"R.drawable.cupboards" as image resource for the image view instance.
Following source code depicts a simple demonstration of usage of ImageView
in an Application using Activity.
ExampleImageView.java
....
....
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imView1 = new ImageView(this);
imView1.setImageResource(R.drawable.cupboards);
ViewGroup.LayoutParams viewLayoutParam =
new ViewGroup.LayoutParams(20, 20);
addContentView(imView1, viewLayoutParam);
}
....
....
|
Taking this example one step further, and modified this example to create a List
with multiple items containing different images within each item with Vertical
positions.
For this example to work we might have to place two images under drawable folder
within res folder in the Android project.
Android layout XML file can be very simple with a single list view with id as
"list" , as shown below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
|
and the source code of the example activity as follows:
ExampleImageView.java
package com.techienjoy.example.listview;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class ExampleImageView extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ExampleAdapter(this));
}
private class ExampleAdapter extends BaseAdapter {
private Context context;
public ExampleAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return 2;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView
, ViewGroup parent) {
ImageView imView1 = null;
if(position == 0) {
imView1 = new ImageView(this.context);
imView1.setImageResource(R.drawable.cupboards);
}
if(position == 1) {
imView1 = new ImageView(this.context);
imView1.setImageResource(R.drawable.camerascategory);
}
return imView1;
}
}
}
|
In this very example I havn't tried using convertView and Parent attribute
from the getView overridden method inside a private class within the
main Activity class file.
The overridden getView method as marked in green color code as shown above,
does nothing more than just returning different ImageViews based on the
position int type argument that is being passed to this method as runtime.
position of value '0' means first item in the ListView, and position of
value '1' means second item in the same ListView. This ListView is being
already available through the Android layout XML file with a ListView of
id '@android:id/
list'.
On successful compilation and execution of this program would show output
as shown in the image below:

Note:- Please ignore application header shown on the image, as it could be
different base on specific devlopment environent and configuration.
If you would like to share your thoughts on this, please write to us @
usingframeworks @ gmail dot com
If anything missed out , please let me know at
techienjoy at yahoo . com
| Android ListView Example : |
Example on Android ListView
explained with a very simple scenario
whereby showing folder and files with
structure and appropriate screens
captured and shown.
|
|
|
|
|
|
|
|
|
|
|
| Android ListView Example : |
Example on Android ListView and
explained with a very simple scenario
and article with appropriate screens
captured and shown.
|
|
|
|
|
|
|
|
|
|
|
| Android ListView Example : |
Example on Android List View
explained with a very simple scenario
and article with appropriate screens
captured and shown.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Android Sensors Example : |
Example on Android Sensors Listed and
explained with a very simple scenario
and article with appropriate screens
captured and shown.
|
|
| Android Examples : |
List of ANDROid examples
with source code and output
screens captured and shown.
|
|
|
| Google GWT Example : |
Example using GWT and some design patterns and various
ways of implementing this example.
|
|
| Android Tab View Example : |
Example on Android Tab View
explained with a very simple scenario
and appropriate screens captured and shown.
|
|
| Android Gallery Example : |
Example on Android Gallery View
explained with a very simple scenario
and appropriate screens captured and shown.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|