Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AndroidManifest.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.rssreader">
- <uses-permission android:name="android.permission.INTERNET"/>
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity
- android:screenOrientation="portrait"
- android:name=".MainActivity"
- android:label="@string/app_name"
- android:theme="@style/AppTheme.NoActionBar">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
- FeedItem.java:
- package com.example.rssreader;
- /**
- * Created by rishabh on 24-02-2016.
- */
- public class FeedItem {
- String title;
- String link;
- String description;
- String pubDate;
- String thumbnailUrl;
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getLink() {
- return link;
- }
- public void setLink(String link) {
- this.link = link;
- }
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- public String getPubDate() {
- return pubDate;
- }
- public void setPubDate(String pubDate) {
- this.pubDate = pubDate;
- }
- public String getThumbnailUrl() {
- return thumbnailUrl;
- }
- public void setThumbnailUrl(String thumbnailUrl) {
- this.thumbnailUrl = thumbnailUrl;
- }
- }
- MainActivity.java:
- package com.example.rssreader;
- import android.os.Bundle;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.RecyclerView;
- import android.support.v7.widget.Toolbar;
- import android.view.View;
- import android.view.Menu;
- import android.view.MenuItem;
- public class MainActivity extends AppCompatActivity {
- RecyclerView recyclerView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
- .setAction("Action", null).show();
- }
- });
- recyclerView= (RecyclerView) findViewById(R.id.recyclerview);
- ReadRss readRss=new ReadRss(this,recyclerView);
- readRss.execute();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
- MyAdapter:
- package com.example.rssreader;
- import android.animation.ObjectAnimator;
- import android.content.Context;
- import android.support.v7.widget.CardView;
- import android.support.v7.widget.RecyclerView;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ImageView;
- import android.widget.TextView;
- import com.daimajia.androidanimations.library.Techniques;
- import com.daimajia.androidanimations.library.YoYo;
- import com.squareup.picasso.Picasso;
- import java.util.ArrayList;
- /**
- * Created by rishabh on 26-02-2016.
- */
- public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
- ArrayList<FeedItem>feedItems;
- Context context;
- public MyAdapter(Context context,ArrayList<FeedItem>feedItems){
- this.feedItems=feedItems;
- this.context=context;
- }
- @Override
- public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- View view= LayoutInflater.from(context).inflate(R.layout.custum_row_news_item,parent,false);
- MyViewHolder holder=new MyViewHolder(view);
- return holder;
- }
- @Override
- public void onBindViewHolder(MyViewHolder holder, int position) {
- YoYo.with(Techniques.FadeIn).playOn(holder.cardView);
- FeedItem current=feedItems.get(position);
- holder.Title.setText(current.getTitle());
- holder.Description.setText(current.getDescription());
- holder.Date.setText(current.getPubDate());
- Picasso.with(context).load(current.getThumbnailUrl()).into(holder.Thumbnail);
- }
- @Override
- public int getItemCount() {
- return feedItems.size();
- }
- public class MyViewHolder extends RecyclerView.ViewHolder {
- TextView Title,Description,Date;
- ImageView Thumbnail;
- CardView cardView;
- public MyViewHolder(View itemView) {
- super(itemView);
- Title= (TextView) itemView.findViewById(R.id.title_text);
- Description= (TextView) itemView.findViewById(R.id.description_text);
- Date= (TextView) itemView.findViewById(R.id.date_text);
- Thumbnail= (ImageView) itemView.findViewById(R.id.thumb_img);
- cardView= (CardView) itemView.findViewById(R.id.cardview);
- }
- }
- }
- ReadRss.java:
- package com.example.rssreader;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.os.AsyncTask;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.util.Log;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.ArrayList;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- /**
- * Created by rishabh on 31-01-2016.
- */
- public class ReadRss extends AsyncTask<Void, Void, Void> {
- Context context;
- String address = "http://www.tuttoandroid.net/feed/";
- ProgressDialog progressDialog;
- ArrayList<FeedItem>feedItems;
- RecyclerView recyclerView;
- URL url;
- public ReadRss(Context context,RecyclerView recyclerView) {
- this.recyclerView=recyclerView;
- this.context = context;
- progressDialog = new ProgressDialog(context);
- progressDialog.setMessage("Caricamento...");
- }
- @Override
- protected void onPreExecute() {
- progressDialog.show();
- super.onPreExecute();
- }
- @Override
- protected void onPostExecute(Void aVoid) {
- super.onPostExecute(aVoid);
- progressDialog.dismiss();
- MyAdapter adapter=new MyAdapter(context,feedItems);
- recyclerView.setLayoutManager(new LinearLayoutManager(context));
- recyclerView.addItemDecoration(new VerticalSpace(50));
- recyclerView.setAdapter(adapter);
- }
- @Override
- protected Void doInBackground(Void... params) {
- ProcessXml(Getdata());
- return null;
- }
- private void ProcessXml(Document data) {
- if (data != null) {
- feedItems=new ArrayList<>();
- Element root = data.getDocumentElement();
- Node channel = root.getChildNodes().item(1);
- NodeList items = channel.getChildNodes();
- for (int i = 0; i < items.getLength(); i++) {
- Node cureentchild = items.item(i);
- if (cureentchild.getNodeName().equalsIgnoreCase("item")) {
- FeedItem item=new FeedItem();
- NodeList itemchilds = cureentchild.getChildNodes();
- for (int j = 0; j < itemchilds.getLength(); j++) {
- Node cureent = itemchilds.item(j);
- if (cureent.getNodeName().equalsIgnoreCase("title")){
- item.setTitle(cureent.getTextContent());
- }else if (cureent.getNodeName().equalsIgnoreCase("description")){
- item.setDescription(cureent.getTextContent());
- }else if (cureent.getNodeName().equalsIgnoreCase("pubDate")){
- item.setPubDate(cureent.getTextContent());
- }else if (cureent.getNodeName().equalsIgnoreCase("link")){
- item.setLink(cureent.getTextContent());
- }else if (cureent.getNodeName().equalsIgnoreCase("media:thumbnail")){
- //this will return us thumbnail url
- String url=cureent.getAttributes().item(0).getTextContent();
- item.setThumbnailUrl(url);
- }
- }
- feedItems.add(item);
- }
- }
- }
- }
- public Document Getdata() {
- try {
- url = new URL(address);
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("GET");
- InputStream inputStream = connection.getInputStream();
- DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = builderFactory.newDocumentBuilder();
- Document xmlDoc = builder.parse(inputStream);
- return xmlDoc;
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- }
- VerticalSpace.java:
- package com.example.rssreader;
- import android.graphics.Rect;
- import android.support.v7.widget.RecyclerView;
- import android.view.View;
- /**
- * Created by rishabh on 01-03-2016.
- */
- public class VerticalSpace extends RecyclerView.ItemDecoration {
- int Space;
- public VerticalSpace(int Space){
- this.Space=Space;
- }
- @Override
- public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
- outRect.left=Space;
- outRect.bottom=Space;
- outRect.right=Space;
- if (parent.getChildLayoutPosition(view)==0){
- outRect.top=Space;
- }
- }
- }
- activity_main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:fitsSystemWindows="true"
- tools:context="com.example.rssreader.MainActivity">
- <android.support.design.widget.AppBarLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:theme="@style/AppTheme.AppBarOverlay">
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:background="?attr/colorPrimary"
- app:popupTheme="@style/AppTheme.PopupOverlay" />
- </android.support.design.widget.AppBarLayout>
- <include layout="@layout/content_main" />
- <android.support.design.widget.FloatingActionButton
- android:id="@+id/fab"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="bottom|end"
- android:layout_margin="@dimen/fab_margin"
- android:src="@android:drawable/ic_dialog_email" />
- </android.support.design.widget.CoordinatorLayout>
- content_main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- app:layout_behavior="@string/appbar_scrolling_view_behavior"
- tools:context="com.example.rssreader.MainActivity"
- tools:showIn="@layout/activity_main">
- <android.support.v7.widget.RecyclerView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/recyclerview"
- />
- </RelativeLayout>
- custum_row_news_item.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:id="@+id/cardview"
- app:cardBackgroundColor="@color/cardview_dark_background"
- app:cardElevation="4dp">
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:id="@+id/date_text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentEnd="true"
- android:layout_alignParentRight="true"
- android:layout_alignParentTop="true"
- android:layout_margin="@dimen/activity_horizontal_margin"
- android:text="Small Text"
- android:textAppearance="?android:attr/textAppearanceSmall" />
- <ImageView
- android:id="@+id/thumb_img"
- android:layout_width="match_parent"
- android:layout_height="200dp"
- android:layout_below="@+id/date_text"
- android:layout_centerHorizontal="true"
- android:src="@drawable/test" />
- <TextView
- android:id="@+id/title_text"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignBottom="@+id/thumb_img"
- android:layout_centerHorizontal="true"
- android:background="#80000000"
- android:gravity="center"
- android:lines="1"
- android:text="Large Text"
- android:textAppearance="?android:attr/textAppearanceLarge" />
- <TextView
- android:id="@+id/description_text"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:layout_below="@+id/thumb_img"
- android:layout_margin="@dimen/activity_horizontal_margin"
- android:text="Medium Text"
- android:textAppearance="?android:attr/textAppearanceMedium" />
- </RelativeLayout>
- </android.support.v7.widget.CardView>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement