4

I am using Gallery like this

<Gallery
    android:id="@+id/gallery1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:spacing="2dp" >
</Gallery>

but when I am running the code I am finding gallery starting from middle and I want to start it from left. what should I do for this please help me.

Jignesh Ansodariya
  • 12,583
  • 24
  • 81
  • 113

6 Answers6

7

A structure describing general information about a display, such as its size, density, and font scaling.To access the DisplayMetrics members, initialize an object like this

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        Gallery g = (Gallery) findViewById(R.id.gallery);

        // set gallery to left side
        MarginLayoutParams mlp = (MarginLayoutParams) g.getLayoutParams();
        mlp.setMargins(-(metrics.widthPixels / 2 + (imageWidth/2)), mlp.topMargin,
                    mlp.rightMargin, mlp.bottomMargin);
anupam sharma
  • 1,705
  • 17
  • 13
2

just set the selection of Gallery to next, which resemble that gallery is in Left position.

Gallery mGallery= (Gallery) findViewById(R.id.gallery);
mGallery.setSelection(1);

then continue with your normal work :)

Ahmed Ekri
  • 4,601
  • 3
  • 23
  • 42
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
2

You should use setSelection(1) but it's important to place it after setAdapter(). In another case it doesn't work.

Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new GalleryImageAdapter(this));
gallery.setSelection(1);
KaroCodes
  • 246
  • 2
  • 13
1

What you need to use is .setSelection not .setSelected, example below

Gallery gallery= (Gallery) findViewById(R.id.gallery);

gallery.setSelection(1);

David
  • 111
  • 1
  • 5
0

Set left margin of the Gallery to the negative integer (say -80 dip). To properly calculate you would check for the screen width at the run time and then given your item (image) width you will do something like:

    int offset = width/2 - itemWidth/2; // you may add your spacing here too
    MarginLayoutParams mlp = (MarginLayoutParams) getLayoutParams();
    mlp.setMargins(-offset, mlp.topMargin, mlp.rightMargin, mlp.bottomMargin);
Bostone
  • 36,858
  • 39
  • 167
  • 227
0

Try this it will work.....

Gallery g=(Gallery)findViewById(R.id.gallery1);

MarginLayoutParams mlp=(MarginLayoutParams)g.getLayoutParams();

mlp.setMargins(-200, 0, 0, 0);

Pranav Singh
  • 17,079
  • 30
  • 77
  • 104