Create News Application Android Studio – Here i will show how to create android news application in android studio. There are more popular news channel will be there but here you can also develop the updated & Create news app in your android platform using API key.
I have used New York Times API key to get latest news updates, it’s regularly update all topic like social, sports, entertainment news and more. So you have just get the API key and insert from our android news application project. After that the API key automatically fetch all news and show from android application feed.
Create News Application Android Studio
Let’s start to develop android news application. First very important to get API key from New York Times websites. So in below i give link for how to get API key for your project create news app. Open the link and just register your account via enter email. After that they send API key from your registered email-id. Copy the API key and paste from your project (build.gradle file).
Get API key
Link for API website- https://newsapi.org
Click here to Get NYT API Key – New York Times APIs. Just fill that details in the link they ask your Name and Email-id after fill the SignUP form you get API key.
Create News App
As usual File=>create news app project and choose Empty Activity, after creating the project open the default class of MainActivity.java file and add the following below code,
MainActivity.java
package com.dude.newsapp;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
String API_KEY = "8190df9eb51445228e397e4185311a66"; // ### YOUE NEWS API HERE ###
String NEWS_SOURCE = "techcrunch"; // Other news source code at: https://newsapi.org/sources
ListView listNews;
ProgressBar loader;
ArrayList<HashMap<String, String>> dataList = new ArrayList<>();
static final String KEY_AUTHOR = "author";
static final String KEY_TITLE = "title";
static final String KEY_DESCRIPTION = "description";
static final String KEY_URL = "url";
static final String KEY_URLTOIMAGE = "urlToImage";
static final String KEY_PUBLISHEDAT = "publishedAt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listNews = findViewById(R.id.listNews);
loader = findViewById(R.id.loader);
listNews.setEmptyView(loader);
if (Function.isNetworkAvailable(getApplicationContext())) {
DownloadNews newsTask = new DownloadNews();
newsTask.execute();
} else {
Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_LONG).show();
}
}
class DownloadNews extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() { super.onPreExecute(); }
protected String doInBackground(String... args) {
String xml = Function.excuteGet("https://newsapi.org/v1/articles?source=" + NEWS_SOURCE + "&sortBy=top&apiKey=" + API_KEY);
return xml;
}
@Override
protected void onPostExecute(String xml) {
if (xml.length() > 10) { // Just checking if not empty
try {
JSONObject jsonResponse = new JSONObject(xml);
JSONArray jsonArray = jsonResponse.optJSONArray("articles");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
HashMap<String, String> map = new HashMap<>();
map.put(KEY_AUTHOR, jsonObject.optString(KEY_AUTHOR));
map.put(KEY_TITLE, jsonObject.optString(KEY_TITLE));
map.put(KEY_DESCRIPTION, jsonObject.optString(KEY_DESCRIPTION));
map.put(KEY_URL, jsonObject.optString(KEY_URL));
map.put(KEY_URLTOIMAGE, jsonObject.optString(KEY_URLTOIMAGE));
map.put(KEY_PUBLISHEDAT, jsonObject.optString(KEY_PUBLISHEDAT));
dataList.add(map);
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Unexpected error", Toast.LENGTH_SHORT).show();
}
ListNewsAdapter adapter = new ListNewsAdapter(MainActivity.this, dataList);
listNews.setAdapter(adapter);
listNews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(MainActivity.this, DetailsActivity.class);
i.putExtra("url", dataList.get(+position).get(KEY_URL));
startActivity(i);
}
});
} else {
Toast.makeText(getApplicationContext(), "No news found", Toast.LENGTH_SHORT).show();
}
}
}
}
ListNewsAdapter.java
package com.dude.newsapp;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
//import com.squareup.picasso.Picasso;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.HashMap;
class ListNewsAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
public ListNewsAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ListNewsViewHolder holder = null;
if (convertView == null) {
holder = new ListNewsViewHolder();
convertView = LayoutInflater.from(activity).inflate(
R.layout.list_row, parent, false);
holder.galleryImage = (ImageView) convertView.findViewById(R.id.galleryImage);
holder.author = (TextView) convertView.findViewById(R.id.author);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.sdetails = (TextView) convertView.findViewById(R.id.sdetails);
holder.time = (TextView) convertView.findViewById(R.id.time);
convertView.setTag(holder);
} else {
holder = (ListNewsViewHolder) convertView.getTag();
}
holder.galleryImage.setId(position);
holder.author.setId(position);
holder.title.setId(position);
holder.sdetails.setId(position);
holder.time.setId(position);
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
try{
holder.author.setText(song.get(MainActivity.KEY_AUTHOR));
holder.title.setText(song.get(MainActivity.KEY_TITLE));
holder.time.setText(song.get(MainActivity.KEY_PUBLISHEDAT));
holder.sdetails.setText(song.get(MainActivity.KEY_DESCRIPTION));
if(song.get(MainActivity.KEY_URLTOIMAGE).toString().length() < 5)
{
holder.galleryImage.setVisibility(View.GONE);
}else{
Picasso.get()
.load(song.get(MainActivity.KEY_URLTOIMAGE))
.resize(300, 200)
.centerCrop()
.into(holder.galleryImage);
}
}catch(Exception e) {}
return convertView;
}
}
class ListNewsViewHolder {
ImageView galleryImage;
TextView author, title, sdetails, time;
}
NOTE- Remaining all code are given in Source code click and download below
In this project we need to create more java classes and XML files to create perfect android create news app in android studio. So here finally i give the full source code of this project because not enough space to explain all java classes in this article.
Make browser app click here.
ADD Dependency on gradle file
implementation 'com.squareup.picasso:picasso:2.71828'
gradle.app
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.dude.newsapp"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.squareup.picasso:picasso:2.71828'
}
Activity Layouts
Need to integrate XML files into java clsses to make android news application. Open under the path of res =>layout =>activity_main.xml file and add the following code,
YouTube Video
Download Source Code
Click below to get the source code android news application.
Download News Apk: CLICK ME
https://www.mediafire.com/file/ka3m455yi36kwba/news_app.apk/file
Download News Logo: CLICK ME
Click below to get the full source code android news application.
Conclusion
We have successfully created a News App Android application using Android Studio.