Skip to content Skip to sidebar Skip to footer

Insert A View Dynamically In A Horizontalscrollview In Android

I'm developing an application for Android tablet 3.0 that has one activity that should be scrollable in the horizontal axis, like an e-book. For that, I'm using a RelativeLayout i

Solution 1:

HorizontalScrollViewscrollView= (HorizontalScrollView) findViewById(R.id.scrollView1);

        LinearLayouttopLinearLayout=newLinearLayout(this);
       // topLinearLayout.setLayoutParams(android.widget.LinearLayout.LayoutParams.FILL_PARENT,android.widget.LinearLayout.LayoutParams.FILL_PARENT);
        topLinearLayout.setOrientation(LinearLayout.HORIZONTAL); 

        for (inti=0; i < 15; i++){



            finalImageViewimageView=newImageView (this);

            imageView.setTag(i);

            imageView.setImageResource(R.drawable.ic_launcher);

            topLinearLayout.addView(imageView);

            imageView.setOnClickListener(newOnClickListener()
            {

                @OverridepublicvoidonClick(View v)
                {
                    // TODO Auto-generated method stub
                    Log.e("Tag",""+imageView.getTag());
                }
            });


        }

        scrollView.addView(topLinearLayout);


//      ImageView img=(ImageView)findViewById(R.id.imageView1);//      final ImageLoader imageLoader = ImageLoader.getInstance();//      //      //      //      ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())//      .threadPoolSize(3)//      .threadPriority(Thread.NORM_PRIORITY - 2)//      .memoryCacheSize(1500000) // 1.5 Mb//      .discCacheSize(50000000) // 50 Mb//      .httpReadTimeout(10000) // 10 s//      .denyCacheImageMultipleSizesInMemory()//      .enableLogging() // Not necessary in common//      .build();//  // Initialize ImageLoader with configuration.//  ImageLoader.getInstance().init(config);//      //      final DisplayImageOptions options = new DisplayImageOptions.Builder()//      .showStubImage(R.drawable.ic_launcher)//      .cacheInMemory()//      .cacheOnDisc()//      .build();//      //      imageLoader.displayImage("https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihJ-CIs4q97UTcw4HebF0nCwIxkmvBLf-LE9W9wu3FTBw7mTW9gse4El7qehoodAEzrgVnLrezVfGXZk21Q4SP3LO70_CiOlIUPj3D5O9Rx92iiTJtpt4X84Z97RK01aj2ES6GJTDvPdi0/s1600/android+logo1.jpg",img,options);//      //      img.setOnClickListener(new OnClickListener()//      {//          //          @Overridt//          public void onClick(View v)//          {//              // TODO Auto-generated method stub//              Dialog d =new Dialog(TestActivity.this);//              d.setContentView(R.layout.dialog);//          //              d.setCancelable(true);//              //              ImageView d_img=(ImageView)d.findViewById(R.id.dialog_img);////                d.setLayoutParams(L)//              imageLoader.displayImage("https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihJ-CIs4q97UTcw4HebF0nCwIxkmvBLf-LE9W9wu3FTBw7mTW9gse4El7qehoodAEzrgVnLrezVfGXZk21Q4SP3LO70_CiOlIUPj3D5O9Rx92iiTJtpt4X84Z97RK01aj2ES6GJTDvPdi0/s1600/android+logo1.jpg",d_img,options);//              //              d.show();//              }//      });

Solution 2:

Change the width of Relativelayout to wrap_content.

Try using this method to add the view.

voidaddView (View child, ViewGroup.LayoutParams params)

EDIT: Remove android:orientation="horizontal" from the HorizontalScrollView

Solution 3:

you add the view into the relativelayout not into the horizontalscrollview using parent

try with the horizontalscrollview object as you can done with the relativelayout

Solution 4:

Try this code.

ImageView imageView = new ImageView(this);
    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    imageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
    imageView.setBackgroundColor(Color.blue);


parent.addView(v,params);

Post a Comment for "Insert A View Dynamically In A Horizontalscrollview In Android"