Gallery items become off-center when switching into landscape orientation.

Posted by Sandile on Stack Overflow See other posts from Stack Overflow or by Sandile
Published on 2011-01-14T22:49:56Z Indexed on 2011/01/14 22:53 UTC
Read the original article Hit count: 413

Filed under:

Hey Guys, Thanks for your time.

I'm writing an application for android 2.2 and I'm using a gallery to circulate through a list of images that the user can then click to navigate other portions of the application. In portrait orientation, everything is kosher: the images are displaying correctly (completely in the center of the screen as I intended); however, in landscape orientation, all the images in the gallery are strangely displaced a fixed distance to the left of the center of the screen. I've been researching this issue for hours now and haven't found a solution. I'm hoping that some android guru can aid me in the resolution of this bizarre rendering issue.

MainMenuActivity.java relevant code snippet (the activity in which the gallery resides)

Gallery optionList = (Gallery)findViewById(R.id.mainMenuOptionList);
GalleryItem[] optionListItemIdArray = new GalleryItem[] { 
        new GalleryItem(R.drawable.icon_main_menu_option_list_blackboard, "Start Next Lesson",
                        "Start the next planned vocab lesson."),
        new GalleryItem(R.drawable.icon_main_menu_option_list_index_cards, "Review Words", 
                        "Manually look over old words."),
        new GalleryItem(R.drawable.icon_main_menu_option_list_dice, "Play Vocab Games",
                        "Play games to reinforce knowledge."),
        new GalleryItem(R.drawable.icon_main_menu_option_list_calendar, "View Lesson Plan", 
                        "View the schedule for coming lessons."),
        new GalleryItem(R.drawable.icon_main_menu_option_list_pie_chart, "View Performance Report",
                        "Evaluate your overall performance statistics."),
        new GalleryItem(R.drawable.icon_main_menu_option_list_settings, "Manage Settings", 
                        "Change and modify application settings.") 
    };
optionList.setAdapter(new GalleryImageAdapter(this, optionListItemIdArray));

NOTE: A GalleryItem is a POJO with an image resource id, title and description.

GalleryImageAdapter.java:

public class GalleryImageAdapter extends BaseAdapter {
private Context context;
private GalleryItem[] galleryItemArray;

public GalleryImageAdapter(Context context, GalleryItem[] galleryItemArray) {
    this.context = context;
    this.galleryItemArray = galleryItemArray;
}

@Override
public int getCount() {
    return galleryItemArray.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(context);
    imageView.setImageResource(galleryItemArray[position].getImageId());
    imageView.setScaleType(ImageView.ScaleType.FIT_START);
    imageView.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.WRAP_CONTENT, Gallery.LayoutParams.WRAP_CONTENT));

    return imageView;
}
}

Portrait version of main_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainMenuLinearLayout"
android:background="#3067a8"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
    android:id="@+id/mainMenuHeadingTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="10dip"
    android:textColor="#ffffff"
    android:textSize="50sp"
    android:text="@string/main_menu_heading"
    android:shadowColor="#555555"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="2"
   />
<TextView
    android:id="@+id/mainMenuSubHeadingTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="15dip"
    android:textColor="#ffffff"
    android:textSize="15sp"
    android:text="@string/main_menu_sub_heading"
    android:shadowColor="#555555"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="2"
   />
<Gallery 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainMenuOptionList"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="20dip"
    android:paddingBottom="10dip"
    android:spacing="40dip"
/>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainMenuOptionListDetailLinearLayout"
    android:gravity="center_vertical|center_horizontal"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
        android:id="@+id/mainMenuOptionListLabelTextView"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textColor="#ffffff"
        android:textSize="25sp"
        android:shadowColor="#555555"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="2"
    />
    <TextView
        android:id="@+id/mainMenuOptionListDescriptionTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:textSize="15sp"
        android:shadowColor="#555555"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="2"
    />
</LinearLayout>
</LinearLayout>

Landscape version of main_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainMenuLinearLayout"
android:background="#3067a8"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
    android:id="@+id/mainMenuHeadingTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="10dip"
    android:textColor="#ffffff"
    android:textSize="50sp"
    android:text="@string/main_menu_heading"
    android:shadowColor="#555555"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="2"
   />
<TextView
    android:id="@+id/mainMenuSubHeadingTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="15dip"
    android:textColor="#ffffff"
    android:textSize="15sp"
    android:text="@string/main_menu_sub_heading"
    android:shadowColor="#555555"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="2"
   />
<Gallery 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainMenuOptionList"
    android:layout_width="fill_parent"
    android:layout_height="128dip"
    android:paddingLeft="0dip"
    android:paddingTop="5dip"
    android:spacing="10dip"
/>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainMenuOptionListDetailLinearLayout"
    android:gravity="center_vertical|center_horizontal"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
        android:id="@+id/mainMenuOptionListLabelTextView"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textColor="#ffffff"
        android:textSize="25sp"
        android:shadowColor="#555555"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="2"
    />
    <TextView
        android:id="@+id/mainMenuOptionListDescriptionTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:textSize="15sp"
        android:shadowColor="#555555"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="2"
    />
</LinearLayout>
</LinearLayout>

Thank you for your time!

© Stack Overflow or respective owner

Related posts about android