Skip to content

Instantly share code, notes, and snippets.

@sagar-viradiya
Last active August 17, 2021 17:55
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save sagar-viradiya/64b6a8331b3f4d436ae499b9700ac1f4 to your computer and use it in GitHub Desktop.
Save sagar-viradiya/64b6a8331b3f4d436ae499b9700ac1f4 to your computer and use it in GitHub Desktop.
DevFest India 2020 Day 1 - Main Acitvity UI
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/backgroundColor"
tools:context=".MainActivity">
<androidx.core.widget.ContentLoadingProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
style="?android:attr/progressBarStyle" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:orientation="vertical"
android:visibility="gone"
tools:visibility="visible"
tools:listitem="@layout/movie_item_layout" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/errorView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:visibility="gone"
tools:visibility="visible"
android:fontFamily="@font/roboto"
android:text="@string/error_msg"/>
</FrameLayout>
@GauravChoudhary8411
Copy link

@RB-93 I did the exact same thing. Please send the code for MainActivity

@RB-93
Copy link

RB-93 commented Oct 18, 2020

This is my code for the MainActivity.kt file.

class MainActivity : AppCompatActivity() {

    private lateinit var viewModel: MainViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setupViewModel()
        observeViewModel()
    }

    private fun setupViewModel() {
        showProgress()

        viewModel = ViewModelProvider(
            this, MainViewModelFactory(
                NetworkHelper(this),
                MovieRepositoryImpl(
                    MovieDatabase.getInstance(this).movieDao(),
                    RetrofitBuilder.buildService()
                )
            )
        )[MainViewModel::class.java]
        viewModel.onCreate()
    }

    private fun observeViewModel() {
        viewModel.movieResponse.observe(this, Observer {
            showMovies(it.results)
            hideProgress()
        })

        viewModel.errorResponse.observe(this, Observer {
            showErrorMessage(it)
            hideProgress()
        })
    }

    private fun showMovies(movies: List<Movie>) {
        recyclerView.visibility = View.VISIBLE
        recyclerView.setHasFixedSize(true)
        recyclerView.itemAnimator = DefaultItemAnimator()
        recyclerView.adapter = MoviesAdapter(movies)
    }

    private fun showErrorMessage(errorMessage: String?) {
        errorView.visibility = View.VISIBLE
        errorView.text = errorMessage
    }

    private fun hideProgress() {
        progressBar.visibility = View.GONE
    }

    private fun showProgress() {
        progressBar.visibility = View.VISIBLE
    }
}

Let us know if you find any issues.

@soni1468
Copy link

image

The image is not getting shown in the card even though it's coming fine in preview.

image

below is the movie_item_layout.xml for reference:

<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardCornerRadius="8dp"
app:cardElevation="4dp"
app:contentPaddingBottom="16dp"
xmlns:tools="http://schemas.android.com/tools">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/moviePoster"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:scaleType="centerCrop"
        app:layout_constraintDimensionRatio="h,16:9"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:src="@tools:sample/avatars" />

    <TextView
        android:id="@+id/movieTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:textAppearance="@style/TextAppearance.MaterialComponents.Headline6"
        app:layout_constraintBottom_toTopOf="@+id/releaseDate"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/moviePoster"
        tools:text="Movie Title" />

    <TextView
        android:id="@+id/releaseDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:textAppearance="@style/TextAppearance.MaterialComponents.Subtitle2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/movieTitle"
        tools:text="07th July 1996" />

    <TextView
        android:id="@+id/avgVoting"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:drawableStart="@drawable/ic_baseline_thumb_up_24"
        android:drawablePadding="8dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        app:layout_constraintBottom_toTopOf="@+id/totalVotes"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/moviePoster"
        tools:text="3.4" />

    <TextView
        android:id="@+id/totalVotes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="3dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/avgVoting"
        tools:text="1468" />
</androidx.constraintlayout.widget.ConstraintLayout>

</com.google.android.material.card.MaterialCardView>

@sagar-viradiya
Copy link
Author

@soni1468 As I already mentioned in the session, tools namespace is only for preview purposes. You have to either use android namspace and use image stored locally in your app or load it from a remote sever. Niharika and Shreyank did it in the 2nd session.

@sagar-viradiya
Copy link
Author

@RB-93 Thanks for constant support in answering queries.

@RB-93
Copy link

RB-93 commented Oct 18, 2020

@sagar-viradiya

Thanks to you and the team, made possible these talks, explained and covered all the aspects to create wonderful app.
Learnt a lot! 😀
And for support, always ready for it any time. 🙂

@soni1468
Copy link

I am also facing the same exact issue.
Not able to get the image using static links even though internet permission is added.

@ArkaprabhaChakraborty
Copy link

I am also facing the same exact issue.
Not able to get the image using static links even though internet permission is added.

Well I fixed it ...... It was a simple error in logic from my end .... I typed wrap_content where it should be match_parent

@GauravChoudhary8411
Copy link

Could you provide the gist of the whole app?

@RB-93
Copy link

RB-93 commented Oct 26, 2020

Hi,
It's available on DevFest India resources link page under Mobile track.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment