Noaman Akram
Noaman Akram

Reputation: 3830

Show Multiple ImageViews Dynamically in Relative Layout Android

I am working in An Android App in which Relative layout is

  <ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="2dp"
    android:adjustViewBounds="true"
    android:background="@drawable/background" />
<RelativeLayout
    android:id="@+id/mymainlayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
      android:layout_centerVertical="true"
    >

</RelativeLayout>

my problem is I am unable to Draw all ImageViews programatically in this Relative Layout it is showing only one image. Here is my code

    for (int i = 0; i < realAnswer; i++) {

        arrayofImages[i] = new ImageView(this);
        arrayofImages[i].setImageResource(imageId[imageNumber]);
        arrayofImages[i].setId(i);
        if(i!=0)
        {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.LEFT_OF,  arrayofImages[i-1].getId());
        //arrayofImages[i].setLayoutParams(params);
        nn.addView(arrayofImages[i],params);
        }
    else
        {
            nn.addView(arrayofImages[i]);
        }

    }

here nn is my main layout

any help Please ???

Upvotes: 1

Views: 11243

Answers (3)

johntheripp3r
johntheripp3r

Reputation: 989

If you are using RelativeLayout just to hold the imageviews which you are adding dymanically, then use LinearLayout. That will solve your problem.

Upvotes: 1

Jitender Dev
Jitender Dev

Reputation: 6925

Your code is working fine except for

 params.addRule(RelativeLayout.LEFT_OF,  arrayofImages[i-1].getId());

RelativeLayout.LEFT_OF its keep on setting image to the left and you are unable to see it.

Replace above code with

params.addRule(RelativeLayout.RIGHT_OF,
                        arrayofImages[i - 1].getId());

This will display images in horizontal direction.

Updated your for loop like this

for (int i = 0; i < 5; i++) {

            arrayofImages[i] = new ImageView(this);
            arrayofImages[i].setImageResource(R.drawable.ic_launcher);
            arrayofImages[i].setId(i);
            if (i != 0) {
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
                params.addRule(RelativeLayout.RIGHT_OF,
                        arrayofImages[i - 1].getId());
                // arrayofImages[i].setLayoutParams(params);
                nn.addView(arrayofImages[i], params);
            } else {
                nn.addView(arrayofImages[i]);
            }

        }

Upvotes: 3

anand
anand

Reputation: 416

have a look this may be useful to you:

 public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageArra);
      ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
      myPager.setAdapter(adapter);
      myPager.setCurrentItem(0);
    }

    private int imageArra[] = { R.drawable.gallery_photo_1, R.drawable.gallery_photo_2,
     R.drawable.gallery_photo_3, R.drawable.gallery_photo_5,
     R.drawable.gallery_photo_6, R.drawable.gallery_photo_7,
     R.drawable.gallery_photo_8, R.drawable.ic_launcher };

   }
}

and ViewPagerAdapter:

public class ViewPagerAdapter extends PagerAdapter {

 Activity activity;
 int imageArray[];

 public ViewPagerAdapter(Activity act, int[] imgArra) {
 imageArray = imgArra;
 activity = act;
 }

 public int getCount() {
  return imageArray.length;
 }

 public Object instantiateItem(View collection, int position) {
   ImageView view = new ImageView(activity);
   view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
    LayoutParams.FILL_PARENT));
  view.setScaleType(ScaleType.FIT_XY);
  view.setBackgroundResource(imageArray[position]);
  ((ViewPager) collection).addView(view, 0);
 return view;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
 ((ViewPager) arg0).removeView((View) arg2);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
 return arg0 == ((View) arg1);
}

 @Override
 public Parcelable saveState() {
  return null;
 }
}

xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<android.support.v4.view.ViewPager
android:id="@+id/myfivepanelpager"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" >

</ImageView>
</android.support.v4.view.ViewPager>

</LinearLayout>

Upvotes: 0

Related Questions