how to make shuffled flashcards on android studio with audio

Viewed 23

I am new to android studio. I have watched a few tutorials on youtube on how to make flashcards, and now that i have made one.

I want to have 100 flashcards with different words and its meaning, however i am clueless on how should i make the code efficient. If i am going to produce the same layout- only with different java classes due to the word content 100 times, or can i just have one class where it can store those 100 words and its meaning, and it will be retrieved (can firebase be used?) every time i open the flashcards?

it has also a corresponding audio along with the string i am storing, also, how do i shuffle the index of the content when i exit the flashcards and open it again?

this is how my flashcards look like

https://i.stack.imgur.com/bBPEw.png

xml layout

<ImageButton
    android:id="@+id/flashcardsBackBtn"
    android:layout_width="109dp"
    android:layout_height="87dp"
    android:background="@android:color/transparent"
    android:scaleType="fitCenter"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.052"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.093"
    app:srcCompat="@drawable/backbtn" />

<TextView
    android:id="@+id/card_front"
    android:layout_width="350dp"
    android:layout_height="250dp"
    android:background="@drawable/rounded_corner"
    android:fontFamily="@font/anonymousprobold"
    android:gravity="center"
    android:text="salita"
    android:textAlignment="center"
    android:textColor="#000000"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.491"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.428" />

<ImageButton
    android:id="@+id/soundBtn1"
    android:layout_width="40dp"
    android:layout_height="46dp"
    android:background="@android:color/transparent"
    android:scaleType="fitCenter"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.822"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.598"
    app:srcCompat="@drawable/sound" />

<TextView
    android:id="@+id/card_back"
    android:layout_width="350dp"
    android:layout_height="250dp"
    android:alpha="0"
    android:background="#E1E1E1"
    android:fontFamily="@font/anonymousprobold"
    android:gravity="center"
    android:text="word"
    android:textAlignment="center"
    android:textColor="#000000"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.491"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.428" />

<Button
    android:id="@+id/flipBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/button_background"
    android:padding="9dp"
    android:text="Flip"
    android:textColor="#F87A63"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/card_front"
    app:layout_constraintVertical_bias="0.141" />

<Button
    android:id="@+id/nextFC"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="#F87A63"
    android:fontFamily="@font/leaguespartanbold"
    android:text="Next"
    android:textAllCaps="false"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.901"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.833" />

java class (i am using kotlin)

import android.animation.AnimatorInflater
import android.animation.AnimatorSet
import android.content.Intent
import android.media.MediaPlayer
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ImageButton
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity


class Flashcards1 : AppCompatActivity() {
    //Create variable for animators (drawable)
    lateinit var front_anim: AnimatorSet
    lateinit var back_anim: AnimatorSet
    var isFront = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_flashcards1)

        //Textview from the XML layout
        val front = findViewById<TextView>(R.id.card_front)
        val back = findViewById<TextView>(R.id.card_back)
        val flip = findViewById<Button>(R.id.flipBtn)
        //Set display metrics
        val scale: Float = applicationContext.resources.displayMetrics.density
        front.cameraDistance = 8000 * scale
        back.cameraDistance = 8000 * scale
        //Front animation
        front_anim = AnimatorInflater.loadAnimator(
            applicationContext,
            R.animator.front_animator
        ) as AnimatorSet
        //Back animation
        back_anim = AnimatorInflater.loadAnimator(
            applicationContext,
            R.animator.back_animator
        ) as AnimatorSet
        //Button event listener
        flip.setOnClickListener {
            if (isFront) {
                front_anim.setTarget(front)
                back_anim.setTarget(back)
                front_anim.start()
                back_anim.start()
                isFront = false
            } else {
                front_anim.setTarget(back)
                back_anim.setTarget(front)
                back_anim.start()
                front_anim.start()
                isFront = true
            }
        }
        //For audio 1
        val imageButton1 = findViewById<ImageButton>(R.id.soundBtn1) //For Audio

        val mediaPlayer1: MediaPlayer = MediaPlayer.create(this, R.raw.salita) //For Audio

        imageButton1.setOnClickListener(View.OnClickListener { mediaPlayer1.start() } //For Audio
        )
        //Next button 1
        val next = findViewById<Button>(R.id.nextFC)
        next.setOnClickListener {
            val intent = Intent(this, Flashcards2::class.java)
            startActivity(intent)
        }
        //Back button 1
        val backBtn = findViewById<ImageButton>(R.id.flashcardsBackBtn)
        backBtn.setOnClickListener {
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
        }
    }
}
0 Answers
Related