0

I've a single vertical linear layout with a scrollview inside it.

Programmatically I'm adding some thing inside it

  • A TextView, and it's OK: I'm able to center it using

    LayoutParams params = new LinearLayout.LayoutParams (
    LayoutParams.FILL_PARENT,
    LayoutParams.WRAP_CONTENT
    );
    
    ...
    
    monthNameTextView.setLayoutParams(params);
    monthNameTextView.setGravity(Gravity.CENTER_HORIZONTAL);
    
  • Then I add a horizontal LinearLayout. It's OK

    gallery = new LinearLayout(this);
    
    gallery.setOrientation(LinearLayout.HORIZONTAL);
    gallery.setGravity(Gravity.CENTER_HORIZONTAL);
    gallery.setLayoutParams(params);
    
  • Then I add 3 ImageView loading images froms disk

    Bitmap myJpg = BitmapFactory.decodeFile(imgFile.getAbsolutePath());  
    ImageView cover = new ImageView(this);
    cover.setImageBitmap(myJpg);
    gallery.addView(cover);
    

Images are loaded, are three, and are centered into linear layout.

The problem is that there is no spacing from one image the the following one.

I'm new and I'm trying to understand difference from layout_weight and weight, and I'm here to ask you how to set these parameters programmatically to have a simple centered set of three images with 'some' spacing beetween each of them.

double-beep
  • 5,031
  • 17
  • 33
  • 41
realtebo
  • 23,922
  • 37
  • 112
  • 189
  • 1
    You should consider using 'Margin' for some space between views. 'weight' is all about size distribution of sibling views inside a (linear)layout. – yrajabi Sep 08 '12 at 08:22
  • can you explain the difference from weight and layout-weight ? Also: how set this property programmatically ? – realtebo Sep 08 '12 at 08:24
  • I tryed magParams.setMargins(10, 10, 10, 10); but in this way there is space from top and space from bottom, but NO SPACE beetween images – realtebo Sep 08 '12 at 08:34

2 Answers2

2

Change your code for adding ImageViews to this:

Bitmap myJpg = BitmapFactory.decodeFile(imgFile.getAbsolutePath());  
ImageView cover = new ImageView(this);
cover.setImageBitmap(myJpg);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(YOUR_DESIRED_SPACE_VALUE, 0, 0, 0); // 4 margin values for Top/Left/Right/Bottom
gallery.addView(cover, llp);
yrajabi
  • 637
  • 1
  • 9
  • 28
  • `LayoutParams magParams = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ); magParams.setMargins(10, 10, 10, 10); ` I just tried this, but in this way there is space from top and from bottom but not beetween images – realtebo Sep 08 '12 at 08:40
  • 1
    @realtebo OK, I edited my answer, it seems we should pass the LayoutParam when adding ImageView to its parent. – yrajabi Sep 08 '12 at 08:46
  • Opps ! i did a mistake. I'm appling the layout with border to linear layout instead of applying to single images. When apllied to images, all was working, thanks ! – realtebo Sep 08 '12 at 08:48
1

Is there a need to do layout in your code? If number of Images you want to display is always three, just create an XML layout file and then dynamically set image for them.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" 
        android:layout_margin="5dp"/>

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" 
        android:layout_margin="5dp"/>

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" 
        android:layout_margin="5dp"/>

</LinearLayout>

And then in your code:

ImageView iv1 = (ImageView) findViewById(R.id.imageView1);
iv1.setImageBitmap(YOUR_BITMAP);
// ...
Ali Behzadian Nejad
  • 8,804
  • 8
  • 56
  • 106
  • It's a wonderfull way I didn't know of it ! How to set this layout dinamically ? Images are three every time, but the title+3 images are added 'n' times. So loading a layout from a file and simple setting images will be a interesting way – realtebo Sep 08 '12 at 09:28