Professional Documents
Culture Documents
Key Components:
1. activity_web.xml:
- Defines the layout for the WebActivity, containing a WebView to
display web pages.
2. activity_main.xml:
- Defines the layout for the main activity, including a RecyclerView
to display a list of news articles.
- Includes a ProgressBar to indicate loading while fetching news.
3. article_item.xml:
- Defines the layout for each item in the RecyclerView.
- Contains TextViews for the article title, description, author, date,
and an ANImageView for displaying the article thumbnail.
4. NewsArticle.java:
- Model class representing a news article.
- Contains properties such as author, title, description, URL, URL to
image, published date, and content.
- Provides getter and setter methods for accessing and setting
these properties.
5. WebActivity.java:
- Activity class responsible for displaying web pages.
- Loads the URL passed via Intent into the WebView.
6. ArticleAdapter.java:
- RecyclerView adapter responsible for binding news articles to the
RecyclerView.
- Inflates the article_item.xml layout for each item.
- Binds data to the views and handles click events to open articles
in WebActivity.
7. MainActivity.java:
- Main activity class responsible for initializing the application and
fetching news
articles from the NewsAPI.
- Uses Fast Android Networking Library to make GET requests to
the NewsAPI.
- Parses the JSON response and populates the RecyclerView with
news articles using the ArticleAdapter.
Tech Stack:
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=".WebActivity">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<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"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
<ProgressBar
android:id="@+id/progressbar_id"
android:layout_width="wrap_content"
android:indeterminateTint="#0F9D58"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_height="wrap_content">
</ProgressBar>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:gravity="center"
android:weightSum="10"
android:orientation="horizontal"
android:layout_height="wrap_content">
android:id="@+id/image_id"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_weight="7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/title_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:textStyle="bold"
android:textColor="#0F9D58"
android:textSize="14sp"/>
<TextView
android:id="@+id/description_id"
android:layout_marginTop="2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:fontFamily="sans-serif-medium"
android:textAllCaps="false"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/contributordate_id"
android:layout_marginTop="1dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textSize="12sp" />
<View
android:layout_width="match_parent"
android:layout_marginTop="3dp"
android:layout_marginBottom="5dp"
android:layout_height="0.2dp"
android:background="#0F9D58"/>
// the properties/attributes
// of the ArticleModel
String author;
String title;
String description;
String url;
String urlToImage;
String publishedAt;
String content;
public NewsArticle() {
return author;
this.author = author;
return title;
this.title = title;
}
// get method : returns the description of the article
return description;
this.description = description;
return url;
this.url = url;
return urlToImage;
this.urlToImage = urlToImage;
}
// get method : returns the date
return publishedAt;
this.publishedAt = publishedAt;
return content;
this.content = content;
}import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
if(intent!=null){
url = intent.getStringExtra("url_key");
myWebView.loadUrl(url);
@Override
super.onRestart();
finish();
}
}import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.androidnetworking.widget.ANImageView;
import java.util.ArrayList;
this.mContext=context;
this.mArrayList=list;
@NonNull
@Override
View view=LayoutInflater.from(mContext).inflate(R.layout.article_item,parent,false);
@Override
// getting the current article from the ArrayList using the position
NewsArticle currentArticle=mArrayList.get(position);
holder.title.setText(currentArticle.getTitle());
holder.description.setText(currentArticle.getDescription());
holder.contributordate.setText(currentArticle.getAuthor()+
" | "+currentArticle.getPublishedAt().substring(0,10));
holder.image.setDefaultImageResId(R.drawable.ic_launcher_background);
holder.image.setErrorImageResId(R.drawable.ic_launcher_foreground);
holder.image.setImageUrl(currentArticle.getUrlToImage());
holder.image.setContentDescription(currentArticle.getContent());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
intent.putExtra("url_key",currentArticle.getUrl());
mContext.startActivity(intent);
});
@Override
return mArrayList.size();
super(itemView);
title=itemView.findViewById(R.id.title_id);
description=itemView.findViewById(R.id.description_id);
image=itemView.findViewById(R.id.image_id);
contributordate=itemView.findViewById(R.id.contributordate_id);
}
}import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.common.Priority;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.JSONObjectRequestListener;
import com.jacksonandroidnetworking.JacksonParserFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AndroidNetworking.initialize(getApplicationContext());
AndroidNetworking.setParserFactory(new JacksonParserFactory());
mProgressBar=(ProgressBar)findViewById(R.id.progressbar_id);
mRecyclerView=(RecyclerView)findViewById(R.id.recyclerview_id);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mArticleList=new ArrayList<>();
// calling get_news_from_api()
get_news_from_api();
AndroidNetworking.get("https://newsapi.org/v2/top-headlines")
.addQueryParameter("country", "in")
.addQueryParameter("apiKey",API_KEY)
.addHeaders("token", "1234")
.setTag("test")
.setPriority(Priority.LOW)
.build()
.getAsJSONObject(new JSONObjectRequestListener(){
@Override
mProgressBar.setVisibility(View.GONE);
try {
JSONArray articles=response.getJSONArray("articles");
String author=article.getString("author");
String title=article.getString("title");
String description=article.getString("description");
String url=article.getString("url");
String urlToImage=article.getString("urlToImage");
String publishedAt=article.getString("publishedAt");
String content=article.getString("content");
currentArticle.setAuthor(author);
currentArticle.setTitle(title);
currentArticle.setDescription(description);
currentArticle.setUrl(url);
currentArticle.setUrlToImage(urlToImage);
currentArticle.setPublishedAt(publishedAt);
currentArticle.setContent(content);
mArticleList.add(currentArticle);
mArticleAdapter=new ArticleAdapter(getApplicationContext(),mArticleList);
mRecyclerView.setAdapter(mArticleAdapter);
} catch (JSONException e) {
e.printStackTrace();
Log.d(TAG,"Error : "+e.getMessage());
@Override
});
Conclusion:
The Android news application demonstrates the integration of
network data fetching, JSON parsing, and RecyclerView usage to
create a user-friendly interface for browsing news articles. By
leveraging various Android components and libraries, the application
provides a seamless experience for users to explore and read the
latest news content.<?xml version="1.0" encoding="utf-8"?>
OUTPUT: