How to use ViewFlipper Android using Kotlin?

Kotlin Apps/ApplicationsMobile Development

This example demonstrates how to use ViewFlipper Android using Kotlin.

Step 1 − Create a new project in Android Studio, go to File ? New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

Example

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:padding="4dp"
   tools:context=".MainActivity">
   <Button
      android:id="@+id/playPause"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_gravity="center_horizontal"
      android:layout_marginTop="15dp"
      android:text="Pause" />
   <ViewFlipper
      android:id="@+id/viewFlipper"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_below="@id/playPause"
      android:layout_centerInParent="true"
      android:autoStart="true"
      android:flipInterval="2000">
   </ViewFlipper>
</RelativeLayout>

Step 3 − Add the following code to src/MainActivity.kt

Example

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.widget.*
class MainActivity : AppCompatActivity() {
   private var imageList = intArrayOf(
   R.drawable.image1,
   R.drawable.image2,
   R.drawable.image3,
   R.drawable.image4,
   R.drawable.image5
   )
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      title = "KotlinApp"
      val viewFlipper = findViewById<ViewFlipper>(R.id.viewFlipper)
      if (viewFlipper != null) {
         viewFlipper.setInAnimation(applicationContext, android.R.anim.slide_in_left)
         viewFlipper.setOutAnimation(applicationContext, android.R.anim.slide_out_right)
      }
      if (viewFlipper != null) {
         for (image in imageList) {
            val imageView = ImageView(this)
            val layoutParams = FrameLayout.LayoutParams(
               ViewGroup.LayoutParams.MATCH_PARENT,
               ViewGroup.LayoutParams.WRAP_CONTENT
            )
            layoutParams.setMargins(30, 30, 30, 30)
            layoutParams.gravity = Gravity.CENTER
            imageView.layoutParams = layoutParams
            imageView.setImageResource(image)
            viewFlipper.addView(imageView)
         }
      }
      val btnPlayPause = findViewById<Button>(R.id.playPause)
      if (btnPlayPause != null && viewFlipper != null) {
         btnPlayPause.setOnClickListener {
            if (viewFlipper.isFlipping)
               viewFlipper.stopFlipping()
            else
               viewFlipper.startFlipping()
            if (viewFlipper.isFlipping) {
               btnPlayPause.text = "Pause"
               Toast.makeText(this@MainActivity, "Started Flipping",
               Toast.LENGTH_SHORT).show()
            } else {
               btnPlayPause.text = "Play"
               Toast.makeText(this@MainActivity, "Stopped Flipping",
               Toast.LENGTH_SHORT).show()
            }
         }
      }
   }
}

Step 4 − Add the following code to androidManifest.xml

Example

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.q15">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click the Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen

raja
Published on 23-May-2020 15:48:32
Advertisements