We all want to set some photo that we have captured with our camera, some moment, on our home screen. This can be done through our app as well. This is an example where the user can set some image as background using phones image gallery. In very few steps, this procedure will be explained in detail how to implement in your project.

NOTE: We are going to use ButterKnife Library also, for binding the Views from the XML, like buttons, text views, edit text etc in our java classes.
ButterKnife by Jake Wharton Library with examples

 

At first, we must create a new project in Android Studio. After that in our example, everything is happening in our MainActivity. It is a simple example of selecting image from gallery, setting on our screen in the application for preview, and setting the same image as phones background.

MainActivity:

First section is the Binding the views with the XML resources. This is done throughout the ButterKnife library that i strongly recommend for using. Than is the static int variable, RESULT_LOAD_IMAGE which is used for onActivityResult() for getting the image form the Gallery.

In onCreate() we are adding only the ButterKnife library for binding the XML views in our class.

With @onClick annotation (another ButterKnife feature) we set the intent where we want to select image from the gallery. The intent starts the phones gallery for us to choose from. The previously declared and initialed static int number is used as a code for retrieving the image later in our activity., when we select the wanted image.

In onActivityResault(), we get the selected image and we set on the ImageView for preview. Also, here we use rotateImageIfRequired() method for image rotating if necessary.

In the end, on setAsWallpaper() button click, the image will be set as phones background or wallpaper.

And also, the MainActivity layout file:

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *