how to programmatically create an imageView with id and src parameters?

user1905474 picture user1905474 · Dec 15, 2012 · Viewed 28.8k times · Source

The following works, but I want to eliminate the xml so that I can change the image programmatically: Java:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.p6_touch);
    ImageView floatImage = (ImageView) findViewById(R.id.p6_imageView);
    floatImage.setOnTouchListener(this);    
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background_black" >
    <ImageView
        android:id="@+id/p6_imageView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="matrix"
        android:src="@drawable/p6_trail_map" >
    </ImageView>
    <ImageView
          android:id="@+id/imageView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="bottom"
          android:onClick="showFlashlight"
          android:src="@drawable/legend_block24" />
</FrameLayout>

Answer

Sam picture Sam · Dec 15, 2012

In the documentation there is a table that maps XML to Java.

  • For instance: android:src is equivalent to setImageResource().

You will need to check the inherited table for attributes from any super class.

  • For instance: android:id is equivalent to setId().

width, height, and gravity are all set in a LayoutParams object and passed to setLayoutParams().

Understand that not every XML attribute has a matching Java method (and vica versa), but all of the attributes you are using do.


An Example, let's call this file activity_main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background_black" >
    <!-- We'll add this missing ImageView back in with Java -->
    <ImageView
          android:id="@+id/imageView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="bottom"
          android:onClick="showFlashlight"
          android:src="@drawable/legend_block24" />
</FrameLayout>

Now in our Activity:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Let's create the missing ImageView
    ImageView image = new ImageView(this);

    // Now the layout parameters, these are a little tricky at first
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT);

    image.setScaleType(ImageView.ScaleType.MATRIX);
    image.setImageResource(R.drawable.p6_trail_map);
    image.setOnTouchListener(this);    

    // Let's get the root layout and add our ImageView
    FrameLayout layout = (FrameLayout) findViewById(R.id.root);
    layout.addView(image, 0, params);
}