Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- XML
- <?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:background="@color/colorPrimaryDark"
- tools:context="com.example.mrmohammad.movies4u.DetailActivity"
- >
- <LinearLayout
- android:id="@+id/ad"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:paddingBottom="10dp">
- <ScrollView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- tools:showIn="@layout/activity_detail">
- <android.support.constraint.ConstraintLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- >
- <TextView
- android:id="@+id/tv_title_movie"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="16dp"
- android:layout_marginStart="16dp"
- android:layout_marginTop="@dimen/tv_title_movie_margin_top_content_detail"
- android:background="@drawable/bg_rating"
- android:padding="@dimen/tv_title_movie_padding_content_detail"
- android:text="Harry Potter"
- android:textColor="#fff"
- android:textSize="21sp"
- android:textStyle="bold"
- app:layout_constraintLeft_creator="1"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintTop_creator="1"
- app:layout_constraintTop_toTopOf="parent" />
- <TextView
- android:id="@+id/tv_release_year"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="16dp"
- android:layout_marginStart="16dp"
- android:layout_marginTop="@dimen/tv_release_year_content_detail_rating"
- android:background="@drawable/bg_rating"
- android:padding="@dimen/tv_title_movie_padding_content_detail"
- android:text="2018"
- android:textColor="#fff"
- android:textSize="16sp"
- app:layout_constraintLeft_creator="1"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintTop_creator="1"
- app:layout_constraintTop_toBottomOf="@id/tv_title_movie" />
- <TextView
- android:id="@+id/tv_rating"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="16dp"
- android:layout_marginStart="16dp"
- android:layout_marginTop="@dimen/tv_release_year_content_detail_rating"
- android:background="@drawable/bg_rating"
- android:drawableLeft="@drawable/ic_star_black_24dp"
- android:padding="@dimen/tv_title_movie_padding_content_detail"
- android:paddingRight="5dp"
- android:text="8.9"
- android:textColor="#fff"
- android:textSize="16sp"
- android:textStyle="bold"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintTop_toBottomOf="@id/tv_release_year" />
- <RelativeLayout
- android:id="@+id/llsynopsisbtn"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="16dp"
- android:layout_marginStart="16dp"
- android:layout_marginTop="@dimen/tv_release_year_content_detail_rating"
- android:orientation="horizontal"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintTop_toBottomOf="@id/tv_rating">
- <TextView
- android:id="@+id/tv_synopsis_label"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/bg_rating"
- android:padding="@dimen/tv_title_movie_padding_content_detail"
- android:text="@string/synopsis"
- android:textColor="#fff"
- android:textSize="18sp" />
- <View
- android:id="@+id/emptyView"
- android:layout_width="0dp"
- android:layout_height="0dp"
- android:layout_toRightOf="@id/tv_synopsis_label"
- android:layout_weight="1" />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="40dp"
- android:gravity="end"
- android:orientation="horizontal">
- <ImageView
- android:id="@+id/share"
- android:layout_width="35dp"
- android:layout_height="35dp"
- android:contentDescription="Share..."
- android:padding="4dp"
- android:src="@drawable/ic_share_black_24dp" />
- <ImageView
- android:id="@+id/saveFav"
- android:layout_width="35dp"
- android:layout_height="35dp"
- android:layout_below="@id/share"
- android:contentDescription="Save in Favorites"
- android:padding="4dp"
- android:src="@drawable/thumbsup" />
- <ImageView
- android:id="@+id/deleteFav"
- android:layout_width="35dp"
- android:layout_height="35dp"
- android:layout_toRightOf="@id/saveFav"
- android:contentDescription="Delete from Favorites"
- android:padding="4dp"
- android:src="@drawable/delete" />
- </LinearLayout>
- </RelativeLayout>
- <TextView
- android:id="@+id/tv_synopsis"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="@dimen/synopsis_margin_left"
- android:layout_marginRight="@dimen/synopsis_margin_right"
- android:layout_marginTop="@dimen/synopsis_margin_top"
- android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitationlaboris nisi ut aliquip ex ea commodo consequat."
- android:textColor="#fff"
- android:textSize="15sp"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintTop_toBottomOf="@id/llsynopsisbtn" />
- <TextView
- android:id="@+id/thumbnailURL"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:visibility="invisible" />
- <LinearLayout
- android:id="@+id/linLayContentDetail"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="16dp"
- android:layout_marginStart="16dp"
- android:layout_marginTop="4dp"
- android:orientation="vertical"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintTop_toBottomOf="@id/tv_synopsis">
- <TextView
- android:id="@+id/trailer"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="8dp"
- android:background="@drawable/bg_rating"
- android:padding="8dp"
- android:text="Trailers"
- android:textColor="#fff"
- android:textSize="20sp"
- />
- <android.support.v7.widget.RecyclerView
- android:id="@+id/rvTrailers"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="3dp"
- android:clipToPadding="false"
- android:padding="12dp"
- android:paddingBottom="25dp"
- android:scrollbars="vertical"
- >
- </android.support.v7.widget.RecyclerView>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="16dp"
- android:layout_marginStart="16dp"
- android:layout_marginTop="4dp"
- android:orientation="vertical"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintTop_toBottomOf="@id/linLayContentDetail">
- <TextView
- android:id="@+id/review"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="8dp"
- android:background="@drawable/bg_rating"
- android:padding="8dp"
- android:text="Reviews"
- android:textColor="#fff"
- android:textSize="20sp"
- />
- <android.support.v7.widget.RecyclerView
- android:id="@+id/rvReview"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="3dp"
- android:clipToPadding="false"
- android:padding="12dp"
- android:paddingBottom="25dp"
- >
- <!--HERE IS TEH RECYCLERVIEW THAT DONT DISPLAY-->
- </android.support.v7.widget.RecyclerView>
- </LinearLayout>
- </android.support.constraint.ConstraintLayout>
- </ScrollView>
- </LinearLayout>
- </RelativeLayout>
- DetailActivity
- public class DetailActivity extends AppCompatActivity {
- TextView movieName, releaseDate, userRating, synopsis;
- int id;
- ImageView iv, share, saveFav, deletFav;
- private RecyclerView rvDetail, rvReview;
- private TrailerAdapter adapter;
- private ReviewAdapter reviewAdapter;
- private List<Trailer> trailerList;
- private List<Review> reviewList;
- public static final String BASE_URL =
- "https://image.tmdb.org/t/p/w500";
- public static final String TAG = DetailActivity.class.getSimpleName();
- public MovieDbHelper dbHelper;
- private Movie favoriteMovie;
- private AppCompatActivity activity = DetailActivity.this;
- String poster, overview, rating, movieTitle, release;
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_detail);
- Stetho.initializeWithDefaults(this);
- share = findViewById(R.id.share);
- saveFav = findViewById(R.id.saveFav);
- deletFav = findViewById(R.id.deleteFav);
- final int movie_id = getIntent().getExtras().getInt("id");
- if (checkFavoriteMovie(movie_id)) {
- //Is a favorite
- saveFav.setVisibility(View.GONE);
- saveFav.setEnabled(false);
- deletFav.setEnabled(true);
- deletFav.setVisibility(View.VISIBLE);
- } else {
- deletFav.setVisibility(View.GONE);
- deletFav.setEnabled(false);
- saveFav.setEnabled(true);
- saveFav.setVisibility(View.VISIBLE);
- }
- iv = findViewById(R.id.iv_detail);
- movieName = findViewById(R.id.tv_title_movie);
- releaseDate = findViewById(R.id.tv_release_year);
- userRating = findViewById(R.id.tv_rating);
- synopsis = findViewById(R.id.tv_synopsis);
- Intent intentFromAdapter = getIntent();
- if (intentFromAdapter.hasExtra("original_title")) {
- poster =
- intentFromAdapter.getExtras().getString("poster_path");
- movieTitle =
- intentFromAdapter.getExtras().getString("original_title");
- overview = intentFromAdapter.getExtras().getString("overview");
- rating =
- intentFromAdapter.getExtras().getString("vote_average");
- release =
- intentFromAdapter.getExtras().getString("release_date");
- Picasso.with(this).load(BASE_URL + poster).into(iv);
- movieName.setText(movieTitle);
- releaseDate.setText(release);
- userRating.setText(rating);
- synopsis.setText(overview);
- } else {
- Toast.makeText(this, "No Data...", Toast.LENGTH_SHORT).show();
- }
- SharedPreferences sharedPreferences =
- PreferenceManager.getDefaultSharedPreferences(this);
- saveFav.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (checkFavoriteMovie(movie_id)) {
- Toast.makeText(activity, "Already added!",
- Toast.LENGTH_SHORT).show();
- } else {
- SharedPreferences.Editor editor =
- getSharedPreferences("com.example.mrmohammad.movies4u.DetailActivity", MODE_PRIVATE).edit();
- editor.putBoolean("Favorite Added", true);
- editor.commit();
- addFavorites();
- Toast.makeText(DetailActivity.this, "Movie Added successfully...", Toast.LENGTH_SHORT).show();
- }
- //
- saveFav.setEnabled(false);
- deletFav.setEnabled(true);
- deletFav.setVisibility(View.VISIBLE);
- saveFav.setVisibility(View.GONE);
- //
- }
- });
- deletFav.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- SharedPreferences.Editor editor = getSharedPreferences("com.example.mrmohammad.movies4u.DetailActivity", MODE_PRIVATE).edit();
- editor.putBoolean("Favorite Removed", true);
- editor.commit();
- removeFromFavorites(movie_id);
- Toast.makeText(DetailActivity.this, "Movie Removed...", Toast.LENGTH_SHORT).show();
- //
- deletFav.setEnabled(false);
- saveFav.setEnabled(true);
- deletFav.setVisibility(View.GONE);
- saveFav.setVisibility(View.VISIBLE);
- //
- }
- });
- share.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String d = movieName.getText().toString().trim();
- String s = "http://www.youtube.com/watch?v=" + d;
- s = s.replaceAll(" ", "%20");
- String full = "CLICK LINK " + s;
- Intent shareIntent = ShareCompat.IntentBuilder.from(DetailActivity.this)
- .setType("text/plain")
- .setText(full)
- .getIntent();
- startActivity(shareIntent);
- }
- });
- initializeViews();
- }
- private void initializeViews() {
- /////// Trailers
- trailerList = new ArrayList<>();
- adapter = new TrailerAdapter(this, trailerList);
- RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
- rvDetail = findViewById(R.id.rvTrailers);
- rvDetail.setLayoutManager(layoutManager);
- rvDetail.setAdapter(adapter);
- adapter.notifyDataSetChanged();
- ////////// Reviews
- reviewList = new ArrayList<>();
- reviewAdapter = new ReviewAdapter(this, reviewList);
- RecyclerView.LayoutManager lm = new LinearLayoutManager(getApplicationContext());
- rvReview = findViewById(R.id.rvReview);
- rvReview.setLayoutManager(lm);
- rvReview.setAdapter(reviewAdapter);
- reviewAdapter.notifyDataSetChanged();
- loadJSONTrailers();
- loadJSONReview();
- }
- private void loadJSONTrailers() {
- int movie_id = getIntent().getExtras().getInt("id");
- try {
- if (BuildConfig.THE_MOVIE_DB_API_TOKEN.isEmpty()) {
- Toast.makeText(this, "Please obtain API KEY", Toast.LENGTH_SHORT).show();
- return;
- }
- Client Client = new Client();
- Service apiService = Client.getClient().create(Service.class);
- Call<TrailerResponse> call = apiService.getMovieTrailer(movie_id, BuildConfig.THE_MOVIE_DB_API_TOKEN);
- call.enqueue(new Callback<TrailerResponse>() {
- @Override
- public void onResponse(Call<TrailerResponse> call, Response<TrailerResponse> response) {
- List<Trailer> trailers = response.body().getResuts();
- rvDetail.setAdapter(new TrailerAdapter(getApplicationContext(), trailers));
- rvDetail.smoothScrollToPosition(0);
- }
- @Override
- public void onFailure(Call<TrailerResponse> call, Throwable t) {
- Log.d("Error", t.getMessage());
- Toast.makeText(DetailActivity.this, "Error fetching Trailer", Toast.LENGTH_SHORT).show();
- }
- });
- } catch (Exception e) {
- Log.d("Error", e.getMessage());
- Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
- }
- }
- private void loadJSONReview() {
- int movie_id = getIntent().getExtras().getInt("id");
- try {
- if (BuildConfig.THE_MOVIE_DB_API_TOKEN.isEmpty()) {
- Toast.makeText(this, "Please obtain API KEY", Toast.LENGTH_SHORT).show();
- return;
- }
- Client Client = new Client();
- Service apiService = Client.getClient().create(Service.class);
- Call<ReviewResponse> call = apiService.getMovieReviews(movie_id, BuildConfig.THE_MOVIE_DB_API_TOKEN);
- call.enqueue(new Callback<ReviewResponse>() {
- @Override
- public void onResponse(Call<ReviewResponse> call, Response<ReviewResponse> response) {
- List<Review> reviews = response.body().getResults();
- rvReview.setAdapter(new ReviewAdapter(getApplicationContext(), reviews));
- rvReview.smoothScrollToPosition(0);
- Log.d(TAG, "Here>>>>>>>>>>>>>>>>");
- }
- @Override
- public void onFailure(Call<ReviewResponse> call, Throwable t) {
- Log.d("Error", t.getMessage());
- Toast.makeText(DetailActivity.this, "Error fetching Review", Toast.LENGTH_SHORT).show();
- }
- });
- } catch (Exception e) {
- Log.d("Error", e.getMessage());
- Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
- }
- }
- public void addFavorites() {
- int movie_id = getIntent().getExtras().getInt("id");
- dbHelper = new MovieDbHelper(activity);
- favoriteMovie = new Movie();
- String rate = getIntent().getExtras().getString("vote_average");
- String poster = getIntent().getExtras().getString("poster_path");
- favoriteMovie.setId(movie_id);
- favoriteMovie.setOriginalTitle(movieName.getText().toString().trim());
- favoriteMovie.setPosterPath(poster);
- favoriteMovie.setVoteAverage(Double.parseDouble(rate));
- favoriteMovie.setOverview(synopsis.getText().toString().trim());
- favoriteMovie.setReleaseDate(releaseDate.getText().toString().trim());
- dbHelper.addFavorites(favoriteMovie);
- Log.d(TAG, "Added fav.....");
- }
- private void removeFromFavorites(int id) {
- getApplicationContext().getContentResolver().delete(MovieContract.MovieEntry.CONTENT_URI, MovieContract.MovieEntry.COLUMN_MOVIE_ID + " = ?",
- new String[]{String.valueOf(id)});
- }
- private boolean checkFavoriteMovie(int id) {
- Cursor cursor = activity.getContentResolver().query(MovieContract.MovieEntry.CONTENT_URI, null, MovieContract.MovieEntry.COLUMN_MOVIE_ID + " = " + id, null, null);
- assert cursor != null;
- if (cursor.getCount() <= 0) {
- cursor.close();
- return false;
- }
- cursor.close();
- return true;
- }
- }
- ReviewAdapter
- public class ReviewAdapter extends
- RecyclerView.Adapter<ReviewAdapter.MyViewHolder>{
- private Context mContext;
- private List<Review> reviewsList;
- public ReviewAdapter(Context mContext, List<Review> reviewsList){
- this.mContext = mContext;
- this.reviewsList = reviewsList;
- }
- @Override
- public ReviewAdapter.MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i){
- View view = LayoutInflater.from(viewGroup.getContext())
- .inflate(R.layout.review, viewGroup, false);
- return new MyViewHolder(view);
- }
- @Override
- public void onBindViewHolder(final ReviewAdapter.MyViewHolder holder, int i){
- holder.reviewAuthor.setText(reviewsList.get(i).getAuthor());
- holder.reviewContent.setText(reviewsList.get(i).getContent());
- }
- @Override
- public int getItemCount(){
- return reviewsList.size();
- }
- public class MyViewHolder extends RecyclerView.ViewHolder{
- TextView reviewAuthor;
- TextView reviewContent;
- public MyViewHolder(View view){
- super(view);
- reviewAuthor = view.findViewById(R.id.review_author);
- reviewContent = view.findViewById(R.id.review_content);
- view.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- int pos = getAdapterPosition();
- if(pos != RecyclerView.NO_POSITION){
- String author = reviewsList.get(pos).getAuthor();
- String content = reviewsList.get(pos).getContent();
- reviewAuthor.setText(author);
- reviewContent.setText(content);
- }
- }
- });
- }
- }
- }
Add Comment
Please, Sign In to add comment