Then you will need to add the WallpapersListFragment destination to the navigation graph as the start destination using the navigation editor in Android Studio shown in the screenshot below. First you will need to add a NavHostFragment to the layout resource of the main activity.Īfter that you will need to create a navigation resource for the navigation graph. The final part of this step of this section of the tutorial is to set up the WallpapersListFragment to be shown in the navigation graph within the activity. See the code sample for the WallpapersListFragment class below. In the onCreateView(…) method of the fragment we set up the RecylcerView by setting the layout manager and the adapter containing the list of wallpapers objects. Once the list is created we create the WallpaperGalleryRecyclerAdapter and pass the list of wallpapers to the custom RecyclerView adapter using the setWallpapers(List) method. You will see in the onCreate(…) method of the fragment class we are creating a List of Wallpaper objects using wallpaper images from the free to use stock photo website. Now we will create the fragment class that will contain the RecyclerView of the wallpapers. See the code sample for the WallpaperGalleryRecyclerAdapter class below. Next we will create the RecyclerView adapter class for showing the wallpapers in the RecyclerView. See the code sample for the WallpaperViewHolder class below. Set an OnClickListener to the ImageView containing the wallpaper to invoke the onWallpaperSelect(Wallpaper) method on the WallpaperSelectListener.Load the image of the wallpaper from the Internet into the ImageView.Set the text of the wallpaper title TextView.In the bind(…) method of the WallpaperViewHolder we will perform the following on the wallpaper card. Now we will create a ViewHolder class to be used within the RecyclerView for the showing each card containing a wallpaper. See the code sample for the WallpaperSelectListener interface below. Next we will create an interface that will contain methods that will be invoked when a wallpaper image is selected within any of the cards in the RecyclerView. See the code sample for the Wallpaper class below. We will first create a model class for the Wallpaper that will contain a String for the URI of the image on the Internet and a String for the title of the wallpaper. This RecyclerView will show a card for each wallpaper, each card will contain an image of the wallpaper (loaded using Glide) and a textual description. In this section of the tutorial we will be setting up a fragment inside an activity that will contain a RecyclerView. Step 3: Setting up RecyclerView to Display a Gallery of Wallpapers See the sample app level adle containing the dependencies for the Jetpack navigation component and Glide. To learn more about the Jetpack navigation component and how to use it to move between fragments check out the post I wrote below.Īndroid Glide Tutorial with Examples in Java We will also be using the image loading library Glide to load wallpaper images from the internet. In this tutorial for building an app to set the wallpaper on the device we will be using Jetpack navigation component for navigating between fragments in our Android app. See a sample Android Manifest below requesting the INTERNET and SET_WALLPAPER permissions for use in the wallpaper Android app. To set an image as the wallpaper on the Android device we will require the SET_WALLPAPER permission to be granted in the Android manifest. We will need to request the following two Android permissions in the manifest file for our Android wallpaper app to work.Īs will be loading images for the internet to be used for selecting the wallpaper we will need to the INTERNET permission enabled. Step 1: Requesting Permissions in the Android Manifest
0 Comments
Leave a Reply. |