Create presentation viewer for your Android📱 [use case]
Tags: tutorial, beginners, programming, android
Published: 2019-11-29 13:34:01

I will show how to create your own mobile presentation viewer with **[Aspose.Slides API for Android](https://products.aspose.com/slides/android-java?utm_source=https://dev.to/grazhevskaja/create-presentation-viewer-for-android-3ngj-temp-slug-6238644&utm_medium=email)**.

This is the **first article in a series**, where I will pass you through some trivial and non-trivial scenarious, like: edit and convert presentation from mobile; upload presentation as a web application or add it to [Aspose Cloud Storage](https://dashboard.aspose.cloud/?utm_source=https://dev.to/grazhevskaja/create-presentation-viewer-for-android-3ngj-temp-slug-6238644&utm_medium=email); share presentation in various formats; generate unique presenation key; and many others. 

Please stay tuned and share which other cases you are interested to see.👇

##Presentation viewer for Android:
So, let us start with the first step - basic presentation viewer!👩‍💻

I will pass you through the key points, while you can find the whole project on **[GitHub](https://github.com/agrazhevskaja/aspose-slides-use-cases/tree/master/slides-android-demo-app-develop?utm_source=https://dev.to/grazhevskaja/create-presentation-viewer-for-android-3ngj-temp-slug-6238644&utm_medium=email)**.

1. Open [Android Studio](https://developer.android.com/guide).

2. **[Install](https://downloads.aspose.com/slides/androidjava?utm_source=https://dev.to/grazhevskaja/create-presentation-viewer-for-android-3ngj-temp-slug-6238644&utm_medium=email)** latest Aspose.Slides API for Android.

3. Define **[Main Layout](https://github.com/agrazhevskaja/aspose-slides-use-cases/blob/master/slides-android-demo-app-develop/src/main/java/com/aspose/slides/demoapp/MainActivity.java?utm_source=https://dev.to/aspose_slides/create-presentation-viewer-for-your-android-use-case-3c7a&utm_medium=email)** with **btnOpenPresentation** button, or just take it from link.
My lookes simply this way:
    
    <img width="200" src="https://i.imgur.com/XLKuA4B.png">

4. Create **[Main Activity](https://github.com/agrazhevskaja/aspose-slides-use-cases/blob/master/slides-android-demo-app-develop/src/main/res/layout/activity_main.xml?utm_source=https://dev.to/aspose_slides/create-presentation-viewer-for-your-android-use-case-3c7a&utm_medium=email)** and add this code to **onCreate** method, which will launch Presentation Activity by btnOpenPresentation click:
    ```java
public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
           // ...

           // Start Presentation activity on Open button click
           final Button btnOpenPresentation = findViewById(R.id.btnOpenPresentation);
              btnOpenPresentation.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                 execPresentationActivity();
           }});
        }
    
        // Start Presentation activity
        private void execPresentationActivity() {
           Intent openPresentationIntent = new Intent(MainActivity.this, 
           PresentationActivity.class);
           startActivity(openPresentationIntent);
        }
}

    ```

5. Create **[Presentation Layout](https://github.com/agrazhevskaja/aspose-slides-use-cases/blob/master/slides-android-demo-app-develop/src/main/res/layout/activity_presentation.xml)** with slides’ carousele and a control to show the current slide, or just take it from link.
My looks simply this way, but there is a lot of space to show creativity here👩‍🎨.
Actually, this is a final application we should get:
    
    <img width="200" src="https://i.imgur.com/hpGWN8f.png">

6. Create **[Presentation Activity](https://github.com/agrazhevskaja/aspose-slides-use-cases/blob/master/slides-android-demo-app-develop/src/main/java/com/aspose/slides/demoapp/PresentationActivity.java?utm_source=https://dev.to/grazhevskaja/create-presentation-viewer-for-android-3ngj-temp-slug-6238644&utm_medium=email)**.
Now, let us stop more detailed.

    1. Define **execFilePicker()** method and call it from **onCreate()** in Presentation Activity. This method will open the file system, that we could choose the presentation file this way:
        
        <img width="200" src="https://i.imgur.com/DGDeQ5k.png">
        
        All this is done just in 3 lines:
        ```java
public class PresentationActivity extends AppCompatActivity {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                execFilePicker();
            }

            // Open File system to choose presentation file
            private void execFilePicker() {
                Intent mRequestFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
                mRequestFileIntent.setType();
                startActivityForResult(mRequestFileIntent, READ_REQUEST_CODE);
            }
 }
         ```

    2. Note that we’ve passed READ_REQUEST_CODE to startActivityForResult method. We will use it to check activity result in **onActivityResult** method and know if we can load the presentation chosen:

        ```java
     public class PresentationActivity extends AppCompatActivity {

            @Override
            public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
                if (requestCode == READ_REQUEST_CODE && resultCode == RESULT_OK && resultData != null) {
                    Uri uri = resultData.getData();

                    // Load chosen presentation
                    execLoadPresentation(uri);
            }}
}
        ```

    3. In previous method we’ve called **execLoadPresentation** method to load chosen presentation.
    Then **selectSlide** method can be called to select the slide of the presentation, which should be shown on Presentation Layout.

        ```java
        public class PresentationActivity extends AppCompatActivity {

          private SlidesApp app;

          // Invoke LoadPresentationTask to load chosen presentation
          private void execLoadPresentation(Uri uri) throws IOException {
              InputStream input = getContentResolver().openInputStream(uri);
              new LoadPresentationTask(this).execute(input);
          }

          // Invoke SelectSlideTask to select a slide to be shown
          private void selectSlide(int pos) {
              new SelectSlideTask(this).execute(pos);
          }
       }
        ```

7. Now lets create **LoadPresentationTask** and **SelectSlideTask** classes used in two previous methods.

    **LoadPresentationTask** will do the following:
       * **doInBackground** - set the presenation, choosed by user, as the current one.

    ```java
private static class LoadPresentationTask extends AsyncTask<InputStream, Void, Presentation>
{
        // Set chosen presentation as current one
        @Override
        protected Presentation doInBackground(InputStream... inputs) {
            InputStream input = inputs[0];
            Presentation p = input == null ? new Presentation() : new Presentation(input);

            // Call setPresentation by SlidesApp to set chosen presentation as current one
            presentationActivity.app.setPresentation(p, presentationActivity.currSlideViewSize, presentationActivity.slidesListViewSize);
            return p;
        }
}
    ```

    **SelectSlideTask** will do the following:
       * **doInBackground** - select the slide of presentation to be shown on Presentation Layout.

    ```java
private static class SelectSlideTask extends AsyncTask<Integer, Void, Bitmap> 
{
        // Select the slide, that should be shown
        @Override
        protected Bitmap doInBackground(Integer... ints) {
            int pos = ints[0];
            return presentationActivity.app.selectSlide(pos);
        }
}
    ```

    These tasks refer to presentationActivity.app.**setPresentation()** and presentationActivity.app.**selectSlide()**, where presentationActivity.app - is the instance of **SlidesApp** class, which manages all logic about Presentation and Slide objects.
        
8. In the end, we came to the main part of the story.👩‍🏫
    **❗️THE MAGIC STARTS HERE!❗️**

    **[SlidesApp](https://github.com/agrazhevskaja/aspose-slides-use-cases/blob/master/slides-android-demo-app-develop/src/main/java/com/aspose/slides/demoapp/SlidesApp.java?utm_source=https://dev.to/grazhevskaja/create-presentation-viewer-for-android-3ngj-temp-slug-6238644&utm_medium=email)** class incapsulates all the logic to work with **[Aspose.Slides API for Android](https://products.aspose.com/slides/android-java?utm_source=https://dev.to/grazhevskaja/create-presentation-viewer-for-android-3ngj-temp-slug-6238644&utm_medium=email)**.
This class will allow us to locally save presentation, get all slides, set current slide, convert it into the thumbnail, calculate thumbnail’s size, etc.

    1. Import **[IPresentation](https://apireference.aspose.com/cpp/slides/class/aspose.slides.i_presentation/?utm_source=https://dev.to/grazhevskaja/create-presentation-viewer-for-android-3ngj-temp-slug-6238644&utm_medium=email)**, **[ISlide](https://apireference.aspose.com/cpp/slides/class/aspose.slides.i_slide/?utm_source=https://dev.to/grazhevskaja/create-presentation-viewer-for-android-3ngj-temp-slug-6238644&utm_medium=email)**, **[ISlideCollection](https://apireference.aspose.com/cpp/slides/class/aspose.slides.i_slide_collection/?utm_source=https://dev.to/grazhevskaja/create-presentation-viewer-for-android-3ngj-temp-slug-6238644&utm_medium=email)** types:
        ```java
        import com.aspose.slides.IPresentation;
        import com.aspose.slides.ISlide;
        import com.aspose.slides.ISlideCollection;
        ```

    2. Add **SlidesApp** class with the instances of current presentation and current slide:
        ```java
        public class SlidesApp extends Application {
            private IPresentation presentation;
            private Bitmap currSlide;
            //...
}
        ```

    3. Now lets implement **selectSlide** method, that will call getSlides to retrieve all presentation slides, then get the slide needed by its id. And, get the thumbnail of the slide with **getThumbnail** method.
        ```java
        public class SlidesApp extends Application {
            public Bitmap selectSlide(int idx) {
                //...
                this.currSlide = this.presentation.getSlides().get_Item(idx).getThumbnail(this.slideThumbnailSize);
                return this.currSlide;
            }
        }
        ```

    4. The thumbnail is saved into currSlide property and returned back into SelectSlideTask, which will draw it on Presentation Layout:
        
        ```java
        public class PresentationActivity extends AppCompatActivity {
            private ImageView currSlide;
            //...
        }
        
        private static class SelectSlideTask extends AsyncTask<Integer, Void, Bitmap> 
        {
            private final PresentationActivity presentationActivity;

            @Override
            protected void onPostExecute(Bitmap result) {
                // Set thumbnail to currSlide image view, that it could be drawn on Presentation Layout
                presentationActivity.currSlide.setImageBitmap(result);
            }
        }
        ```
<br/>
<hr/>

I have shown you how to developer a basic presentation viewer, while there are a lot of features you can easily add to this application with **[Aspose.Slides API for Android](https://products.aspose.com/slides/android-java?utm_source=https://dev.to/grazhevskaja/create-presentation-viewer-for-android-3ngj-temp-slug-6238644&utm_medium=email)** to edit, convert or manage presentation or a certain slide. Check it out [here](https://docs.aspose.com/display/slidesjava/developer+guide?utm_source=https://dev.to/grazhevskaja/create-presentation-viewer-for-android-3ngj-temp-slug-6238644&utm_medium=email).👈