0% found this document useful (0 votes)
75 views19 pages

Android News Application With News Api: Project Overview

The document describes an Android news application that fetches news articles from the NewsAPI. It includes layout files, model classes and activity classes to display articles in a recycler view and view web pages. Key components are described along with the tech stack which includes Android SDK, Java, XML, and libraries like RecyclerView and Fast Android Networking.

Uploaded by

kamatekk19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views19 pages

Android News Application With News Api: Project Overview

The document describes an Android news application that fetches news articles from the NewsAPI. It includes layout files, model classes and activity classes to display articles in a recycler view and view web pages. Key components are described along with the tech stack which includes Android SDK, Java, XML, and libraries like RecyclerView and Fast Android Networking.

Uploaded by

kamatekk19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

ANDROID NEWS APPLICATION

WITH NEWS API


Project Overview:
The provided code snippets constitute an Android application
designed to fetch news articles from the NewsAPI and display them
in a RecyclerView. The application consists of several key
components, including layout files, model classes, and activity
classes.

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:

- Android SDK: Used for developing the Android application.


- Java: Primary programming language for implementing application
logic.
- XML: Used for defining layouts and UI elements.
- RecyclerView: Used to efficiently display a list of news articles.
- WebView: Utilized to display web pages within the application.
- Fast Android Networking Library: Used for making HTTP requests
and handling JSON responses.
PROGRAM:
<LinearLayout

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 to display web pages

as part of the activity -->

<WebView

android:id="@+id/webview"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

</LinearLayout><?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"

tools:context=".MainActivity">

<!-- A RecyclerView to efficiently

display a list of articles -->

<androidx.recyclerview.widget.RecyclerView

android:id="@+id/recyclerview_id"

android:layout_width="match_parent"

android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>

<!-- loading spinner to be displayed

while the library fetches news -->

<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>

</RelativeLayout><?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:orientation="vertical"

android:layout_height="wrap_content">

<!-- horizontal LinearLayout containing

image and article details -->

<LinearLayout

android:layout_width="match_parent"

android:gravity="center"

android:weightSum="10"

android:orientation="horizontal"

android:layout_height="wrap_content">

<!-- Image of news thumbnail -->

<!-- ANImageView is a view provided by

Fast Android Networking Library -->


<com.androidnetworking.widget.ANImageView

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">

<!-- Text with title -->

<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"/>

<!-- Text with description name -->

<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>

<!-- Text with author name and date -->

<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 showing thin green horizontal

line separating articles -->

<View

android:layout_width="match_parent"

android:layout_marginTop="3dp"

android:layout_marginBottom="5dp"

android:layout_height="0.2dp"

android:background="#0F9D58"/>

