Rohit
Rohit

Reputation: 2681

Create Gallery Like layout

I want to develop an app like this. Which is exactly like android gallery. So I tried with HorizontalScrollView. But still I am facing some problems

enter image description here

Here is my layout.

<ImageView 
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/hScrollView"/>

<HorizontalScrollView
android:id="@+id/hScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    
    </LinearLayout>
    
</HorizontalScrollView>

and here is my code

LinearLayout image;
ImageView main;

ImageView[] iv=new ImageView[images.length];
int i;

and in Activity,

for(i=0;i<images.length;i++)
    {
        iv[i]=new ImageView(this);
        
        LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        iv[i].setLayoutParams(lparams);
        iv[i].setImageResource(images[i]);
        
        iv[i].setTag(images[i]);
        
        image.addView(iv[i]);
        
        iv[i].setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                main.setImageResource((Integer) iv[i].getTag());    
            }
        });
        

But it is always giving me ArrayIndexOutOfBoundException. I know Value of i is getting updated for each iteration. So What am I supposed to do? Any alternative ways are appreciable.

I am using api level 18 so Gallery won't work.

Upvotes: 0

Views: 308

Answers (4)

Sonali8890
Sonali8890

Reputation: 2005

Try this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/hScrollView"
    android:background="@drawable/ic_launcher" />

<HorizontalScrollView
    android:id="@+id/hScrollView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    </LinearLayout>
</HorizontalScrollView>

</RelativeLayout>

In Activity class:

int[] drawable = { R.drawable.background, R.drawable.forground,
        R.drawable.ic_launcher, R.drawable.ic_launcher,
        R.drawable.ic_launcher, R.drawable.ic_launcher };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    centerImageView = (ImageView) findViewById(R.id.imageView);

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
    for (int i = 0; i < drawable.length; i++) {
        LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        ImageView view = new ImageView(this);
        view.setLayoutParams(lparams);
        view.setId(i);
        view.setImageResource(drawable[i]);
        linearLayout.addView(view);

        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                        centerImageView.setImageResource(drawable[v.getId()]);
            }
        });

    }
}

Hope will help you:)

Upvotes: 1

GrIsHu
GrIsHu

Reputation: 23638

Please note: android.widget.Gallery is no longer supported. Other horizontally scrolling widgets include HorizontalScrollView and ViewPager from the support library.

As per your requirement the HorizontalScrollView is more suitable. Check out the below tutorial which will help you in implementation.

Implement HorizontalScrollView like GalleryView

Upvotes: 1

Hariharan
Hariharan

Reputation: 24853

Try this..

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >



    <ImageView
        android:id="@+id/ImageView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

     <Gallery
        android:id="@+id/examplegallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

JAVA

 private Integer[] Imgid = {

        R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7

};

 ImageView imgView = (ImageView)findViewById(R.id.ImageView01);
imgView.setImageResource(Imgid[0]);

Gallery gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));

gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
imgView.setImageResource(Imgid[position]);
}
});

Reference :

http://saigeethamn.blogspot.in/2010/05/gallery-view-android-developer-tutorial.html

http://learnandroideasily.blogspot.in/2013/07/android-gallery-view-example.html

http://www.androidpeople.com/android-gallery-imageview-example

http://www.androidhive.info/2013/09/android-fullscreen-image-slider-with-swipe-and-pinch-zoom-gestures/

Upvotes: 1

Ravind Maurya
Ravind Maurya

Reputation: 977

you can use like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:orientation="horizontal"
    android:padding="5dp" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:scrollbars="horizontal" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

Upvotes: 1

Related Questions