top of page

How To Develop A Local News App in Android

Introduction

The Local News App shows users local news of their area. There are users who can add news to the app and the app will reflect the news to the users using the app. Users can add images related to News, details of News, author name, etc. Application uses authentication to authenticate users.


Concepts Used

The major concepts application uses are Realtime Database, Storage, Authentication, Creating Custom rows in RecyclerView, Uploading Image, etc.


  • Realtime Database: In our application we save news title, description, date, author name, etc in realtime database which firebase provides. We created a model class which defines what are the fields we are going to store in our realtime database. Realtime Database uses Database Reference, A Reference represents a specific location in your Database and can be used for reading or writing data to that Database location. You can reference the root or child location in your Database by calling firebase. database(). ... ref("child/path") . We can save our data in this reference by creating an object of the model class and then instantiating it and putting it in realtime database.


  • Storage: Cloud Storage for Firebase stores your files in a Google Cloud Storage bucket, making them accessible through both Firebase and Google Cloud. This allows you the flexibility to upload and download files from mobile clients via the Firebase SDKs for Cloud Storage. Our application also asks for getting image from the device which we save in bucket of storage. And provide its reference to our realtime database so that we can access it later while displaying news items. We also use a library called Picasso, which is a powerful image downloading and caching library for Android. So we use it while accessing image reference stored in Realtime Database, and then show it in recycler view.


  • Authentication: Firebase Authentication provides backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to your app. It supports authentication using passwords, phone numbers, popular federated identity providers like Google, Facebook and Twitter, and more. We use login activity to validate users then let them add news or if it is admin who is logging in then we will let him move to activity where admin checks the authenticity of news and allow it to move to homepage news items.



  • Uploading Images: We get images from our device internal storage and then provide it for the news item. For getting images from device internal storage we use Action_Get_Content. ACTION_GET_CONTENT with MIME type */* and category CATEGORY_OPENABLE -- Display all pickers for data that can be opened withContentResolver.openInputStream(), allowing the user to pick one of them and then some data inside of it and returning the resulting URI to the caller. This can be used, for example, in an e-mail application to allow the user to pick some data to include as an attachment.



Code Snippet:

public void getPickImageIntent(){
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    startActivityForResult(intent, PICK_IMAGES);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == PICK_IMAGES) {
            if (data.getClipData() != null) {
                ClipData mClipData = data.getClipData();
                for (int i = 0; i < mClipData.getItemCount(); i++) {
                    ClipData.Item item = mClipData.getItemAt(i);
                    Uri uri = item.getUri();
                    // display your images
                    imageView.setImageURI(uri);
                }
            } else if (data.getData() != null) {
                Uri uri = data.getData();
                // display your image
                imageView.setImageURI(uri);
            }
        }
    }
}                       

We save this image uri to our Firebase storage and it’s reference to the realtime database.


  • Recycler View: Our Application needs to have two different activities where one shows items containing Image, Title and date of the news and other activity contains news items with their summary which need to be validated by the admin. So we created two different java classes to attain this, and two different adapters to display our news items. Once you've determined your layout, you need to implement your Adapter and ViewHolder. These two classes work together to define how your data is displayed. The ViewHolder is a wrapper around a View that contains the layout for an individual item in the list. The Adapter creates ViewHolder objects as needed, and also sets the data for those views. The process of associating views to their data is called binding.

When you define your adapter, you need to override three key methods:

  • onCreateViewHolder(): RecyclerView calls this method whenever it needs to create a new ViewHolder. The method creates and initializes the ViewHolder and its associated View, but does not fill in the view's contents—the ViewHolder has not yet been bound to specific data.

  • onBindViewHolder(): RecyclerView calls this method to associate a ViewHolder with data. The method fetches the appropriate data and uses the data to fill in the view holder's layout. For example, if the RecyclerView displays a list of names, the method might find the appropriate name in the list and fill in the view holder's TextView widget.

  • getItemCount(): RecyclerView calls this method to get the size of the data set. For example, in an address book app, this might be the total number of addresses. RecyclerView uses this to determine when there are no more items that can be displayed.



if you want complete source code of this application please contact us we'll provide this app at minimum development charges.


If you are looking for Android Assignment Help or Homework Help? Codersarts Android Assignment help expert will provides best quality plagiarism free solution at affordable price. We are available 24 * 7 online to assist you. You may chat with us through website chat or email or can fill contact form.

bottom of page