Android news app free source code
Here is a free source code for an Android news app:
News App Source Code
This is a basic news app that fetches news articles from a JSON API and displays them in a list view. The app includes features such as:
- News article list view with title, description, and image
- Article details view with title, description, and image
- Search functionality
- Swipe gestures for navigation
Code Structure
The code is divided into the following packages:
com.example.newsapp
: The main package for the appcom.example.newsapp.data
: The package for data storage and retrievalcom.example.newsapp.ui
: The package for user interface componentscom.example.newsapp.utils
: The package for utility functions
Code Files
Here are the main code files:
MainActivity.java
: The main activity that handles the app's UI and logicNewsAdapter.java
: The adapter class that binds data to the list viewNewsArticle.java
: The data model class for news articlesNewsAPI.java
: The API class that fetches news articles from the JSON APISearchFragment.java
: The fragment class that handles search functionalitystyles.xml
: The style file for the app's UIstrings.xml
: The string file for the app's resources
Code Snippets
Here are some code snippets to give you an idea of how the app works:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ListView listView;
private NewsAdapter adapter;
private NewsAPI api;
@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);
api = new NewsAPI();
api.getNewsArticles(new NewsAPI.OnNewsArticlesReceived() {
@Override
public void onNewsArticlesReceived(List<NewsArticle> articles) {
adapter.setNewsArticles(articles);
adapter.notifyDataSetChanged();
}
});
}
}
NewsAdapter.java
public class NewsAdapter extends BaseAdapter {
private Context context;
private List<NewsArticle> newsArticles;
public NewsAdapter(Context context) {
this.context = context;
this.newsArticles = new ArrayList<>();
}
public void setNewsArticles(List<NewsArticle> newsArticles) {
this.newsArticles = newsArticles;
}
@Override
public int getCount() {
return newsArticles.size();
}
@Override
public NewsArticle getItem(int position) {
return newsArticles.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.news_item, parent, false);
}
NewsArticle article = getItem(position);
TextView titleTextView = view.findViewById(R.id.title_text_view);
TextView descriptionTextView = view.findViewById(R.id.description_text_view);
ImageView imageView = view.findViewById(R.id.image_view);
titleTextView.setText(article.getTitle());
descriptionTextView.setText(article.getDescription());
imageView.setImageResource(article.getImageResourceId());
return view;
}
}
NewsArticle.java
public class NewsArticle {
private String title;
private String description;
private int imageResourceId;
public NewsArticle(String title, String description, int imageResourceId) {
this.title = title;
this.description = description;
this.imageResourceId = imageResourceId;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public int getImageResourceId() {
return imageResourceId;
}
}
NewsAPI.java
public class NewsAPI {
private static final String API_URL = "https://newsapi.org/v2/top-headlines?country=us&apiKey=YOUR_API_KEY";
public void getNewsArticles(OnNewsArticlesReceived listener) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(API_URL)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
listener.onNewsArticlesReceived(null);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseBody = response.body().string();
List<NewsArticle> articles = parseJson(responseBody);
listener.onNewsArticlesReceived(articles);
}
});
}
private List<NewsArticle> parseJson(String json) {
// Parse the JSON response and return a list of NewsArticle objects
}
}
SearchFragment.java
public class SearchFragment extends Fragment {
private EditText searchEditText;
private ListView searchListView;
private NewsAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.search_fragment, container, false);
searchEditText = view.findViewById(R.id.search_edit_text);
searchListView = view.findViewById(R.id.search_list_view);
adapter = new NewsAdapter(getContext());
searchListView.setAdapter(adapter);
searchEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
}
});
return view;
}
}
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">News App</string>
<string name="search_hint">Search for news articles</string>
</resources>
This is just a basic example to get you started. You will need to modify the code to fit your specific requirements and add more features as needed.
Note: This code uses the News API from newsapi.org, which is a free API that provides news articles from various sources. You will need to obtain an API key from the website and replace YOUR_API_KEY
with your actual API key.