Android news app tutorial

Here is a step-by-step tutorial on building a basic Android news app:

Step 1: Set up the project

Step 2: Design the user interface

Step 3: Create the data model

Step 4: Create the data source

Step 5: Implement the news feed

Step 6: Implement the article details

Step 7: Implement the navigation

Step 8: Test the app

Here is some sample code to get you started:

// NewsArticle.java
public class NewsArticle {
    private String title;
    private String summary;
    private String url;

    public NewsArticle(String title, String summary, String url) {
        this.title = title;
        this.summary = summary;
        this.url = url;
    }

    public String getTitle() {
        return title;
    }

    public String getSummary() {
        return summary;
    }

    public String getUrl() {
        return url;
    }
}

// NewsDataSource.java
public class NewsDataSource {
    private Retrofit retrofit;

    public NewsDataSource() {
        retrofit = new Retrofit.Builder()
               .baseUrl("https://api.example.com/")
               .addConverterFactory(GsonConverterFactory.create())
               .build();
    }

    public List<NewsArticle> getNewsArticles() {
        Call<List<NewsArticle>> call = retrofit.create(NewsApi.class).getNewsArticles();
        call.enqueue(new Callback<List<NewsArticle>>() {
            @Override
            public void onResponse(Call<List<NewsArticle>> call, Response<List<NewsArticle>> response) {
                List<NewsArticle> articles = response.body();
                // Process the articles
            }

            @Override
            public void onFailure(Call<List<NewsArticle>> call, Throwable t) {
                // Handle the error
            }
        });
    }
}

// NewsFeedAdapter.java
public class NewsFeedAdapter extends RecyclerView.Adapter<NewsFeedAdapter.ViewHolder> {
    private List<NewsArticle> articles;
    private Context context;

    public NewsFeedAdapter(List<NewsArticle> articles, Context context) {
        this.articles = articles;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        NewsArticle article = articles.get(position);
        holder.titleTextView.setText(article.getTitle());
        holder.summaryTextView.setText(article.getSummary());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Navigate to the article details
                Intent intent = new Intent(context, ArticleDetailsActivity.class);
                intent.putExtra("article", article);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return articles.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView titleTextView;
        public TextView summaryTextView;

        public ViewHolder(View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.title_text_view);
            summaryTextView = itemView.findViewById(R.id.summary_text_view);
        }
    }
}

// ArticleDetailsActivity.java
public class ArticleDetailsActivity extends AppCompatActivity {
    private NewsArticle article;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.article_details);

        article = getIntent().getParcelableExtra("article");

        TextView titleTextView = findViewById(R.id.title_text_view);
        titleTextView.setText(article.getTitle());

        TextView summaryTextView = findViewById(R.id.summary_text_view);
        summaryTextView.setText(article.getSummary());

        WebView webView = findViewById(R.id.web_view);
        webView.loadUrl(article.getUrl());
    }
}

This is just a basic example to get you started. You will need to customize the app to fit your specific requirements.