How do I create a news feed app on Android?

Prashant Verma

May 31, 2020

5 min read

In this article, we are going to create a simple News App in Android. Users would be able to read live news articles from different news sources in this APP. You should have basic knowledge of Java, OPPs, Activity, Fragment, anyone networking library, Layouts, and Recycler view in order to grasp this article.

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 {

    private static  String API_KEY="";

    private static String TAG="MainActivity";

    private ProgressBar mProgressBar;

    private RecyclerView mRecyclerView;

    private ArrayList<NewsArticle> mArticleList;

    private ArticleAdapter mArticleAdapter;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

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

        get_news_from_api();

    }

    public void get_news_from_api(){

        mArticleList.clear();

                .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) {

                        mProgressBar.setVisibility(View.GONE);

                        try {

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

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

                            {

                                JSONObject article=articles.getJSONObject(j);

                                NewsArticle currentArticle=new NewsArticle();

                                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

                    public void onError(ANError error) {

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

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

                    }

                });

    }

}

How do I create a news feed app?

Build a news app for Android and iOS in 3 easy steps – without coding!.
Name & logo. Choose a template, or a blank app and set the name and icons..
Set Features. Select features from the mobile app builder that suits best your News app needs..
Publish..

How do I create a Web and news app?

The first four steps you need to take to build a popular news app are the following:.
Decide on the type of app. First, decide if you're going to build a single-publication app or an aggregator. ... .
Choose a good example. ... .
Create a technical specification with requirements. ... .
Find the right app development company..

Does Android have a news app?

Open Google News To get started, download the Google News app for Android.

What is a news feed app?

These news apps collect articles from a large pool of sources, and incorporate different types of reporting, so instead of getting stories just from the New York Times or a local news station, you'll get a good mix. If you really only want updates from your regional newspaper, go ahead and download that dedicated app.

Postingan terbaru

LIHAT SEMUA