Adding Dynamically ImageViews to horizontal scrollview in android - android

I'm trying to show images dynamically by string as ID and want to display it in Horizontal scroll-view as i have 13 cards which can't be fit on screen
Here is my code its working fine but i want it in Horizontal Scroll View
int resID = getResources().getIdentifier(resourceName, "id", getPackageName());
ImageView im = (ImageView) findViewById(resID);
Context context = im.getContext();
int id = context.getResources().getIdentifier(resourceName, "drawable", context.getPackageName());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(im.getLayoutParams());
lp.setMargins(counter * 43, 0, 0, 0);//left,right,top,bottom
im.setLayoutParams(lp);
im.setImageResource(id);
im.setOnClickListener(this);
counter++;
}
mp = MediaPlayer.create(getApplicationContext(), R.raw.clicksound);
}![enter image description here][2]

As per your requirement I will suggest you to RecyclerView which comes with library which you will have to add and use RecyclerView with horizontal orientation refer to link below.
https://developer.android.com/training/material/lists-cards.html
Or you can do it dynamically as below
here what you can do add Linear horizontal layout inside scrollview say id is imgcontainer then you can do is
create image view dynamically and add to linear layout.
for(int i=0;i<10;i++)
{
//create imageview here and setbg
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher);
((LinearLayout) findViewById(R.id.imgcontainer)).addView(
imageView, i);
}

int[] image_array = new int[]{R.drawable.image1, R.drawable.image2, ... R.drawable.image10};
LinearLayout ll = (LinearLayout) findViewById (R.id.ll_images);
for (int i=0 ; i<10; i++){
ImageView img = new ImageView (this);
img .setBackgroundResource (image_array [i]);
ll .addView(img );
}

Related

Declare ImageView programmatically Android

How can I declare some ImageViews programmatically in oncreate method ? I want to have something like this:
for (int i =1 ; i<=10;i++){
ImageView image+i = (ImageView)findViewById(R.id.image+i);
}
I don't know if it is possible something like this, or you have to make it imageview by imageview.
ImageView[] imageViews = new ImageView[10];
for (int i =1 ; i<= 10; i++){
int id = getResources().getIdentifier("image"+i, "id", getPackageName());
imageViews[i - 1] = (ImageView)findViewById(id);
}
getIdentifier returns the resource's id associate to the name, the first parameter, and you could use an array to store the view's reference.
you can use something like this method
LinearLayout layout = (LinearLayout)findViewById(R.id.imageLayout);
for(int i=0;i<10;i++)
{
ImageView image+i = new ImageView(this);
image+i.setLayoutParams(new android.view.ViewGroup.LayoutParams(80,60));
image+i.setMaxHeight(20);
image+i.setMaxWidth(20);
// Adds the view to the layout
layout.addView(image);
}
go through this tutorial and this too.

Adding image buttons side by side dynamically to Liner Layout whose Orientation is vertical

I have the following code:
LinearLayout lyt = (LinearLayout)findViewById(R.id.mylayout);
for(i=0;i<=3 ;i++) {
ImageButton ib= new ImageButton(this);
BitmapDrawable imagebd;
ib.setClickable(true);
imageid = getResources().getIdentifier("drawable/" + image,null,getPackageName());
ib.setBackgroundResource(imageid);
lyt.addView(ib);
}
all the images are show vertically i want it to be horizontal
Try this code:
LinearLayout lyt = (LinearLayout) findViewById(R.id.mylayout);
lyt.setOrientation(LinearLayout.HORIZONTAL);
for(i=0;i<=3 ;i++) {
ImageButton ib= new ImageButton(this);
BitmapDrawable imagebd;
ib.setClickable(true);
imageid = getResources().getIdentifier("drawable/" + image,null,getPackageName());
ib.setBackgroundResource(imageid);
lyt.addView(ib);
}
Avoid setting attributes in your Java code when it can be done via XML. Try setting the orientation attribute in your ListView:
<LinearLayout
...
android:orientation="horizontal"
...>
</LinearLayout>
Try it like that:
LinearLayout lyt = (LinearLayout)findViewById(R.id.mylayout);
LinearLayout buttonsLinearLayout = new LinearLayout(context);
buttonsLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
for(i=0;i<=3 ;i++)
{
ImageButton ib= new ImageButton(this);
BitmapDrawable imagebd;
ib.setClickable(true);
imageid = getResources().getIdentifier("drawable/" + image,null,getPackageName());
ib.setBackgroundResource(imageid);
buttonsLinearLayout.addView(ib);
}
lyt.addView(buttonsLinearLayout);
That way, you still have your main vertical linearlayout, so you can put stuff under the buttons.
EDIT:
to use it for more than 1 row, i did a simple math calc...
LinearLayout lyt = (LinearLayout) findViewById(R.id.mylayout);
// calculate the number of rows needed
int numOfButtons = something you already have i guess;
// row layout
LinearLayout buttonsLinearLayout = new LinearLayout(context);
;
for (i = 0; i <= numOfButtons; i++) {
// for every 3 rows, create a new layout
// and add it to the main linear layout
if (i % 3 == 0) {
// create layout for a 3 button row
buttonsLinearLayout = new LinearLayout(context);
buttonsLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
// add the new row with 3 buttons to the main lineal layout
lyt.addView(buttonsLinearLayout);
}
ImageButton ib = new ImageButton(this);
BitmapDrawable imagebd;
ib.setClickable(true);
imageid = getResources().getIdentifier("drawable/" + image, null, getPackageName());
ib.setBackgroundResource(imageid);
buttonsLinearLayout.addView(ib);
}
}
not tested, let me know if that worked for you...

