D. Ace
D. Ace

Reputation: 418

How to use intent to display movie poster

I have an app that displays movie posters in the main page when the app runs. Now I’m would like to show the movie poster in DetailMovieActivity class when the user taps on the movie poster.

The following code is able to show the URL of the poster, but the problem is how do I get it to display the actual poster rather than the URL? I would like to achieve this using intent and parcelable. To help me with this, I have been reading this article but it doesn’t make sense to me: http://www.javacodegeeks.com/2014/01/android-tutorial-two-methods-of-passing-object-by-intent-serializableparcelable.html

DetailMovieActivity.java

...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {

   View rootView = inflater.inflate(R.layout.fragment_detail_movie, container, false);

   // The detail Activity called via intent.  
  Intent intent = getActivity().getIntent();
   if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
       String send = intent.getStringExtra(Intent.EXTRA_TEXT);
       ((TextView) rootView.findViewById(R.id.detail_title_textview)).setText(send);

   }
   return rootView;
}

MainActivityFragment.java

...

//Displays URL when poster is tap
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
       String forecast = mMovieAdapter.getItem(position);
       Intent intent = new Intent(getActivity(), DetailMovieActivity.class)
               .putExtra(Intent.EXTRA_TEXT, forecast);
       startActivity(intent);
   }
});

fragment_detail_movie.xml

    <RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
       android:paddingRight="@dimen/activity_horizontal_margin"
       android:paddingTop="@dimen/activity_vertical_margin"
       android:paddingBottom="@dimen/activity_vertical_margin"
       tools:context="com.example.android.sunshine.app.DetailMovieActivity.DetailFragment">

       <ImageView  android:layout_width="wrap_content"
           android:id="@+id/detail_poster_imageview"
           android:layout_height="wrap_content"
          />

       <TextView  android:layout_width="wrap_content"
           android:id="@+id/detail_title_textview"
           android:layout_height="wrap_content"
           android:layout_below="@+id/detail_poster_imageview"/>


</RelativeLayout>

Movie.java

public class Movie implements  Parcelable {


   private String originalTitle;
   private double userRating;
   private String releaseDate;
   private String plotSynopsis;
   private String posterURL;

   public Movie(String originalTitle, double userRating, String releaseDate, String plotSynopsis, String posterPath)
   {
       this.originalTitle = originalTitle;
       this.userRating = userRating;
       this.releaseDate = releaseDate;
       this.plotSynopsis = plotSynopsis;
       this.posterURL =  "http://image.tmdb.org/t/p/w185" + posterPath;
   }



   public String getOriginalTitle() {
       return originalTitle;
   }

   public double getUserRating() {
       return userRating;
   }

   public String getReleaseDate() {
       return releaseDate;
   }

   public String getPlotSynopsis() {
       return plotSynopsis;
   }

   public String getPosterUrl() {
       return posterURL;
   }


   protected Movie(Parcel in) {
       originalTitle = in.readString();
       userRating = in.readDouble();
       releaseDate = in.readString();
       plotSynopsis = in.readString();
       posterURL = in.readString();
   }

   @Override
   public int describeContents() {
       return 0;
   }

   @Override
   public void writeToParcel(Parcel dest, int flags) {
       dest.writeString(originalTitle);
       dest.writeDouble(userRating);
       dest.writeString(releaseDate);
       dest.writeString(plotSynopsis);
       dest.writeString(posterURL);
   }

   @SuppressWarnings("unused")
   public static final Parcelable.Creator<Movie> CREATOR = new Parcelable.Creator<Movie>() {
       @Override
       public Movie createFromParcel(Parcel in) {
           return new Movie(in);
       }

       @Override
       public Movie[] newArray(int size) {
           return new Movie[size];
       }
   };
}

Upvotes: 0

Views: 376

Answers (2)

mur7ay
mur7ay

Reputation: 833

You'll need to create an setOnItemClickListener, something similar to this:

yourView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(getApplicationContext(), New_Activity.class);
                intent.putExtra("imageID", position);
                startActivity(intent);
            }
});

So when a user clicks on the movie poster it'll transition them to the new activity.

Upvotes: 0

Marcin Orlowski
Marcin Orlowski

Reputation: 75635

Standard ImageView widget does not support remote sources. You need to either download it yourself or use libraries like Glide or Picasso that would take care of remote images

Upvotes: 2

Related Questions