In my android app I pull data from http requests.
In my layout I want to show images dynamically with Glide library.
I get the image urls but I can not find a way to show them in my layout.
In my layout file I have:
<LinearLayout
android:id="#+id/social_media_layout"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="60dp"
android:gravity="center_vertical">
</LinearLayout>
Somehow I want to add imageviews(dynamically) in my LinearLayout.
For example: If I get 2 image urls from the request I need two imageviews in my linearlayout.
Note: I want to load images with Glide library.
Any idea would be helful.
Thanks.
You could make a new XML file for item_social_media.xml
For example
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp" >
<ImageView
android:id="#+id/social_media_icon"
/>
<!-- More customization as needed -->
</RelativeLayout>
And you can inflate that and add into the LinearLayout.
final Context context = MainActivity.this; // TODO: Get a context
// Inflate and update the view
View socialMediaItem = LayoutInflater.from(context).inflate(R.layout.item_social_media);
ImageView icon = (ImageView) socialMediaItem.findViewById(R.id.social_media_icon);
// TODO: Glide ... into(icon);
// Add to the LinearLayout
linearLayout.addView(socialMediaItem);
You should better use ListView or RecycleView . But if you insist on adding them into LinearLayout dynamically, then:
LinearLayout layout = (LinearLayout)findViewById(R.id.social_media_layout);
// assuming that image_urls_list contains the images urls
for(int i=0;i<image_urls_list.size();i++)
{
ImageView image = new ImageView(this);
// set whatever width and height you want to give
image.setLayoutParams(new android.view.ViewGroup.LayoutParams(100,100));
// load image using Glide
Glide.with(context).load(image_urls_list.get(i)).into(image);
// Adds the view to the layout
layout.addView(image);
}
You might want to add a HorizontalScrollView tag as a parent tag to your LinearLayout so that you can scroll horizontally.
Related
I am developing an Android app. In my app, I need to inflate list of views dynamically. I added them and working. The problem is with setting the width and height of layout. Now I will demonstrate my problem with a simple project. Actually, my project is much more complicated than this simple project.
I am inflating views to this layout.
<LinearLayout
android:orientation="horizontal"
android:id="#+id/cm_photos_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
I am looping through a list of bitmap and adding view dynamically as follow
for(Bitmap bmp : bitmaps)
{
View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,null);
ImageView previewImageView = (ImageView)preview.findViewById(R.id.item_cm_preview_image);
previewImageView.setImageBitmap(bmp);
container.addView(preview);
}
Please note, in the above code, container is a LinearLayout added dynamically to the parent XML in the above.
container = new LinearLayout(this);
container.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
container.setLayoutParams(params);
parentLinearLayout.addView(container);
This is my item_cm_preview_image.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_weight="1"
android:layout_height="400dp"
android:layout_width="0dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:scaleType="centerCrop"
android:id="#+id/item_cm_preview_image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
As you can see, I set the layout height to 400dp, width 0 and layout_weight to 1 in the XML. So all image height must be same and width must be equal because of layout_weight. But the result is not as expected. You can see screenshot below.
As you can see in the screenshot, both layout_weight and height are not working for all inflated views. But if I add extra ViewGroup dynamically and inflate the view to that layout, it is working. Below is my code
//This happening in for loop
LinearLayout wrapper = new LinearLayout(this);
wrapper.setLayoutParams(new LinearLayout.LayoutParams(0,500,1));
View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,null);
ImageView previewImageView = (ImageView)preview.findViewById(R.id.item_cm_preview_image);
previewImageView.setImageBitmap(bmp);
wrapper.addView(preview);
container.addView(wrapper);
This is the result:
As you can see both layout_weight and height working when I use an extra dynamic linear layout. Why is setting layout weight and height in XML not working? Why second way is working? How can I set weight and height in XML layout file? Is it possible?
If you inflate layout using method
View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,null);
it skip its width and height parameters..., but if you will use:
View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,parent,false);
it should work correct, for example if you inflate view in activity as parent you can provide (ViewGroup) getView():
View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,(ViewGroup) getView(), false);
I have several images in my mipmap folder and I want to add them to the screen at a specific location and then move them. Here is the code I thought should work:
ImageView item = new ImageView(this);
item.setImageResource(R.mipmap.ball);
Where ball is a valid png file. Why doesn't this code show the image?
i assumed that your xml is look like this.
Xml code is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layout">
</RelativeLayout>
And try this one in your class file(i.e in activity)
RelativeLayout layout=(RelativeLayout)findViewById(R.id.layout);
ImageView imageView=new ImageView(this);
imageView.setImageResource(R.mipmap.add_symbol);
//which adds the imageview to your layout
layout.addView(imageView);
As stated in the comments, you are creating an image view unattached to any view. The instance is there, but it doesn't know where to render.
If you know where you want to insert it, lets say, some LinearLayout with id image_container, you could do something like (and I'm being very simplistic about it!):
ImageView item = new ImageView(this);
item.setImageResource(R.mipmap.ball);
LinearLayout layout = activity.findById(R.id.image_container);
layout.addView(item)
You might also want to use item.setScaleType( ... ) in order to get the desired scaling.
That being said, please keep in mind that the proper way of adding several items programatically would be using something like a RecyclerView.
Also, in order to get a nice performance for you images loading and rendering, something like Glide or Picasso would be very useful.
I want to cast and ImageView to RelativeLayout. Because the RelativeLayout is the parent View and there are many images as Child views in this. So I have written code to download image and set it to the Image view. But the case here i want to set this downloaded image as background of the parent view that is of RelativeLayout.
RelativeLayout layout = (RelativeLayout) findViewById(R.id.bg_image);
ImageView bgImage=(ImageView) findViewById(R.id.bground_image);
downloader = new ImageDownloader();
downloader.download(item.imageurl(), bgImage);
/// Image is now set in bgImage//
///////// Now here i want bgImage to be set as a background of layout////
thankx in advance
To set a background image you can use something like
layout.setBackground(new BitmapDrawable(<your image>));
Best way I think is take one another Imageview with same dimensions as Relativelayout(may be fill parent) with scaletype fitxy and set the image to this as you are doing lazy loding and cant pass that relativelayout to loader....... this fitxy will work as background of parent layout
Or another way is change your downloder code to accept relative layout and set background once you have the image
Just add the image to the background of RelativeLayout. It should be 1st in order to be behind all other views in the layout. Then you can use it with code that expects an ImageView.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:id="#+id/an_image"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!--other views go here-->
</RelativeLayout>
And no, you can't cast between these types, obviously. This is Java!
No this is simply not possible because these are completely different types.
You can cast any RelativeLayout or ImageView to View, because this is the base class for all views.
But what you are trying to do should be a little different. You can inflate the ImageView from the RelativeLayout because it is the parent.
I will add a code snippet later.
Update :
// First inflate your layout
View layout = (View)findViewById(R.layout.relativelayout);
// Then inflate the imageview from the layout
ImageView imgview = (ImageView)layout.findViewById(R.id.bground_image);
// Then do your download logic with the imageview
downloader = new ImageDownloader();
downloader.download(item.imageurl(), imgview);
Let me know if this works ;).
I want to have a scroll bar in android showing each individual plant such that a user can press on one to select it, create a new Plant object he can then move around the screen.
I'm running into trouble populating this list.
I have a HorizontalScrollView:
bar = (HorizontalScrollView)findViewById(R.id.horizontalScrollView1);
The brunt of my code happens in
private LinearLayout loadBar(ArrayList<Plant> listOfPlants, HorizontalScrollView bar){
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setHorizontalScrollBarEnabled(true);
ImageView image= new ImageView(getApplicationContext());
ArrayList<View> images = new ArrayList<View>();
for (int i = 0; i < list.size(); i++) {
image = listOfPlants.get(i).getPhoto();
images.add(image);
}
layout.addTouchables(images);
return layout;
}
I want to add this linear layout to the scroll bar I'm creating but I don't see any way to do this. Or am I going about this the wrong way and is there a much simpler way to get what I want done?
You can take a ListView inside LinearLayout, that is inside horizontal scroll view as below:
<HorizontalScrollView ...........>
<LinearLayout ......>
<LinearLayout ......>
//List View Titles will be here
</LinearLayout>
<ListView ........ android:layout_weight="1" />
</LinearLayout>
</HorizontalScrollView>
Also, it is important to put layout_weight in the ListView. Hope it works for you. Also, you can check this customized horizontally scrollable listview tutorial.
I am new to android, and I am searching for a logic for grid view like pinterest(homescreen) app that has been build for i-phone. A large no. of images are coming from the server that I need to show in following form with pagination effect i.e loading images on scroll.
please reply if it is possible other way around. I'll be highly thankful.
If you want to perform loading of image on scroll then it will similar to List View.First save all data from WS URL then load on demand Now
Commonsware Endless Adapter For Listview,you can integrate it with GridView too
EndLessAdapter
Another way is to put your grid views in a ViewFlipper and then flip with an animation.
Use setInAnimation() and setOutAnimation() to set the animations and flip the pages with showNext() and showPrevious()
Create layout like as follow
<ScrollView...>
<LinearLayout....
android:id="#+id/linear1"
orientation="horizontal">
<LinearLayout....
android:id="#+id/linear2"
android:layout_weight="0.33"
orientation="vertical">
<LinearLayout....
android:id="#+id/linear3"
android:layout_weight="0.33"
orientation="vertical">
<LinearLayout....
android:layout_weight="0.33"
orientation="vertical">
</LinearLayout>
</ScrollView>
Now add your ImageView dynamically in layouts
linear1 = (LinearLayout) findViewById(R.id.linear1);
linear2 = (LinearLayout) findViewById(R.id.linear2);
linear3 = (LinearLayout) findViewById(R.id.linear3);
for(int i=0;i<n;i++)
{
ImageView iv = new ImageView(this);
iv.setImageResource(R.id.icon);
int j = count % 3; <----
if(j==0)
linear1.addView(iv);
else if(j==1)
linear2.addView(iv);
else
linear3.addView(iv);
}
Check: Staggered GridView
The StaggeredGridView allows the user to create a GridView with uneven rows similar to how Pinterest looks. Includes own OnItemClickListener and OnItemLongClickListener, selector, and fixed position restore.
This is old question, but for those that have similar problem:
Easiest way to accomplish this layout style is to use RecyclerView with StaggeredGridLayoutManager, like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
View recyclerView = findViewById(R.id.recycle_view);
assert recyclerView != null;
StaggeredGridLayoutManager gaggeredGridLayoutManager = new
StaggeredGridLayoutManager(2, 1);
recyclerView.setLayoutManager(gaggeredGridLayoutManager);
}
For the other part of question (pagging) it's best to receive your images in chunks (for example 50 images per request) and when user scrolls down (comes to the end) load more.