Advertisement
carsit

Untitled

Sep 10th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.68 KB | None | 0 0
  1. AndroidManifest.xml:
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4. package="com.example.rssreader">
  5. <uses-permission android:name="android.permission.INTERNET"/>
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:supportsRtl="true"
  11. android:theme="@style/AppTheme">
  12. <activity
  13. android:screenOrientation="portrait"
  14. android:name=".MainActivity"
  15. android:label="@string/app_name"
  16. android:theme="@style/AppTheme.NoActionBar">
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19.  
  20. <category android:name="android.intent.category.LAUNCHER" />
  21. </intent-filter>
  22. </activity>
  23. </application>
  24.  
  25. </manifest>
  26.  
  27. FeedItem.java:
  28. package com.example.rssreader;
  29.  
  30. /**
  31. * Created by rishabh on 24-02-2016.
  32. */
  33. public class FeedItem {
  34. String title;
  35. String link;
  36. String description;
  37. String pubDate;
  38. String thumbnailUrl;
  39.  
  40. public String getTitle() {
  41. return title;
  42. }
  43.  
  44. public void setTitle(String title) {
  45. this.title = title;
  46. }
  47.  
  48. public String getLink() {
  49. return link;
  50. }
  51.  
  52. public void setLink(String link) {
  53. this.link = link;
  54. }
  55.  
  56. public String getDescription() {
  57. return description;
  58. }
  59.  
  60. public void setDescription(String description) {
  61. this.description = description;
  62. }
  63.  
  64. public String getPubDate() {
  65. return pubDate;
  66. }
  67.  
  68. public void setPubDate(String pubDate) {
  69. this.pubDate = pubDate;
  70. }
  71.  
  72. public String getThumbnailUrl() {
  73. return thumbnailUrl;
  74. }
  75.  
  76. public void setThumbnailUrl(String thumbnailUrl) {
  77. this.thumbnailUrl = thumbnailUrl;
  78. }
  79. }
  80.  
  81.  
  82. MainActivity.java:
  83. package com.example.rssreader;
  84.  
  85. import android.os.Bundle;
  86. import android.support.design.widget.FloatingActionButton;
  87. import android.support.design.widget.Snackbar;
  88. import android.support.v7.app.AppCompatActivity;
  89. import android.support.v7.widget.RecyclerView;
  90. import android.support.v7.widget.Toolbar;
  91. import android.view.View;
  92. import android.view.Menu;
  93. import android.view.MenuItem;
  94.  
  95. public class MainActivity extends AppCompatActivity {
  96. RecyclerView recyclerView;
  97. @Override
  98. protected void onCreate(Bundle savedInstanceState) {
  99. super.onCreate(savedInstanceState);
  100. setContentView(R.layout.activity_main);
  101. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  102. setSupportActionBar(toolbar);
  103. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  104. fab.setOnClickListener(new View.OnClickListener() {
  105. @Override
  106. public void onClick(View view) {
  107. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  108. .setAction("Action", null).show();
  109. }
  110. });
  111. recyclerView= (RecyclerView) findViewById(R.id.recyclerview);
  112. ReadRss readRss=new ReadRss(this,recyclerView);
  113. readRss.execute();
  114. }
  115.  
  116. @Override
  117. public boolean onCreateOptionsMenu(Menu menu) {
  118. // Inflate the menu; this adds items to the action bar if it is present.
  119. getMenuInflater().inflate(R.menu.menu_main, menu);
  120. return true;
  121. }
  122.  
  123. @Override
  124. public boolean onOptionsItemSelected(MenuItem item) {
  125. // Handle action bar item clicks here. The action bar will
  126. // automatically handle clicks on the Home/Up button, so long
  127. // as you specify a parent activity in AndroidManifest.xml.
  128. int id = item.getItemId();
  129.  
  130. //noinspection SimplifiableIfStatement
  131. if (id == R.id.action_settings) {
  132. return true;
  133. }
  134.  
  135. return super.onOptionsItemSelected(item);
  136. }
  137. }
  138.  
  139. MyAdapter:
  140. package com.example.rssreader;
  141.  
  142. import android.animation.ObjectAnimator;
  143. import android.content.Context;
  144. import android.support.v7.widget.CardView;
  145. import android.support.v7.widget.RecyclerView;
  146. import android.view.LayoutInflater;
  147. import android.view.View;
  148. import android.view.ViewGroup;
  149. import android.widget.ImageView;
  150. import android.widget.TextView;
  151.  
  152. import com.daimajia.androidanimations.library.Techniques;
  153. import com.daimajia.androidanimations.library.YoYo;
  154. import com.squareup.picasso.Picasso;
  155.  
  156. import java.util.ArrayList;
  157.  
  158. /**
  159. * Created by rishabh on 26-02-2016.
  160. */
  161. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
  162. ArrayList<FeedItem>feedItems;
  163. Context context;
  164. public MyAdapter(Context context,ArrayList<FeedItem>feedItems){
  165. this.feedItems=feedItems;
  166. this.context=context;
  167. }
  168. @Override
  169. public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  170. View view= LayoutInflater.from(context).inflate(R.layout.custum_row_news_item,parent,false);
  171. MyViewHolder holder=new MyViewHolder(view);
  172. return holder;
  173. }
  174.  
  175. @Override
  176. public void onBindViewHolder(MyViewHolder holder, int position) {
  177. YoYo.with(Techniques.FadeIn).playOn(holder.cardView);
  178. FeedItem current=feedItems.get(position);
  179. holder.Title.setText(current.getTitle());
  180. holder.Description.setText(current.getDescription());
  181. holder.Date.setText(current.getPubDate());
  182. Picasso.with(context).load(current.getThumbnailUrl()).into(holder.Thumbnail);
  183.  
  184. }
  185.  
  186.  
  187.  
  188. @Override
  189. public int getItemCount() {
  190. return feedItems.size();
  191. }
  192.  
  193. public class MyViewHolder extends RecyclerView.ViewHolder {
  194. TextView Title,Description,Date;
  195. ImageView Thumbnail;
  196. CardView cardView;
  197. public MyViewHolder(View itemView) {
  198. super(itemView);
  199. Title= (TextView) itemView.findViewById(R.id.title_text);
  200. Description= (TextView) itemView.findViewById(R.id.description_text);
  201. Date= (TextView) itemView.findViewById(R.id.date_text);
  202. Thumbnail= (ImageView) itemView.findViewById(R.id.thumb_img);
  203. cardView= (CardView) itemView.findViewById(R.id.cardview);
  204. }
  205. }
  206. }
  207.  
  208. ReadRss.java:
  209.  
  210. package com.example.rssreader;
  211.  
  212. import android.app.ProgressDialog;
  213. import android.content.Context;
  214. import android.os.AsyncTask;
  215. import android.support.v7.widget.LinearLayoutManager;
  216. import android.support.v7.widget.RecyclerView;
  217. import android.util.Log;
  218.  
  219. import org.w3c.dom.Document;
  220. import org.w3c.dom.Element;
  221. import org.w3c.dom.Node;
  222. import org.w3c.dom.NodeList;
  223.  
  224. import java.io.InputStream;
  225. import java.net.HttpURLConnection;
  226. import java.net.MalformedURLException;
  227. import java.net.URL;
  228. import java.util.ArrayList;
  229.  
  230. import javax.xml.parsers.DocumentBuilder;
  231. import javax.xml.parsers.DocumentBuilderFactory;
  232.  
  233. /**
  234. * Created by rishabh on 31-01-2016.
  235. */
  236. public class ReadRss extends AsyncTask<Void, Void, Void> {
  237. Context context;
  238. String address = "http://www.tuttoandroid.net/feed/";
  239. ProgressDialog progressDialog;
  240. ArrayList<FeedItem>feedItems;
  241. RecyclerView recyclerView;
  242. URL url;
  243. public ReadRss(Context context,RecyclerView recyclerView) {
  244. this.recyclerView=recyclerView;
  245. this.context = context;
  246. progressDialog = new ProgressDialog(context);
  247. progressDialog.setMessage("Caricamento...");
  248. }
  249.  
  250. @Override
  251. protected void onPreExecute() {
  252. progressDialog.show();
  253. super.onPreExecute();
  254. }
  255.  
  256. @Override
  257. protected void onPostExecute(Void aVoid) {
  258. super.onPostExecute(aVoid);
  259. progressDialog.dismiss();
  260. MyAdapter adapter=new MyAdapter(context,feedItems);
  261. recyclerView.setLayoutManager(new LinearLayoutManager(context));
  262. recyclerView.addItemDecoration(new VerticalSpace(50));
  263. recyclerView.setAdapter(adapter);
  264.  
  265. }
  266.  
  267. @Override
  268. protected Void doInBackground(Void... params) {
  269. ProcessXml(Getdata());
  270.  
  271. return null;
  272. }
  273.  
  274. private void ProcessXml(Document data) {
  275. if (data != null) {
  276. feedItems=new ArrayList<>();
  277. Element root = data.getDocumentElement();
  278. Node channel = root.getChildNodes().item(1);
  279. NodeList items = channel.getChildNodes();
  280. for (int i = 0; i < items.getLength(); i++) {
  281. Node cureentchild = items.item(i);
  282. if (cureentchild.getNodeName().equalsIgnoreCase("item")) {
  283. FeedItem item=new FeedItem();
  284. NodeList itemchilds = cureentchild.getChildNodes();
  285. for (int j = 0; j < itemchilds.getLength(); j++) {
  286. Node cureent = itemchilds.item(j);
  287. if (cureent.getNodeName().equalsIgnoreCase("title")){
  288. item.setTitle(cureent.getTextContent());
  289. }else if (cureent.getNodeName().equalsIgnoreCase("description")){
  290. item.setDescription(cureent.getTextContent());
  291. }else if (cureent.getNodeName().equalsIgnoreCase("pubDate")){
  292. item.setPubDate(cureent.getTextContent());
  293. }else if (cureent.getNodeName().equalsIgnoreCase("link")){
  294. item.setLink(cureent.getTextContent());
  295. }else if (cureent.getNodeName().equalsIgnoreCase("media:thumbnail")){
  296. //this will return us thumbnail url
  297. String url=cureent.getAttributes().item(0).getTextContent();
  298. item.setThumbnailUrl(url);
  299. }
  300. }
  301. feedItems.add(item);
  302.  
  303.  
  304.  
  305.  
  306.  
  307. }
  308. }
  309. }
  310. }
  311.  
  312. public Document Getdata() {
  313. try {
  314. url = new URL(address);
  315. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  316. connection.setRequestMethod("GET");
  317. InputStream inputStream = connection.getInputStream();
  318. DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
  319. DocumentBuilder builder = builderFactory.newDocumentBuilder();
  320. Document xmlDoc = builder.parse(inputStream);
  321. return xmlDoc;
  322. } catch (Exception e) {
  323. e.printStackTrace();
  324. return null;
  325. }
  326. }
  327. }
  328.  
  329. VerticalSpace.java:
  330. package com.example.rssreader;
  331.  
  332. import android.graphics.Rect;
  333. import android.support.v7.widget.RecyclerView;
  334. import android.view.View;
  335.  
  336. /**
  337. * Created by rishabh on 01-03-2016.
  338. */
  339. public class VerticalSpace extends RecyclerView.ItemDecoration {
  340. int Space;
  341. public VerticalSpace(int Space){
  342. this.Space=Space;
  343. }
  344.  
  345. @Override
  346. public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
  347. outRect.left=Space;
  348. outRect.bottom=Space;
  349. outRect.right=Space;
  350. if (parent.getChildLayoutPosition(view)==0){
  351. outRect.top=Space;
  352. }
  353. }
  354. }
  355.  
  356. activity_main.xml:
  357. <?xml version="1.0" encoding="utf-8"?>
  358. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  359. xmlns:app="http://schemas.android.com/apk/res-auto"
  360. xmlns:tools="http://schemas.android.com/tools"
  361. android:layout_width="match_parent"
  362. android:layout_height="match_parent"
  363. android:fitsSystemWindows="true"
  364. tools:context="com.example.rssreader.MainActivity">
  365.  
  366. <android.support.design.widget.AppBarLayout
  367. android:layout_width="match_parent"
  368. android:layout_height="wrap_content"
  369. android:theme="@style/AppTheme.AppBarOverlay">
  370.  
  371. <android.support.v7.widget.Toolbar
  372. android:id="@+id/toolbar"
  373. android:layout_width="match_parent"
  374. android:layout_height="?attr/actionBarSize"
  375. android:background="?attr/colorPrimary"
  376. app:popupTheme="@style/AppTheme.PopupOverlay" />
  377.  
  378. </android.support.design.widget.AppBarLayout>
  379.  
  380. <include layout="@layout/content_main" />
  381.  
  382. <android.support.design.widget.FloatingActionButton
  383. android:id="@+id/fab"
  384. android:layout_width="wrap_content"
  385. android:layout_height="wrap_content"
  386. android:layout_gravity="bottom|end"
  387. android:layout_margin="@dimen/fab_margin"
  388. android:src="@android:drawable/ic_dialog_email" />
  389.  
  390. </android.support.design.widget.CoordinatorLayout>
  391.  
  392. content_main.xml:
  393. <?xml version="1.0" encoding="utf-8"?>
  394. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  395. xmlns:app="http://schemas.android.com/apk/res-auto"
  396. xmlns:tools="http://schemas.android.com/tools"
  397. android:layout_width="match_parent"
  398. android:layout_height="match_parent"
  399.  
  400. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  401. tools:context="com.example.rssreader.MainActivity"
  402. tools:showIn="@layout/activity_main">
  403.  
  404. <android.support.v7.widget.RecyclerView
  405. android:layout_width="wrap_content"
  406. android:layout_height="wrap_content"
  407. android:id="@+id/recyclerview"
  408. />
  409. </RelativeLayout>
  410.  
  411. custum_row_news_item.xml:
  412. <?xml version="1.0" encoding="utf-8"?>
  413. <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
  414. xmlns:app="http://schemas.android.com/apk/res-auto"
  415. android:layout_width="match_parent"
  416. android:layout_height="match_parent"
  417. android:orientation="vertical"
  418. android:id="@+id/cardview"
  419. app:cardBackgroundColor="@color/cardview_dark_background"
  420. app:cardElevation="4dp">
  421.  
  422. <RelativeLayout
  423. android:layout_width="match_parent"
  424. android:layout_height="match_parent">
  425.  
  426. <TextView
  427. android:id="@+id/date_text"
  428. android:layout_width="wrap_content"
  429. android:layout_height="wrap_content"
  430. android:layout_alignParentEnd="true"
  431. android:layout_alignParentRight="true"
  432. android:layout_alignParentTop="true"
  433. android:layout_margin="@dimen/activity_horizontal_margin"
  434. android:text="Small Text"
  435. android:textAppearance="?android:attr/textAppearanceSmall" />
  436.  
  437. <ImageView
  438. android:id="@+id/thumb_img"
  439. android:layout_width="match_parent"
  440. android:layout_height="200dp"
  441. android:layout_below="@+id/date_text"
  442. android:layout_centerHorizontal="true"
  443. android:src="@drawable/test" />
  444.  
  445. <TextView
  446. android:id="@+id/title_text"
  447. android:layout_width="match_parent"
  448. android:layout_height="wrap_content"
  449. android:layout_alignBottom="@+id/thumb_img"
  450. android:layout_centerHorizontal="true"
  451. android:background="#80000000"
  452. android:gravity="center"
  453. android:lines="1"
  454. android:text="Large Text"
  455. android:textAppearance="?android:attr/textAppearanceLarge" />
  456.  
  457. <TextView
  458. android:id="@+id/description_text"
  459. android:layout_width="match_parent"
  460. android:layout_height="wrap_content"
  461. android:layout_alignParentLeft="true"
  462. android:layout_alignParentStart="true"
  463. android:layout_below="@+id/thumb_img"
  464. android:layout_margin="@dimen/activity_horizontal_margin"
  465. android:text="Medium Text"
  466. android:textAppearance="?android:attr/textAppearanceMedium" />
  467. </RelativeLayout>
  468.  
  469.  
  470. </android.support.v7.widget.CardView>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement