Guest User

Untitled

a guest
Apr 25th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.58 KB | None | 0 0
  1. XML
  2.  
  3. <?xml version="1.0" encoding="utf-8"?>
  4. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. xmlns:tools="http://schemas.android.com/tools"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:background="@color/colorPrimaryDark"
  10. tools:context="com.example.mrmohammad.movies4u.DetailActivity"
  11.  
  12. >
  13.  
  14. <LinearLayout
  15. android:id="@+id/ad"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:paddingBottom="10dp">
  19.  
  20. <ScrollView
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. tools:showIn="@layout/activity_detail">
  24.  
  25. <android.support.constraint.ConstraintLayout
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28.  
  29. >
  30.  
  31.  
  32. <TextView
  33. android:id="@+id/tv_title_movie"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:layout_marginLeft="16dp"
  37. android:layout_marginStart="16dp"
  38. android:layout_marginTop="@dimen/tv_title_movie_margin_top_content_detail"
  39. android:background="@drawable/bg_rating"
  40. android:padding="@dimen/tv_title_movie_padding_content_detail"
  41. android:text="Harry Potter"
  42. android:textColor="#fff"
  43. android:textSize="21sp"
  44. android:textStyle="bold"
  45. app:layout_constraintLeft_creator="1"
  46. app:layout_constraintLeft_toLeftOf="parent"
  47. app:layout_constraintTop_creator="1"
  48. app:layout_constraintTop_toTopOf="parent" />
  49.  
  50. <TextView
  51. android:id="@+id/tv_release_year"
  52. android:layout_width="wrap_content"
  53. android:layout_height="wrap_content"
  54. android:layout_marginLeft="16dp"
  55. android:layout_marginStart="16dp"
  56. android:layout_marginTop="@dimen/tv_release_year_content_detail_rating"
  57. android:background="@drawable/bg_rating"
  58. android:padding="@dimen/tv_title_movie_padding_content_detail"
  59. android:text="2018"
  60. android:textColor="#fff"
  61. android:textSize="16sp"
  62. app:layout_constraintLeft_creator="1"
  63. app:layout_constraintLeft_toLeftOf="parent"
  64. app:layout_constraintTop_creator="1"
  65. app:layout_constraintTop_toBottomOf="@id/tv_title_movie" />
  66.  
  67.  
  68. <TextView
  69. android:id="@+id/tv_rating"
  70. android:layout_width="wrap_content"
  71. android:layout_height="wrap_content"
  72. android:layout_marginLeft="16dp"
  73. android:layout_marginStart="16dp"
  74. android:layout_marginTop="@dimen/tv_release_year_content_detail_rating"
  75. android:background="@drawable/bg_rating"
  76. android:drawableLeft="@drawable/ic_star_black_24dp"
  77. android:padding="@dimen/tv_title_movie_padding_content_detail"
  78. android:paddingRight="5dp"
  79. android:text="8.9"
  80. android:textColor="#fff"
  81. android:textSize="16sp"
  82. android:textStyle="bold"
  83. app:layout_constraintLeft_toLeftOf="parent"
  84. app:layout_constraintTop_toBottomOf="@id/tv_release_year" />
  85.  
  86.  
  87. <RelativeLayout
  88. android:id="@+id/llsynopsisbtn"
  89. android:layout_width="match_parent"
  90. android:layout_height="wrap_content"
  91. android:layout_marginLeft="16dp"
  92. android:layout_marginStart="16dp"
  93. android:layout_marginTop="@dimen/tv_release_year_content_detail_rating"
  94. android:orientation="horizontal"
  95. app:layout_constraintLeft_toLeftOf="parent"
  96. app:layout_constraintTop_toBottomOf="@id/tv_rating">
  97.  
  98.  
  99. <TextView
  100. android:id="@+id/tv_synopsis_label"
  101. android:layout_width="wrap_content"
  102. android:layout_height="wrap_content"
  103. android:background="@drawable/bg_rating"
  104. android:padding="@dimen/tv_title_movie_padding_content_detail"
  105.  
  106. android:text="@string/synopsis"
  107. android:textColor="#fff"
  108. android:textSize="18sp" />
  109.  
  110. <View
  111. android:id="@+id/emptyView"
  112. android:layout_width="0dp"
  113. android:layout_height="0dp"
  114. android:layout_toRightOf="@id/tv_synopsis_label"
  115. android:layout_weight="1" />
  116.  
  117.  
  118. <LinearLayout
  119. android:layout_width="match_parent"
  120. android:layout_height="40dp"
  121. android:gravity="end"
  122. android:orientation="horizontal">
  123.  
  124.  
  125. <ImageView
  126. android:id="@+id/share"
  127. android:layout_width="35dp"
  128. android:layout_height="35dp"
  129. android:contentDescription="Share..."
  130. android:padding="4dp"
  131. android:src="@drawable/ic_share_black_24dp" />
  132.  
  133. <ImageView
  134. android:id="@+id/saveFav"
  135. android:layout_width="35dp"
  136. android:layout_height="35dp"
  137. android:layout_below="@id/share"
  138. android:contentDescription="Save in Favorites"
  139. android:padding="4dp"
  140. android:src="@drawable/thumbsup" />
  141.  
  142. <ImageView
  143. android:id="@+id/deleteFav"
  144. android:layout_width="35dp"
  145. android:layout_height="35dp"
  146.  
  147. android:layout_toRightOf="@id/saveFav"
  148. android:contentDescription="Delete from Favorites"
  149. android:padding="4dp"
  150. android:src="@drawable/delete" />
  151.  
  152.  
  153. </LinearLayout>
  154.  
  155.  
  156. </RelativeLayout>
  157.  
  158. <TextView
  159. android:id="@+id/tv_synopsis"
  160. android:layout_width="match_parent"
  161. android:layout_height="wrap_content"
  162. android:layout_marginLeft="@dimen/synopsis_margin_left"
  163. android:layout_marginRight="@dimen/synopsis_margin_right"
  164. android:layout_marginTop="@dimen/synopsis_margin_top"
  165.  
  166. 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."
  167. android:textColor="#fff"
  168. android:textSize="15sp"
  169. app:layout_constraintLeft_toLeftOf="parent"
  170. app:layout_constraintTop_toBottomOf="@id/llsynopsisbtn" />
  171.  
  172. <TextView
  173. android:id="@+id/thumbnailURL"
  174. android:layout_width="match_parent"
  175. android:layout_height="wrap_content"
  176. android:visibility="invisible" />
  177.  
  178. <LinearLayout
  179. android:id="@+id/linLayContentDetail"
  180. android:layout_width="match_parent"
  181. android:layout_height="wrap_content"
  182. android:layout_marginLeft="16dp"
  183. android:layout_marginStart="16dp"
  184. android:layout_marginTop="4dp"
  185. android:orientation="vertical"
  186. app:layout_constraintLeft_toLeftOf="parent"
  187. app:layout_constraintTop_toBottomOf="@id/tv_synopsis">
  188.  
  189. <TextView
  190. android:id="@+id/trailer"
  191. android:layout_width="wrap_content"
  192. android:layout_height="wrap_content"
  193. android:layout_marginTop="8dp"
  194. android:background="@drawable/bg_rating"
  195. android:padding="8dp"
  196. android:text="Trailers"
  197. android:textColor="#fff"
  198. android:textSize="20sp"
  199.  
  200.  
  201. />
  202.  
  203. <android.support.v7.widget.RecyclerView
  204. android:id="@+id/rvTrailers"
  205. android:layout_width="match_parent"
  206. android:layout_height="wrap_content"
  207. android:layout_margin="3dp"
  208. android:clipToPadding="false"
  209. android:padding="12dp"
  210. android:paddingBottom="25dp"
  211. android:scrollbars="vertical"
  212.  
  213. >
  214.  
  215.  
  216. </android.support.v7.widget.RecyclerView>
  217.  
  218. </LinearLayout>
  219.  
  220.  
  221. <LinearLayout
  222. android:layout_width="match_parent"
  223. android:layout_height="wrap_content"
  224. android:layout_marginLeft="16dp"
  225. android:layout_marginStart="16dp"
  226. android:layout_marginTop="4dp"
  227. android:orientation="vertical"
  228. app:layout_constraintLeft_toLeftOf="parent"
  229. app:layout_constraintTop_toBottomOf="@id/linLayContentDetail">
  230.  
  231. <TextView
  232. android:id="@+id/review"
  233. android:layout_width="wrap_content"
  234. android:layout_height="wrap_content"
  235. android:layout_marginTop="8dp"
  236. android:background="@drawable/bg_rating"
  237. android:padding="8dp"
  238. android:text="Reviews"
  239. android:textColor="#fff"
  240. android:textSize="20sp"
  241.  
  242.  
  243. />
  244.  
  245. <android.support.v7.widget.RecyclerView
  246. android:id="@+id/rvReview"
  247. android:layout_width="match_parent"
  248. android:layout_height="wrap_content"
  249. android:layout_margin="3dp"
  250. android:clipToPadding="false"
  251. android:padding="12dp"
  252. android:paddingBottom="25dp"
  253.  
  254.  
  255. >
  256.  
  257. <!--HERE IS TEH RECYCLERVIEW THAT DONT DISPLAY-->
  258.  
  259.  
  260.  
  261.  
  262. </android.support.v7.widget.RecyclerView>
  263.  
  264. </LinearLayout>
  265.  
  266.  
  267. </android.support.constraint.ConstraintLayout>
  268.  
  269. </ScrollView>
  270.  
  271. </LinearLayout>
  272.  
  273. </RelativeLayout>
  274.  
  275. DetailActivity
  276.  
  277.  
  278. public class DetailActivity extends AppCompatActivity {
  279.  
  280. TextView movieName, releaseDate, userRating, synopsis;
  281. int id;
  282. ImageView iv, share, saveFav, deletFav;
  283. private RecyclerView rvDetail, rvReview;
  284. private TrailerAdapter adapter;
  285. private ReviewAdapter reviewAdapter;
  286. private List<Trailer> trailerList;
  287. private List<Review> reviewList;
  288. public static final String BASE_URL =
  289. "https://image.tmdb.org/t/p/w500";
  290.  
  291.  
  292. public static final String TAG = DetailActivity.class.getSimpleName();
  293. public MovieDbHelper dbHelper;
  294. private Movie favoriteMovie;
  295. private AppCompatActivity activity = DetailActivity.this;
  296. String poster, overview, rating, movieTitle, release;
  297.  
  298. @Override
  299. protected void onCreate(@Nullable Bundle savedInstanceState) {
  300. super.onCreate(savedInstanceState);
  301. setContentView(R.layout.activity_detail);
  302. Stetho.initializeWithDefaults(this);
  303.  
  304.  
  305. share = findViewById(R.id.share);
  306. saveFav = findViewById(R.id.saveFav);
  307. deletFav = findViewById(R.id.deleteFav);
  308.  
  309.  
  310. final int movie_id = getIntent().getExtras().getInt("id");
  311.  
  312. if (checkFavoriteMovie(movie_id)) {
  313.  
  314. //Is a favorite
  315. saveFav.setVisibility(View.GONE);
  316. saveFav.setEnabled(false);
  317.  
  318. deletFav.setEnabled(true);
  319. deletFav.setVisibility(View.VISIBLE);
  320.  
  321.  
  322. } else {
  323. deletFav.setVisibility(View.GONE);
  324. deletFav.setEnabled(false);
  325. saveFav.setEnabled(true);
  326. saveFav.setVisibility(View.VISIBLE);
  327. }
  328.  
  329.  
  330. iv = findViewById(R.id.iv_detail);
  331. movieName = findViewById(R.id.tv_title_movie);
  332. releaseDate = findViewById(R.id.tv_release_year);
  333. userRating = findViewById(R.id.tv_rating);
  334. synopsis = findViewById(R.id.tv_synopsis);
  335.  
  336. Intent intentFromAdapter = getIntent();
  337. if (intentFromAdapter.hasExtra("original_title")) {
  338. poster =
  339. intentFromAdapter.getExtras().getString("poster_path");
  340. movieTitle =
  341. intentFromAdapter.getExtras().getString("original_title");
  342. overview = intentFromAdapter.getExtras().getString("overview");
  343. rating =
  344. intentFromAdapter.getExtras().getString("vote_average");
  345. release =
  346. intentFromAdapter.getExtras().getString("release_date");
  347.  
  348. Picasso.with(this).load(BASE_URL + poster).into(iv);
  349. movieName.setText(movieTitle);
  350. releaseDate.setText(release);
  351. userRating.setText(rating);
  352. synopsis.setText(overview);
  353.  
  354. } else {
  355. Toast.makeText(this, "No Data...", Toast.LENGTH_SHORT).show();
  356. }
  357.  
  358. SharedPreferences sharedPreferences =
  359. PreferenceManager.getDefaultSharedPreferences(this);
  360.  
  361.  
  362. saveFav.setOnClickListener(new View.OnClickListener() {
  363. @Override
  364. public void onClick(View v) {
  365.  
  366.  
  367. if (checkFavoriteMovie(movie_id)) {
  368.  
  369.  
  370. Toast.makeText(activity, "Already added!",
  371. Toast.LENGTH_SHORT).show();
  372.  
  373. } else {
  374.  
  375.  
  376. SharedPreferences.Editor editor =
  377.  
  378. getSharedPreferences("com.example.mrmohammad.movies4u.DetailActivity", MODE_PRIVATE).edit();
  379. editor.putBoolean("Favorite Added", true);
  380. editor.commit();
  381. addFavorites();
  382. Toast.makeText(DetailActivity.this, "Movie Added successfully...", Toast.LENGTH_SHORT).show();
  383.  
  384. }
  385. //
  386.  
  387. saveFav.setEnabled(false);
  388. deletFav.setEnabled(true);
  389. deletFav.setVisibility(View.VISIBLE);
  390. saveFav.setVisibility(View.GONE);
  391. //
  392.  
  393.  
  394. }
  395. });
  396.  
  397. deletFav.setOnClickListener(new View.OnClickListener() {
  398. @Override
  399. public void onClick(View v) {
  400.  
  401.  
  402. SharedPreferences.Editor editor = getSharedPreferences("com.example.mrmohammad.movies4u.DetailActivity", MODE_PRIVATE).edit();
  403. editor.putBoolean("Favorite Removed", true);
  404. editor.commit();
  405.  
  406. removeFromFavorites(movie_id);
  407. Toast.makeText(DetailActivity.this, "Movie Removed...", Toast.LENGTH_SHORT).show();
  408.  
  409.  
  410. //
  411.  
  412.  
  413. deletFav.setEnabled(false);
  414. saveFav.setEnabled(true);
  415. deletFav.setVisibility(View.GONE);
  416. saveFav.setVisibility(View.VISIBLE);
  417.  
  418.  
  419. //
  420. }
  421.  
  422. });
  423.  
  424. share.setOnClickListener(new View.OnClickListener() {
  425. @Override
  426. public void onClick(View v) {
  427. String d = movieName.getText().toString().trim();
  428. String s = "http://www.youtube.com/watch?v=" + d;
  429. s = s.replaceAll(" ", "%20");
  430. String full = "CLICK LINK " + s;
  431.  
  432. Intent shareIntent = ShareCompat.IntentBuilder.from(DetailActivity.this)
  433. .setType("text/plain")
  434. .setText(full)
  435. .getIntent();
  436. startActivity(shareIntent);
  437. }
  438. });
  439.  
  440.  
  441. initializeViews();
  442.  
  443.  
  444. }
  445.  
  446.  
  447. private void initializeViews() {
  448.  
  449. /////// Trailers
  450.  
  451. trailerList = new ArrayList<>();
  452. adapter = new TrailerAdapter(this, trailerList);
  453. RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
  454. rvDetail = findViewById(R.id.rvTrailers);
  455. rvDetail.setLayoutManager(layoutManager);
  456. rvDetail.setAdapter(adapter);
  457. adapter.notifyDataSetChanged();
  458.  
  459.  
  460. ////////// Reviews
  461.  
  462. reviewList = new ArrayList<>();
  463. reviewAdapter = new ReviewAdapter(this, reviewList);
  464. RecyclerView.LayoutManager lm = new LinearLayoutManager(getApplicationContext());
  465. rvReview = findViewById(R.id.rvReview);
  466. rvReview.setLayoutManager(lm);
  467. rvReview.setAdapter(reviewAdapter);
  468. reviewAdapter.notifyDataSetChanged();
  469.  
  470.  
  471. loadJSONTrailers();
  472. loadJSONReview();
  473.  
  474.  
  475. }
  476.  
  477.  
  478. private void loadJSONTrailers() {
  479. int movie_id = getIntent().getExtras().getInt("id");
  480. try {
  481. if (BuildConfig.THE_MOVIE_DB_API_TOKEN.isEmpty()) {
  482. Toast.makeText(this, "Please obtain API KEY", Toast.LENGTH_SHORT).show();
  483. return;
  484. }
  485. Client Client = new Client();
  486. Service apiService = Client.getClient().create(Service.class);
  487. Call<TrailerResponse> call = apiService.getMovieTrailer(movie_id, BuildConfig.THE_MOVIE_DB_API_TOKEN);
  488. call.enqueue(new Callback<TrailerResponse>() {
  489. @Override
  490. public void onResponse(Call<TrailerResponse> call, Response<TrailerResponse> response) {
  491. List<Trailer> trailers = response.body().getResuts();
  492. rvDetail.setAdapter(new TrailerAdapter(getApplicationContext(), trailers));
  493. rvDetail.smoothScrollToPosition(0);
  494.  
  495. }
  496.  
  497. @Override
  498. public void onFailure(Call<TrailerResponse> call, Throwable t) {
  499. Log.d("Error", t.getMessage());
  500. Toast.makeText(DetailActivity.this, "Error fetching Trailer", Toast.LENGTH_SHORT).show();
  501. }
  502. });
  503. } catch (Exception e) {
  504. Log.d("Error", e.getMessage());
  505. Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
  506. }
  507. }
  508.  
  509. private void loadJSONReview() {
  510. int movie_id = getIntent().getExtras().getInt("id");
  511.  
  512. try {
  513. if (BuildConfig.THE_MOVIE_DB_API_TOKEN.isEmpty()) {
  514. Toast.makeText(this, "Please obtain API KEY", Toast.LENGTH_SHORT).show();
  515. return;
  516.  
  517. }
  518. Client Client = new Client();
  519. Service apiService = Client.getClient().create(Service.class);
  520. Call<ReviewResponse> call = apiService.getMovieReviews(movie_id, BuildConfig.THE_MOVIE_DB_API_TOKEN);
  521. call.enqueue(new Callback<ReviewResponse>() {
  522. @Override
  523. public void onResponse(Call<ReviewResponse> call, Response<ReviewResponse> response) {
  524. List<Review> reviews = response.body().getResults();
  525. rvReview.setAdapter(new ReviewAdapter(getApplicationContext(), reviews));
  526. rvReview.smoothScrollToPosition(0);
  527. Log.d(TAG, "Here>>>>>>>>>>>>>>>>");
  528.  
  529. }
  530.  
  531. @Override
  532. public void onFailure(Call<ReviewResponse> call, Throwable t) {
  533. Log.d("Error", t.getMessage());
  534. Toast.makeText(DetailActivity.this, "Error fetching Review", Toast.LENGTH_SHORT).show();
  535. }
  536. });
  537. } catch (Exception e) {
  538. Log.d("Error", e.getMessage());
  539. Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
  540. }
  541. }
  542.  
  543. public void addFavorites() {
  544.  
  545.  
  546. int movie_id = getIntent().getExtras().getInt("id");
  547.  
  548. dbHelper = new MovieDbHelper(activity);
  549. favoriteMovie = new Movie();
  550.  
  551. String rate = getIntent().getExtras().getString("vote_average");
  552. String poster = getIntent().getExtras().getString("poster_path");
  553.  
  554.  
  555. favoriteMovie.setId(movie_id);
  556. favoriteMovie.setOriginalTitle(movieName.getText().toString().trim());
  557. favoriteMovie.setPosterPath(poster);
  558. favoriteMovie.setVoteAverage(Double.parseDouble(rate));
  559. favoriteMovie.setOverview(synopsis.getText().toString().trim());
  560. favoriteMovie.setReleaseDate(releaseDate.getText().toString().trim());
  561.  
  562.  
  563. dbHelper.addFavorites(favoriteMovie);
  564. Log.d(TAG, "Added fav.....");
  565.  
  566.  
  567. }
  568.  
  569. private void removeFromFavorites(int id) {
  570.  
  571.  
  572. getApplicationContext().getContentResolver().delete(MovieContract.MovieEntry.CONTENT_URI, MovieContract.MovieEntry.COLUMN_MOVIE_ID + " = ?",
  573. new String[]{String.valueOf(id)});
  574. }
  575.  
  576. private boolean checkFavoriteMovie(int id) {
  577. Cursor cursor = activity.getContentResolver().query(MovieContract.MovieEntry.CONTENT_URI, null, MovieContract.MovieEntry.COLUMN_MOVIE_ID + " = " + id, null, null);
  578. assert cursor != null;
  579. if (cursor.getCount() <= 0) {
  580. cursor.close();
  581. return false;
  582. }
  583. cursor.close();
  584. return true;
  585. }
  586.  
  587.  
  588. }
  589.  
  590. ReviewAdapter
  591.  
  592.  
  593. public class ReviewAdapter extends
  594. RecyclerView.Adapter<ReviewAdapter.MyViewHolder>{
  595.  
  596. private Context mContext;
  597. private List<Review> reviewsList;
  598.  
  599.  
  600. public ReviewAdapter(Context mContext, List<Review> reviewsList){
  601.  
  602.  
  603. this.mContext = mContext;
  604. this.reviewsList = reviewsList;
  605. }
  606.  
  607. @Override
  608. public ReviewAdapter.MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i){
  609. View view = LayoutInflater.from(viewGroup.getContext())
  610. .inflate(R.layout.review, viewGroup, false);
  611.  
  612. return new MyViewHolder(view);
  613.  
  614. }
  615. @Override
  616. public void onBindViewHolder(final ReviewAdapter.MyViewHolder holder, int i){
  617. holder.reviewAuthor.setText(reviewsList.get(i).getAuthor());
  618. holder.reviewContent.setText(reviewsList.get(i).getContent());
  619.  
  620. }
  621.  
  622. @Override
  623. public int getItemCount(){
  624. return reviewsList.size();
  625.  
  626. }
  627. public class MyViewHolder extends RecyclerView.ViewHolder{
  628.  
  629. TextView reviewAuthor;
  630. TextView reviewContent;
  631.  
  632.  
  633. public MyViewHolder(View view){
  634. super(view);
  635. reviewAuthor = view.findViewById(R.id.review_author);
  636. reviewContent = view.findViewById(R.id.review_content);
  637.  
  638.  
  639.  
  640. view.setOnClickListener(new View.OnClickListener() {
  641. @Override
  642. public void onClick(View v) {
  643. int pos = getAdapterPosition();
  644. if(pos != RecyclerView.NO_POSITION){
  645. String author = reviewsList.get(pos).getAuthor();
  646. String content = reviewsList.get(pos).getContent();
  647.  
  648. reviewAuthor.setText(author);
  649. reviewContent.setText(content);
  650.  
  651. }
  652. }
  653. });
  654. }
  655. }
  656.  
  657. }
Add Comment
Please, Sign In to add comment