Skip to content

Design improved, Changed square image to round shape, Formatted, Added latest targetSdkVersion, Updated readme file. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

This is a simple news feed application based on Retrofit and RecyclerView, The description of this project is first we will send a api request on the server for news json data , then shows into RecyclerView, after that whenever we click on any news item it will show the article on that news...

<h1 align="center">Project Structure</h1>
<p align="center">
<img src="app/src/main/res/drawable/Screenshot 2020-08-18 at 12.47.08 PM.png"/>
</p>

<p align="center">
<img src="app/src/main/res/drawable/Screenshot 2020-08-18 at 12.47.54 PM.png"/>
</p>
# Demo Video
![simplified](https://user-images.githubusercontent.com/54389203/94741498-1e061b00-036c-11eb-9a9b-8d7910a7cb88.gif)

# Structure overview
![str](https://user-images.githubusercontent.com/54389203/94741833-a7b5e880-036c-11eb-8138-f1a30b9b92d1.png)

# Demo ScreenShot
![Screenshot_20201001-024500](https://user-images.githubusercontent.com/54389203/94741133-94564d80-036b-11eb-8984-4ec36a0edb87.jpeg)
13 changes: 8 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
compileSdkVersion 30
buildToolsVersion "30.0.1"

defaultConfig {
applicationId "com.codingwithjks.retrofit"
minSdkVersion 14
targetSdkVersion 29
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"

Expand All @@ -34,13 +34,16 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

// For Recycler View & CardView
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'
//retrofit

// Retrofit : Network request
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'

// Glide : Image Loading Library
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

Expand Down
37 changes: 21 additions & 16 deletions app/src/main/java/com/codingwithjks/retrofit/Adapter/NewsAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,48 @@ import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.codingwithjks.newsfeedapp.Model.Articles
import com.codingwithjks.newsfeedapp.Model.News
import com.codingwithjks.retrofit.Listener.Listener
import com.codingwithjks.retrofit.R

class NewsAdapter(private val context: Context,private var newsList:ArrayList<Articles>,private val listener:Listener) : RecyclerView.Adapter<NewsAdapter.NewsViewHolder>() {

class NewsAdapter(
private val context: Context,
private var newsList: ArrayList<Articles>,
private val listener: Listener
) : RecyclerView.Adapter<NewsAdapter.NewsViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewsViewHolder =
NewsViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.each_row,parent,false))
NewsViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.each_row, parent, false)
)

override fun getItemCount(): Int =newsList.size
override fun getItemCount(): Int = newsList.size

override fun onBindViewHolder(holder: NewsViewHolder, position: Int) {
val news=newsList[position]
holder.title.text=news.title
holder.content.text=news.content
val news = newsList[position]
holder.title.text = news.title
holder.content.text = news.content
Glide.with(context)
.load(news.urlToImage)
.apply(RequestOptions().circleCrop())
.into(holder.image)
}

inner class NewsViewHolder(itemView:View) : RecyclerView.ViewHolder(itemView)
{
val image:ImageView=itemView.findViewById(R.id.image)
val title:TextView=itemView.findViewById(R.id.title)
val content:TextView=itemView.findViewById(R.id.content)
inner class NewsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val image: ImageView = itemView.findViewById(R.id.image)
val title: TextView = itemView.findViewById(R.id.title)
val content: TextView = itemView.findViewById(R.id.content)

init {
itemView.setOnClickListener {
listener.onItemClickListener(adapterPosition)
}
}
}

fun setData(newsList: ArrayList<Articles>)
{
this.newsList=newsList
fun setData(newsList: ArrayList<Articles>) {
this.newsList = newsList
notifyDataSetChanged()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.codingwithjks.retrofit.Listener

interface Listener {
fun onItemClickListener(position:Int)
fun onItemClickListener(position: Int)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package com.codingwithjks.newsfeedapp.Network

object BaseUrl {

const val baseUrl:String="http://newsapi.org/v2/"
const val baseUrl: String = "http://newsapi.org/v2/"
}
14 changes: 8 additions & 6 deletions app/src/main/java/com/codingwithjks/retrofit/Network/NewsApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import retrofit2.http.GET
import retrofit2.http.Query

interface NewsApi {
//http://newsapi.org/v2/everything?q=bitcoin&from=2020-07-17&sortBy=publishedAt&apiKey=156c7ce8e18b47b98a0a459cb348684f
//http://newsapi.org/v2/everything?q=bitcoin&from=2020-09-17&sortBy=publishedAt&apiKey=156c7ce8e18b47b98a0a459cb348684f

@GET("everything/")
fun getNews(@Query("q")q:String,
@Query("apiKey")
apiKey:String,
@Query("from") from:String,
@Query("sortBy") publishedAt:String
fun getNews(
@Query("q") q: String,
@Query("apiKey")
apiKey: String,
@Query("from") from: String,
@Query("sortBy") publishedAt: String

): Call<News>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import retrofit2.converter.gson.GsonConverterFactory

object RetrofitBuilder {

private fun getRetrofit():Retrofit= Retrofit.Builder()
private fun getRetrofit(): Retrofit = Retrofit.Builder()
.baseUrl(BaseUrl.baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build()

val newApi:NewsApi= getRetrofit().create(NewsApi::class.java)

val newApi: NewsApi = getRetrofit().create(NewsApi::class.java)
}
47 changes: 26 additions & 21 deletions app/src/main/java/com/codingwithjks/retrofit/Ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,49 @@ import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MainActivity : AppCompatActivity(),Listener {
class MainActivity : AppCompatActivity(), Listener {
private lateinit var recyclerView: RecyclerView
private lateinit var newsAdapter: NewsAdapter
private lateinit var articlesList: ArrayList<Articles>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setUpUi()
fetchData()
}



private fun setUpUi() {
recyclerView=findViewById(R.id.recyclerView)
newsAdapter= NewsAdapter(this,ArrayList<Articles>(),this)
recyclerView = findViewById(R.id.recyclerView)
newsAdapter = NewsAdapter(this, ArrayList<Articles>(), this)
recyclerView.apply {
setHasFixedSize(true)
layoutManager= LinearLayoutManager(this@MainActivity)
adapter=newsAdapter
layoutManager = LinearLayoutManager(this@MainActivity)
adapter = newsAdapter
}
}

private fun fetchData() {
val call:Call<News> = RetrofitBuilder.newApi.getNews("bitcoin","156c7ce8e18b47b98a0a459cb348684f","2020-07-17","publishedAt")
call.enqueue(object :Callback<News> {
val call: Call<News> = RetrofitBuilder.newApi.getNews(
"google",
"156c7ce8e18b47b98a0a459cb348684f",
"2020-09-17",
"publishedAt"
)
call.enqueue(object : Callback<News> {

override fun onResponse(call: Call<News>, response: Response<News>) {
if(response.isSuccessful)
{
val listArticles=response.body()?.articles
if(listArticles!=null && listArticles.isNotEmpty()){
newsAdapter.setData(listArticles as ArrayList<Articles>)
progressBar.visibility=View.GONE
recyclerView.visibility=View.VISIBLE
}
articlesList= response.body()?.articles as ArrayList<Articles>
}
if (response.isSuccessful) {
val listArticles = response.body()?.articles
if (listArticles != null && listArticles.isNotEmpty()) {
newsAdapter.setData(listArticles as ArrayList<Articles>)
progressBar.visibility = View.GONE
recyclerView.visibility = View.VISIBLE
}
articlesList = response.body()?.articles as ArrayList<Articles>
Log.d("main", response.body().toString())

}
}

override fun onFailure(call: Call<News>, t: Throwable) {
Expand All @@ -66,8 +71,8 @@ class MainActivity : AppCompatActivity(),Listener {
}

override fun onItemClickListener(position: Int) {
val intent=Intent(this, WebActivity::class.java)
intent.putExtra("url",articlesList[position].url)
val intent = Intent(this, WebActivity::class.java)
intent.putExtra("url", articlesList[position].url)
startActivity(intent)
}
}
12 changes: 6 additions & 6 deletions app/src/main/java/com/codingwithjks/retrofit/Ui/WebActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import com.codingwithjks.retrofit.R
import kotlinx.android.synthetic.main.activity_web.*

class WebActivity : AppCompatActivity() {
private lateinit var url:String
private lateinit var url: String

@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_web)
val bundle=intent.extras
if(bundle != null){
url= bundle.getString("url").toString()
val bundle = intent.extras
if (bundle != null) {
url = bundle.getString("url").toString()
}
webview.loadUrl(url)

webview.loadUrl(url)
}
}
Binary file not shown.
Binary file not shown.
19 changes: 9 additions & 10 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@
android:orientation="vertical"
tools:context=".Ui.MainActivity">


<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
tools:listitem="@layout/each_row"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:layout_height="match_parent"
android:visibility="gone"
tools:listitem="@layout/each_row" />

<ProgressBar
android:layout_gravity="center"
android:layout_marginTop="300dp"
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="300dp" />

</LinearLayout>
9 changes: 5 additions & 4 deletions app/src/main/res/layout/activity_web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Ui.WebActivity">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Loading