Skip to content Skip to sidebar Skip to footer

Image Loading Using Url In Android

i was tring to load image using url.a single url is working properly. but i need to add a few more images to this page.i need to add this image s to a list view.pls tell me how can

Solution 1:

try this code I am using Picasso library to populate ImageView,

if you are using android studio add this library using following code into your gradle file

compile'com.squareup.picasso:picasso:2.5.2'

use following code,

It is complete example of how you can make a custom ListView, I didn't include code where you have to get he JSON data from WebService as i didn't want to make the code complicated, I will write a separate code where i will show how to read the data you are interested in.

XML

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ListViewandroid:id="@+id/CustomListViewActivity_listView"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>

single_item.xml layout

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="50dp"android:orientation="horizontal"><TextViewandroid:id="@+id/single_item_textView"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_margin="4dp"android:layout_weight="0.5"android:text="New Text" /><ImageViewandroid:id="@+id/single_item_imageView"android:layout_width="0dp"android:layout_height="match_parent"android:layout_margin="4dp"android:layout_weight="0.5" /></LinearLayout>

Code

publicclassCustomListViewActivityextendsAppCompatActivity {

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_list_view);


        ArrayList<SingleItem> singleItems = newArrayList<>();
        singleItems.add(newSingleItem("http://movito.nervov.com/img/ace-hd.png","first Text"));
        singleItems.add(newSingleItem("http://movito.nervov.com/img/Movito_Logo_M.png","Second Text"));
        singleItems.add(newSingleItem("http://movito.nervov.com/img/ace-hd.png","third Text"));
        singleItems.add(newSingleItem("http://movito.nervov.com/img/Movito_Logo_M.png","fourth Text"));

        ListViewlistView= (ListView)findViewById(R.id.CustomListViewActivity_listView);
        MyAdapteradapter=newMyAdapter(getApplicationContext(), R.layout.single_item,singleItems);
        listView.setAdapter(adapter);


    }

    privateclassMyAdapterextendsArrayAdapter {

        private ArrayList<SingleItem> singleItems;
        private LayoutInflater layoutInflater;
        private Context context;
        private View single_View;

        publicMyAdapter(Context context, int resource, ArrayList<SingleItem> singleItems) {
            super(context, resource, singleItems);
            this.context = context;
            this.singleItems = singleItems;
            layoutInflater = LayoutInflater.from(this.context);
        }

        @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
            Viewrow= convertView;
            ViewHolderholder=null;

            if (row == null) {
                row = layoutInflater.inflate(R.layout.single_item, parent, false);

                holder = newViewHolder();
                holder.textView = (TextView) row.findViewById(R.id.single_item_textView);
                holder.imageView = (ImageView) row.findViewById(R.id.single_item_imageView);
                row.setTag(holder);
            } else {
                holder = (ViewHolder) row.getTag();
            }

            finalSingleItemsingleItem= singleItems.get(position);
            holder.textView.setText("" + singleItem.getText());
            Picasso.with(context).load(""+singleItem.getUrl()).into(holder.imageView);

            return row;
        }

        privateclassViewHolder {
            // Instance Variable (state or data)
            TextView textView;
            ImageView imageView;
        }
    }

    publicclassSingleItem {

        private String url;

        private String text;

        publicSingleItem() {
        }

        publicSingleItem(String url, String text) {
            this.url = url;
            this.text = text;
        }

        public String getUrl() {
            return url;
        }

        publicvoidsetUrl(String url) {
            this.url = url;
        }

        public String getText() {
            return text;
        }

        publicvoidsetText(String text) {
            this.text = text;
        }
    }
}

Output

As you will see the loading the images from the URL provided to the appropriate ImageView is taken care by Picasso, do make sure you add the permission for the internet in the AndroidManifest.xml

<uses-permissionandroid:name="android.permission.INTERNET" />

enter image description here

Post a Comment for "Image Loading Using Url In Android"