Android code for news template
Here is an example of an Android news template using a RecyclerView and a CardView:
news_template.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">
<androidx.cardview.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:backgroundTint="@color/cardview_light_background"
android:elevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp" />
<TextView
android:id="@+id/summary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
NewsAdapter.java
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
private List<NewsItem> newsItems;
public NewsAdapter(List<NewsItem> newsItems) {
this.newsItems = newsItems;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_template, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
NewsItem newsItem = newsItems.get(position);
holder.title.setText(newsItem.getTitle());
holder.date.setText(newsItem.getDate());
holder.summary.setText(newsItem.getSummary());
}
@Override
public int getItemCount() {
return newsItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public TextView date;
public TextView summary;
public ViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
date = itemView.findViewById(R.id.date);
summary = itemView.findViewById(R.id.summary);
}
}
}
NewsItem.java
public class NewsItem {
private String title;
private String date;
private String summary;
public NewsItem(String title, String date, String summary) {
this.title = title;
this.date = date;
this.summary = summary;
}
public String getTitle() {
return title;
}
public String getDate() {
return date;
}
public String getSummary() {
return summary;
}
}
MainActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private NewsAdapter newsAdapter;
private List<NewsItem> newsItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
newsItems = new ArrayList<>();
newsItems.add(new NewsItem("Title 1", "2022-01-01", "Summary 1"));
newsItems.add(new NewsItem("Title 2", "2022-01-02", "Summary 2"));
newsItems.add(new NewsItem("Title 3", "2022-01-03", "Summary 3"));
newsAdapter = new NewsAdapter(newsItems);
recyclerView.setAdapter(newsAdapter);
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(RecyclerView rv) {
}
});
}
}
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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
This is a basic example of a news template using a RecyclerView and a CardView. You can customize the layout and the adapter to fit your specific needs.
Note: This is just an example, you will need to replace the newsItems
list with your actual data and also you will need to implement the logic to fetch the data from your API or database.
Also, you can add more functionality to the adapter, such as clicking on the item to open a detail activity, or swiping to delete the item.
You can also use other libraries such as Glide
to load images and Picasso
to load images.
You can also use Room
persistence library to store the data locally.
You can also use Retrofit
to fetch the data from API.