Hi Guys, you have seemed professional android application which usually having image slider, So, in this, we will learn "How to make image slider by view flipper in the android studio(kotlin)".
activity_main.xml
MainActivity.kt
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <ViewFlipper android:id="@+id/v_flipper" android:layout_width="match_parent" android:layout_height="220dp" /> </LinearLayout> |
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class MainActivity : AppCompatActivity() { lateinit var viewflipper : ViewFlipper override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) viewflipper = findViewById(R.id.v_flipper) for(i in 0 until image.size) { flip_image(image[i]) } } fun flip_image(i : Int) { val view = ImageView(this) view.setBackgroundResource(i) viewflipper.addView(view) viewflipper.setFlipInterval(3000) viewflipper.setAutoStart(true) viewflipper.setInAnimation(this , android.R.anim.slide_in_left) viewflipper.setOutAnimation(this , android.R.anim.slide_out_right) } } |
Comments
Post a Comment