How to Programically set the height and width of an ImageView

i am Programically trying to add imageViews into a Linear Layout as follows:
LinearLayout.LayoutParams layoutParamsTV = new LinearLayout.LayoutParams(100, 100);
for( i = 0; i < 5; i++) {
LinearLayout llv = new LinearLayout(this);
llv.setOrientation(LinearLayout.VERTICAL);
ImageView row1 = new ImageView(this);
row1.getLayoutParams().height=50; // getting error here
}
How to set the height and width of the ImageView and i want to add one more image view right over this imageView , how to do that
Try this:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(640, 480); // width , height
row1.setLayoutParams(params); // row1 is your ImageView

Add images to a HorizontalScrollView

I want to create a HorizontalScrollView which reads the images from drawable folder. The name of the images are "image1" "image2" ... "image20". I donĀ“t know how I can use the numbers to read them. Here is what I have:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout sv = (LinearLayout) findViewById (R.id.images);
for (int i=1 ; i<20; i++){
ImageView iv = new ImageView (this);
iv.setBackgroundResource (R.drawable.image1);
sv.addView(iv);
}
}
You can do that in two ways.
First one is to create array with the id's of images you want to use and in your for cycle, just add the images to your layout:
int[] images = new int[]{R.drawable.image1, R.drawable.image2, ... R.drawable.image20};
LinearLayout sv = (LinearLayout) findViewById (R.id.images);
for (int i=0 ; i<20; i++){
ImageView iv = new ImageView (this);
iv.setBackgroundResource (images[i]);
sv.addView(iv);
}
Or the second way, you can create something similar to this:
for (int i=1 ; i<=20; i++){
String uri = "drawable/image"+i;
// int imageResource = R.drawable.image1;
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
ImageView iv = new ImageView (this);
iv.setBackgroundResource (imageResource);
sv.addView(iv);
}
I didn't tested the codes, but I think they should work.
If you wish to use drawables without an array list, you can do this:
getResources().getIdentifier("Name of the Drawable", "drawable", "Your Package Name");
So your code will be:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout sv = (LinearLayout) findViewById (R.id.images);
for (int i=1 ; i<20; i++){
ImageView iv = new ImageView (this);
int myImage = getResources().getIdentifier("image"+i, "drawable", "Your Package Name");
iv.setBackgroundResource(myImage);
sv.addView(iv);
}
}
A lot of examples like this show building a list of your images first. And then you can use your code and iterate through the list.
So something like
List<Drawable> imagesToAdd = Arrays.asList(R.drawable.image1,R.drawable.image2, .... R.drawable.image20);
Then you can even use the foreach loop to iterate through this.
for (Drawable image in imageToAdd) {
etc...
}

android- Placing two programatically created imageView next to each other in a RelativeLayout

I have created two imageViews promatically as shown below:
public void createImageViews(Integer count){
ImageView[] imageViewArray = new ImageView[count];
for (int i = 0; i < count; i++ ) {
imageViewArray[i] = new ImageView(getBaseContext());
imageViewArray[i].setId(i); // unique property for every imageView
if(i==0){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
imageViewArray[i].setLayoutParams(params);
imageViewArray[i].setBackgroundResource(imagesForIv[i]);
_UIRLParent.addView(imageViewArray[i]);
Log.v("first", "first"+i);
}
else if(i < 3){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i].getId());
imageViewArray[i].setBackgroundResource(imagesForIv[i]);
_UIRLParent.addView(imageViewArray[i],params);
Log.v("second", "second"+i);
}
}
I just need to place the second imageView toRightOf first imageView. Can someone help me. This is eating away a lot of my time.
try https://stackoverflow.com/a/5191159/1436931
you are using wrong index values.
at line
params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i].getId());
you aligning current image RIGHT of current image. :)
you need to track the index of last imageView id i.e left Image view
you need something like this
params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i-1].getId());

Categories

Resources