Question

Actually the main layout is a relative layout which interns contains a relative layout in it. The main objective is to add images to the inner relative layout .

I tried this out but didn't succeed . This is my code :

import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.app.Activity;
import android.graphics.Point;

public class MainActivity extends Activity {

    Display display;
    Point point;
    int width;
    int height;
    Animation up, down, right, left;
    RelativeLayout rl, rl2;


    Integer[] pics = { R.drawable.img1, R.drawable.img2,R.drawable.img3, R.drawable.img4,
            R.drawable.img5, R.drawable.img6,R.drawable.img7, R.drawable.img8,
            R.drawable.img9, R.drawable.img10,R.drawable.img11, R.drawable.img12,
            R.drawable.img13, R.drawable.img14,R.drawable.img15};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Init();

    }

    private void Init() {
        // TODO Auto-generated method stub


        rl = (RelativeLayout) findViewById(R.id.Rlayout);
        rl.setGravity(Gravity.CENTER);
        int x = 0;
        int y = 0;

        for(int i = 1 ; i<16 ; i++ ){
            ImageView img = new ImageView(this);
            img.setImageResource(pics[i-1]);
            int tempx = img.getWidth();
            int tempy = img.getHeight();
            rl.addView(img, x, y);
            x += tempx;
            if(i%4 == 0){
                x = 0;
                y += tempy;
            }

        }
        rl.bringToFront();

        }
}

The XML code for the layout being used is :

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/R1Layout">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:id="@+id/Rlayout" >
    </RelativeLayout>

</RelativeLayout>

The output that i get is a blank screen .

Help me out with a solution how to display the images within the inner relative layout

Thanks in advance ,

Was it helpful?

Solution

I think you are misunderstanding, x and y are not the coordinates, are width and height respectively in

rl.addView(img, x, y);

and you can´t get the size of the imageviews with:

int tempx = img.getWidth();
int tempy = img.getHeight(); 

You must use a container to set size and coordinates to your imageview´s:

 RelativeLayout.LayoutParams params;
                params = new RelativeLayout.LayoutParams(100, 100);             
                params.leftMargin = x;
                params.topMargin = y;
                rl.addView(img,params);

this is an example of how to do it:

private void Init() {
    // TODO Auto-generated method stub

    int x = 0;
    int y = 0;
    rl = (RelativeLayout) findViewById(R.id.Rlayout);
    rl.setGravity(Gravity.CENTER);
      for(int i = 0 ; i<14 ; i++ ){
            ImageView img = new ImageView(this);
            img.setImageResource(pics[i]);
                //int tempx = img.getWidth();
            //int tempy = img.getHeight();           
            int tempx = 100;                
            int tempy = 100;
            //rl.addView(img, x, y);

            x += tempx;
            if(i%4 == 0){
                x = 0;
                y += tempy;
            }

            RelativeLayout.LayoutParams params;
            params = new RelativeLayout.LayoutParams(100, 100);             
            params.leftMargin = x;
            params.topMargin = y;
            rl.addView(img,params);



        }                       
    rl.bringToFront();

}

like Rajesh, i recommend too the use of a Gridview instead of RelativeLayout.

See this example:

GridView and excess space padding

OTHER TIPS

To insert images into the inner RelativeLayout, you need to use ImageView object. You can do it using the addView() method that is available to RelativeLayout (through inheritance from ViewGroup class). You can do it as follows within your activity (you have to decide where you want to place this):

RelativeLayout rl = (RelativeLayout) findViewById(R.id.Rlayout); // find inner layout
ImageView iview = new Image(this);
// Set image view parameters - refer to Android documentation for this
rl.addView(iview); // and so on. 
// You can add as many items as you want to any layout you want in this manner
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top