Initial Movies UI
Create data
Now we create some fake data to display Movies in the app. After we talk about databases, we'll convert the data into something real (and add Actors and Ratings).
Create a Movie
class. For now we just hold a title and description, but we'll add more when we
create our database.
show in full file app/src/main/java/com/androidbyexample/compose/movies/Movie.kt
package com.androidbyexample.compose.movieui1
data class Movie(
val title: String,
val description: String,
)
Next, we create a MovieViewModel
to hold a list of movies.
View models prepare and provide data for our user interface to consume. Here we hardcode
the data, but we'll change that in the database module.
show in full file app/src/main/java/com/androidbyexample/compose/movies/MovieViewModel.kt
// ...
import androidx.lifecycle.ViewModel
class MovieViewModel: ViewModel() {
val movies: List<Movie> = listOf(
Movie("The Transporter", "Jason Statham kicks a guy in the face"),
Movie("Transporter 2", "Jason Statham kicks a bunch of guys in the face"),
Movie("Hobbs and Shaw", "Cars, Explosions and Stuff"),
Movie("Jumanji - Welcome to the Jungle", "The Rock smolders"),
)
}
Finally, we connect the view model to the MainActivity
.
Using the viewModels
function creates a property delegate that will create an instance
of the specified view model (if it doesn't exist), or fetch an existing one for the
activity. This allows us to keep data across configuration changes, when the activity is
destroyed and recreated.
When adding the viewModels
function, it'll show in red. Place your cursor on the word
viewModels
and press Alt+Enter to see your options. It'll show you an option to import
the viewModels
extension function. Selecting it will add an import
at the top of the file
and the error will go away.
show in full file app/src/main/java/com/androidbyexample/compose/movies/MainActivity.kt
// ...
class MainActivity : ComponentActivity() {
private val viewModel by viewModels<MovieViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
// ...
}
// ...
All code changes
CHANGED: app/src/main/java/com/androidbyexample/compose/movies/MainActivity.kt
package com.androidbyexample.compose.movies
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.androidbyexample.compose.movies.ui.theme.MoviesTheme
import com.androidbyexample.compose.movieui1.MovieViewModel
class MainActivity : ComponentActivity() {
private val viewModel by viewModels<MovieViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
MoviesTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Greeting(
name = "Android",
modifier = Modifier.padding(innerPadding)
)
}
}
}
}
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier
)
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
MoviesTheme {
Greeting("Android")
}
}
ADDED: app/src/main/java/com/androidbyexample/compose/movies/Movie.kt
package com.androidbyexample.compose.movieui1
data class Movie(
val title: String,
val description: String,
)
ADDED: app/src/main/java/com/androidbyexample/compose/movies/MovieViewModel.kt
package com.androidbyexample.compose.movieui1
import androidx.lifecycle.ViewModel
class MovieViewModel: ViewModel() {
val movies: List<Movie> = listOf(
Movie("The Transporter", "Jason Statham kicks a guy in the face"),
Movie("Transporter 2", "Jason Statham kicks a bunch of guys in the face"),
Movie("Hobbs and Shaw", "Cars, Explosions and Stuff"),
Movie("Jumanji - Welcome to the Jungle", "The Rock smolders"),
)
}