1. Creating New Project
2. Open build.gradle and add recycler view dependency. com.android.support:recyclerview-v7:27.1.1,com.android.support:cardview-v7:27.1.1,org.jsoup:jsoup:1.11.3,com.android.support:design:27.1.1,com.android.volley:volley:1.1.1 and rebuild the project.
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.eduapp.tutorial.bengali.newsapp"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.volley:volley:1.1.1'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'org.jsoup:jsoup:1.11.3'
implementation 'com.android.support:design:27.1.1'
}
3.Open activity_main.xml and type :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
4.Create new xml file "news_item.xml" in layout folder. :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="6dp"
android:layout_marginBottom="0dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
android:padding="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/nImage"
android:layout_width="80dp"
android:layout_height="80dp"
android:contentDescription="@string/newsImage"
android:layout_margin="2dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_margin="4dp">
<TextView
android:id="@+id/nTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Base.TextAppearance.AppCompat.Title"
android:text="sdfsfsgdgsg"
android:lines="2"
/>
<TextView
android:id="@+id/nDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="textEnd"
android:gravity="end"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
5.Create new Activity "DetailsActivity" in java folder and Open activity_details.xml and type:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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"
tools:context=".DetailsActivity">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginTop="4dp"
android:layout_marginRight="4dp"
app:cardCornerRadius="4dp"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/deltImg"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginBottom="8dp"
android:contentDescription="@string/newsImage"
android:scaleType="fitCenter" />
<TextView
android:id="@+id/titleText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_marginBottom="8dp"
android:padding="4dp"
style="@style/Base.TextAppearance.AppCompat.Title"/>
<TextView
android:id="@+id/discrText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="textStart"
android:padding="4dp"
android:textSize="16sp"
android:layout_marginBottom="8dp"
android:gravity="start" />
<TextView
android:id="@+id/dateText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</ScrollView>
6.Create new java model class "NewsModel.java" in java folder:
package com.eduapp.tutorial.bengali.newsapp.model;
public class NewsModel {
private String name;
private String descri;
private String imgUri;
private String pubDate;
public NewsModel() {
}
public NewsModel(String name, String descri, String imgUri, String pubDate) {
this.name = name;
this.descri = descri;
this.imgUri = imgUri;
this.pubDate = pubDate;
}
public String getName() {
return name;
}
public String getDescri() {
return descri;
}
public String getImgUri() {
return imgUri;
}
public String getPubDate() {
return pubDate;
}
}
7.Create volley singleton class "MySingleton.java" in java folder:
package com.eduapp.tutorial.bengali.newsapp.volleysingleton;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
private MySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache
cache = new LruCache<>(10);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized MySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new MySingleton(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public void addToRequestQueue(Request req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
8.Create Adapter "RecyclerAdapter" :
package com.eduapp.tutorial.bengali.newsapp.adapter;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
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.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.eduapp.tutorial.bengali.newsapp.DetailsActivity;
import com.eduapp.tutorial.bengali.newsapp.R;
import com.eduapp.tutorial.bengali.newsapp.model.NewsModel;
import com.eduapp.tutorial.bengali.newsapp.volleysingleton.MySingleton;
import java.util.ArrayList;
public class RecyclerAdapter extends RecyclerView.Adapter {
private ArrayList newsList;
private MySingleton mySingleton;
private ImageLoader mImageLoader;
private Context ctx;
public RecyclerAdapter(ArrayList newsList, Context ctx) {
this.newsList = newsList;
this.ctx = ctx;
mySingleton = MySingleton.getInstance(ctx);
mImageLoader = mySingleton.getImageLoader();
}
@NonNull
@Override
public MyviewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(ctx.getApplicationContext()).inflate(R.layout.news_items,parent,false);
MyviewHolder myviewHolder = new MyviewHolder(view);
return myviewHolder;
}
@Override
public void onBindViewHolder(@NonNull final MyviewHolder holder, int position) {
final NewsModel newsModel = newsList.get(position);
holder.title.setText(newsModel.getName());
holder.dateNews.setText(newsModel.getPubDate());
String imgUid = newsModel.getImgUri();
if (imgUid!=null){
mImageLoader.get(imgUid, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
holder.mImageView.setImageBitmap(response.getBitmap());
}
@Override
public void onErrorResponse(VolleyError error) {
}
});
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String img = newsModel.getImgUri();
String name = newsModel.getName();
String desc = newsModel.getDescri();
String pubDate = newsModel.getPubDate();
Intent intent = new Intent(ctx.getApplicationContext(),DetailsActivity.class);
intent.putExtra("TITLE",name);
intent.putExtra("IURL",img);
intent.putExtra("DESCR",desc);
intent.putExtra("DATE",pubDate);
ctx.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return newsList.size();
}
public class MyviewHolder extends RecyclerView.ViewHolder{
private ImageView mImageView;
private TextView title,dateNews;
public MyviewHolder(View itemView) {
super(itemView);
mImageView = (ImageView)itemView.findViewById(R.id.nImage);
title = (TextView) itemView.findViewById(R.id.nTitle);
dateNews = (TextView)itemView.findViewById(R.id.nDate);
}
}
}
9. Open MainActivity :
package com.eduapp.tutorial.bengali.newsapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ProgressBar;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.eduapp.tutorial.bengali.newsapp.adapter.RecyclerAdapter;
import com.eduapp.tutorial.bengali.newsapp.model.NewsModel;
import com.eduapp.tutorial.bengali.newsapp.volleysingleton.MySingleton;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.lang.reflect.Method;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RequestQueue mQueue;
private ArrayList nList = new ArrayList<>();
private RecyclerView recyclerView;
private RecyclerAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
mAdapter = new RecyclerAdapter(nList, this);
recyclerView.setAdapter(mAdapter);
mQueue = MySingleton.getInstance(this.getApplicationContext()).getRequestQueue();
String url = "rss url";
StringRequest mStringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener() {
@Override
public void onResponse(String response) {
Document doc = Jsoup.parse(response);
Elements itemElements = doc.select("item");
for (int i = 0; i < itemElements.size(); i++) {
Element item = itemElements.get(i);
String title = item.child(0).text();
String description = item.child(3).text();
String imgUrl = item.child(4).attr("url").toString();
String date = item.child(5).text();
NewsModel newsModel = new NewsModel(title, description, imgUrl, date);
nList.add(newsModel);
// Log.d("ITEM",imgUrl);
}
mAdapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
MySingleton.getInstance(this).addToRequestQueue(mStringRequest);
}
}
10.Open DetailsActivity and copy code and paste :
package com.eduapp.tutorial.bengali.newsapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.eduapp.tutorial.bengali.newsapp.volleysingleton.MySingleton;
public class DetailsActivity extends AppCompatActivity {
private TextView titleText,description,dateD;
private ImageView imageView;
private MySingleton mySingleton;
private ImageLoader mImageLoader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
imageView=(ImageView)findViewById(R.id.deltImg);
titleText=(TextView)findViewById(R.id.titleText);
description=(TextView)findViewById(R.id.discrText);
dateD=(TextView)findViewById(R.id.dateText);
mySingleton = MySingleton.getInstance(this);
mImageLoader = mySingleton.getImageLoader();
String imgUrl = getIntent().getExtras().get("IURL").toString();
if (imgUrl!=null){
mImageLoader.get(imgUrl, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
imageView.setImageBitmap(response.getBitmap());
}
@Override
public void onErrorResponse(VolleyError error) {
}
});
}
titleText.setText(getIntent().getExtras().get("TITLE").toString());
description.setText(getIntent().getExtras().get("DESCR").toString());
dateD.setText(getIntent().getExtras().get("DATE").toString());
}
}
Run this application and enjoy.
Las Vegas Hotels & Casinos - Mapyro
ReplyDeleteHotels 남원 출장안마 in Las Vegas · Fairfield 시흥 출장안마 Inn & Suites by 원주 출장마사지 Wyndham Las Vegas · Fairfield Inn 익산 출장안마 & Suites by Wyndham Las Vegas · Fairfield Inn & Suites by Wyndham Las Vegas · 광양 출장마사지 Fairfield Inn