Android code for news applicaion free source code
Here is a free source code for a news application in Android:
News App Source Code
This code is a basic implementation of a news app that fetches news articles from a JSON API and displays them in a list view. The app also allows users to favorite articles and view their favorite articles in a separate list.
Files:
MainActivity.java
: The main activity of the app that handles the UI and fetches news articles.NewsAdapter.java
: An adapter class that binds the news articles to the list view.NewsArticle.java
: A POJO class that represents a news article.R.java
: The Android resource file that contains the layout and string resources.strings.xml
: The string resource file that contains the app's strings.activity_main.xml
: The layout file for the main activity.news_article.xml
: The layout file for a single news article.
Code:
MainActivity.java:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONObject;
public class MainActivity extends Activity {
private ListView listView;
private NewsAdapter adapter;
private RequestQueue queue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.list_view);
adapter = new NewsAdapter(this);
listView.setAdapter(adapter);
queue = Volley.newRequestQueue(this);
// Fetch news articles
String url = "https://newsapi.org/v2/top-headlines?country=us&apiKey=YOUR_API_KEY";
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
adapter.clear();
for (int i = 0; i < response.length(); i++) {
JSONObject article = response.getJSONObject(i);
NewsArticle newsArticle = new NewsArticle(article.getString("title"), article.getString("description"), article.getString("url"));
adapter.add(newsArticle);
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Error fetching news articles", Toast.LENGTH_SHORT).show();
}
});
queue.add(request);
// Set up list view item click listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
NewsArticle newsArticle = adapter.getItem(position);
Toast.makeText(MainActivity.this, newsArticle.getTitle(), Toast.LENGTH_SHORT).show();
}
});
}
}
NewsAdapter.java:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class NewsAdapter extends ArrayAdapter<NewsArticle> {
private Context context;
private List<NewsArticle> articles;
public NewsAdapter(Context context) {
super(context, 0);
this.context = context;
this.articles = new ArrayList<>();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.news_article, parent, false);
}
NewsArticle article = articles.get(position);
TextView titleTextView = view.findViewById(R.id.title_text_view);
titleTextView.setText(article.getTitle());
TextView descriptionTextView = view.findViewById(R.id.description_text_view);
descriptionTextView.setText(article.getDescription());
return view;
}
public void add(NewsArticle article) {
articles.add(article);
}
public void clear() {
articles.clear();
}
}
NewsArticle.java:
public class NewsArticle {
private String title;
private String description;
private String url;
public NewsArticle(String title, String description, String url) {
this.title = title;
this.description = description;
this.url = url;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getUrl() {
return url;
}
}
R.java:
// activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
// news_article.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
<TextView
android:id="@+id/description_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp" />
</LinearLayout>
strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">News App</string>
</resources>
Note:
- Replace
YOUR_API_KEY
with your actual API key from newsapi.org. - This code is just a basic implementation and you may want to add more features such as image loading, error handling, and caching.
- You can customize the layout and design of the app by modifying the XML files.
- This code is free to use and modify, but please give credit to the original author if you use it in your own project.