</LinearLayout>public class NewsArticle {

// the properties/attributes

// of the ArticleModel
String author;

String title;

String description;

String url;

String urlToImage;

String publishedAt;

String content;

// ArticleModel empty constructor

public NewsArticle() {

// get method : returns the name of the author

public String getAuthor() {

return author;

// set method : sets the author

public void setAuthor(String author) {

this.author = author;

// get method : returns the title of the article

public String getTitle() {

return title;

// set method : sets the title

public void setTitle(String title) {

this.title = title;

}
// get method : returns the description of the article

public String getDescription() {

return description;

// set method : sets the description

public void setDescription(String description) {

this.description = description;

// get method : returns url of the article

public String getUrl() {

return url;

// set method : sets the url of the article

public void setUrl(String url) {

this.url = url;

// get method : returns the urlToImage of the article

public String getUrlToImage() {

return urlToImage;

// set method : sets the UrlToImage

public void setUrlToImage(String urlToImage) {

this.urlToImage = urlToImage;

}
// get method : returns the date

public String getPublishedAt() {

return publishedAt;

// set method : sets the date

public void setPublishedAt(String publishedAt) {

this.publishedAt = publishedAt;

// get method : returns the content of the article

public String getContent() {

return content;

// set method : sets the title

public void setContent(String 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;

public class WebActivity extends AppCompatActivity {

// setting the TAG for debugging purposes

private static String TAG="WebActivity";

// declaring the webview

private WebView myWebView;


// declaring the url string variable

private String url;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_web);

// assigning the views to id's

myWebView = (WebView) findViewById(R.id.webview);

Intent intent = getIntent();

// checking if there is an intent

if(intent!=null){

// retrieving the url in the intent

url = intent.getStringExtra("url_key");

// loading and displaying a

// web page in the activity

myWebView.loadUrl(url);

@Override

protected void onRestart() {

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;

public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ViewHolder>


{

// setting the TAG for debugging purposes

private static String TAG="ArticleAdapter";

private ArrayList<NewsArticle> mArrayList;

private Context mContext;

public ArticleAdapter(Context context,ArrayList<NewsArticle> list){

// initializing the constructor

this.mContext=context;

this.mArrayList=list;

@NonNull

@Override

public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

// inflating the layout with the article view (R.layout.article_item)

View view=LayoutInflater.from(mContext).inflate(R.layout.article_item,parent,false);

return new ViewHolder(view);


}

@Override

public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

// the parameter position is the index of the current article

// getting the current article from the ArrayList using the position

NewsArticle currentArticle=mArrayList.get(position);

// setting the text of textViews

holder.title.setText(currentArticle.getTitle());

holder.description.setText(currentArticle.getDescription());

// subString(0,10) trims the date to make it short

holder.contributordate.setText(currentArticle.getAuthor()+

" | "+currentArticle.getPublishedAt().substring(0,10));

// Loading image from network into

// Fast Android Networking View ANImageView

holder.image.setDefaultImageResId(R.drawable.ic_launcher_background);

holder.image.setErrorImageResId(R.drawable.ic_launcher_foreground);

holder.image.setImageUrl(currentArticle.getUrlToImage());

// setting the content Description on the Image

holder.image.setContentDescription(currentArticle.getContent());

// handling click event of the article

holder.itemView.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

// an intent to the WebActivity that display web pages

Intent intent = new Intent(mContext, WebActivity.class);


intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent.putExtra("url_key",currentArticle.getUrl());

// starting an Activity to display the page of the article

mContext.startActivity(intent);

});

@Override

public int getItemCount() {

return mArrayList.size();

public class ViewHolder extends RecyclerView.ViewHolder{

// declaring the views

private TextView title,description,contributordate;

private ANImageView image;

public ViewHolder(@NonNull View itemView) {

super(itemView);

// assigning views to their ids

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;

public class MainActivity extends AppCompatActivity {

// TODO : set the API_KEY variable to your api key

private static String API_KEY="";

// setting the TAG for debugging purposes

private static String TAG="MainActivity";

// declaring the views

private ProgressBar mProgressBar;

private RecyclerView mRecyclerView;

// declaring an ArrayList of articles


private ArrayList<NewsArticle> mArticleList;

private ArticleAdapter mArticleAdapter;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// initializing the Fast Android Networking Library

AndroidNetworking.initialize(getApplicationContext());

// setting the JacksonParserFactory

AndroidNetworking.setParserFactory(new JacksonParserFactory());

// assigning views to their ids

mProgressBar=(ProgressBar)findViewById(R.id.progressbar_id);

mRecyclerView=(RecyclerView)findViewById(R.id.recyclerview_id);

// setting the recyclerview layout manager

mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

// initializing the ArrayList of articles

mArticleList=new ArrayList<>();

// calling get_news_from_api()

get_news_from_api();

public void get_news_from_api(){

// clearing the articles list before adding news ones


mArticleList.clear();

// Making a GET Request using Fast

// Android Networking Library

// the request returns a JSONObject containing

// news articles from the news api

// or it will return an error

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

public void onResponse(JSONObject response) {

// disabling the progress bar

mProgressBar.setVisibility(View.GONE);

// handling the response

try {

// storing the response in a JSONArray

JSONArray articles=response.getJSONArray("articles");

// looping through all the articles

// to access them individually

for (int j=0;j<articles.length();j++)

// accessing each article object in the JSONArray


JSONObject article=articles.getJSONObject(j);

// initializing an empty ArticleModel

NewsArticle currentArticle=new NewsArticle();

// storing values of the article object properties

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");

// setting the values of the ArticleModel

// using the set methods

currentArticle.setAuthor(author);

currentArticle.setTitle(title);

currentArticle.setDescription(description);

currentArticle.setUrl(url);

currentArticle.setUrlToImage(urlToImage);

currentArticle.setPublishedAt(publishedAt);

currentArticle.setContent(content);

// adding an article to the articles List

mArticleList.add(currentArticle);

// setting the adapter

mArticleAdapter=new ArticleAdapter(getApplicationContext(),mArticleList);

mRecyclerView.setAdapter(mArticleAdapter);
} catch (JSONException e) {

e.printStackTrace();

// logging the JSONException LogCat

Log.d(TAG,"Error : "+e.getMessage());

@Override

public void onError(ANError error) {

// logging the error detail and response to LogCat

Log.d(TAG,"Error detail : "+error.getErrorDetail());

Log.d(TAG,"Error response : "+error.getResponse());

});

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:

You might also like