Build a rss news reader with image android sample

Here is a sample Android app that builds a basic RSS news reader with image support:

Project Structure

app/build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion '29.0.3'

    defaultConfig {
        applicationId "com.example.rssreader"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
        }
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.recyclerview:recyclerview:1.2.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'org.simpleframework:simple-xml:2.7.1'
}

app/src/main/java/com/example/rssreader

app/src/main/res/layout

app/src/main/res/values

MainActivity.java

import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RssFeedAdapter adapter;
    private List<RssFeedItem> rssFeedItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        rssFeedItems = new ArrayList<>();
        adapter = new RssFeedAdapter(rssFeedItems);
        recyclerView.setAdapter(adapter);

        new RssParser().parseRssFeed("https://example.com/rss", new RssParser.OnRssFeedParsedListener() {
            @Override
            public void onRssFeedParsed(List<RssFeedItem> rssFeedItems) {
                MainActivity.this.rssFeedItems.clear();
                MainActivity.this.rssFeedItems.addAll(rssFeedItems);
                adapter.notifyDataSetChanged();
            }
        });
    }
}

RssFeedAdapter.java

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class RssFeedAdapter extends RecyclerView.Adapter<RssFeedAdapter.ViewHolder> {
    private List<RssFeedItem> rssFeedItems;

    public RssFeedAdapter(List<RssFeedItem> rssFeedItems) {
        this.rssFeedItems = rssFeedItems;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        RssFeedItem rssFeedItem = rssFeedItems.get(position);
        holder.titleTextView.setText(rssFeedItem.getTitle());
        holder.descriptionTextView.setText(rssFeedItem.getDescription());
        Picasso.get().load(rssFeedItem.getImageUrl()).into(holder.imageView);
    }

    @Override
    public int getItemCount() {
        return rssFeedItems.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView titleTextView;
        public TextView descriptionTextView;
        public ImageView imageView;

        public ViewHolder(View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.title_text_view);
            descriptionTextView = itemView.findViewById(R.id.description_text_view);
            imageView = itemView.findViewById(R.id.image_view);
        }
    }
}

RssFeedItem.java

import java.util.Date;

public class RssFeedItem {
    private String title;
    private String description;
    private String imageUrl;

    public RssFeedItem(String title, String description, String imageUrl) {
        this.title = title;
        this.description = description;
        this.imageUrl = imageUrl;
    }

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }

    public String getImageUrl() {
        return imageUrl;
    }
}

RssParser.java

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Root(name = "rss", strict = false)
public class RssParser {
    private OnRssFeedParsedListener onRssFeedParsedListener;

    public RssParser() {}

    public void parseRssFeed(String url, OnRssFeedParsedListener onRssFeedParsedListener) {
        this.onRssFeedParsedListener = onRssFeedParsedListener;
        new DownloadRssFeedTask().execute(url);
    }

    public interface OnRssFeedParsedListener {
        void onRssFeedParsed(List<RssFeedItem> rssFeedItems);
    }

    private class DownloadRssFeedTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            try {
                InputStream inputStream = new URL(urls[0]).openStream();
                String rssFeedXml = convertInputStreamToString(inputStream);
                return rssFeedXml;
            } catch (Exception e) {
                return null;
            }
        }

        @Override
        protected void onPostExecute(String rssFeedXml) {
            if (rssFeedXml!= null) {
                List<RssFeedItem> rssFeedItems = parseRssFeedXml(rssFeedXml);
                onRssFeedParsedListener.onRssFeedParsed(rssFeedItems);
            }
        }
    }

    private String convertInputStreamToString(InputStream inputStream) {
        // Convert the input stream to a string
        //...
    }

    private List<RssFeedItem> parseRssFeedXml(String rssFeedXml) {
        // Parse the RSS feed XML and return a list of RssFeedItem objects
        //...
    }
}

item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/description_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp" />

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop" />

</LinearLayout>

This is a basic example of an RSS news reader with image support. You'll need to modify the RssParser class to parse the RSS feed XML and return a list of RssFeedItem objects. You'll also need to modify the MainActivity class to display the RSS feed items in a RecyclerView.

Note that this is just a sample code and you'll need to modify it to fit your specific requirements. Additionally, you'll need to add error handling and other features to make the app more robust.