align Gallery To Left - Android User Interface

Android examples for User Interface:Gallery

Description

align Gallery To Left

Demo Code


import android.content.Context;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.Gallery;

public class Main {

  public static void alignGalleryToLeft(Context context, Gallery gallery,
      int itemWidth, int spacing) {
    int galleryWidth = 300;
    // if (viewGroup != null)
    // galleryWidth = viewGroup.getWidth();
    int offset = 0;
    if (galleryWidth <= itemWidth) {
      offset = galleryWidth / 2 - itemWidth / 2 - spacing;
    } else {// w  w w  .j a va 2 s  . co  m
      offset = galleryWidth - itemWidth - 2 * spacing;
    }

    MarginLayoutParams mlp = (MarginLayoutParams) gallery.getLayoutParams();
    mlp.setMargins(-offset, mlp.topMargin, mlp.rightMargin, mlp.bottomMargin);
  }
}

Related Tutorials