Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jaskirat #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:recyclerview-v7:25.1.0'
compile 'com.android.support:cardview-v7:25.1.0'

testCompile 'junit:junit:4.12'
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,47 @@

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public class MainActivity extends AppCompatActivity {
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity
{
List<Movie> movieList=new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

RecyclerView cycle=(RecyclerView) findViewById(R.id.recycle);
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(this);
cycle.setLayoutManager(layoutManager);

Movie e=new Movie("The Shawshank Redemption","1994","Thriller",R.drawable.shawshank);
Movie f=new Movie("The Pursuit of Happyness","2006","Biography",R.drawable.happyness);
Movie b=new Movie("Fifty Shades of Grey","2011","Romance",R.drawable.grey);
Movie g=new Movie("Titanic","1997","Drama/Disaster",R.drawable.titanic);
Movie a=new Movie("The Wolf of Wall Street","2013","Biography",R.drawable.wolf);
Movie c=new Movie("The Dictator","2012","Comedy",R.drawable.dictator);
Movie d=new Movie("The Godfather","1972","Drama",R.drawable.godfather);
Movie h=new Movie("The Dark Knight","2008","Crime",R.drawable.joker);
Movie i=new Movie("La La Land","2016","Romance",R.drawable.lala);

movieList.add(a);
movieList.add(b);
movieList.add(c);
movieList.add(d);
movieList.add(e);
movieList.add(f);
movieList.add(g);
movieList.add(h);
movieList.add(i);

MovieAdapter movieAdapter=new MovieAdapter(this,movieList);
cycle.setAdapter(movieAdapter);
}
}
83 changes: 83 additions & 0 deletions app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.sdsmdg.hareshkh.lectureassignment;

/**
* Created by Jaskirat on 03-02-2017.
*/

public class Movie
{
private String name;
private String year;
private String genre;
private int img;

//

private boolean isSelected;

public boolean getSelected()
{
return isSelected;
}

public void setSelected(boolean x)
{
isSelected=x;
}

//

public Movie()
{
isSelected=false;
}

public Movie(String name, String year, String genre, int img)
{
this.name = name;
this.year = year;
this.genre = genre;
this.img = img;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public String getYear()
{
return year;
}

public void setYear(String year)
{
this.year = year;
}

public String getGenre()
{
return genre;
}

public void setGenre(String genre)
{
this.genre = genre;
}

public void setImg(int img)
{
this.img = img;
}

public int getImg()
{
return img;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package com.sdsmdg.hareshkh.lectureassignment;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

/**
* Created by Jaskirat on 03-02-2017.
*/

public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MyViewHolder>
{
private Context mContext;
private List<Movie> movieList;
private String line;

public class MyViewHolder extends RecyclerView.ViewHolder
{
public TextView genre, name, year;
public ImageView thumbnail;

public MyViewHolder(View view)
{
super(view);
name = (TextView) view.findViewById(R.id.title);
year = (TextView) view.findViewById(R.id.yr);
genre= (TextView) view.findViewById(R.id.gen);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
}
}

public MovieAdapter(Context mContext, List<Movie> movieList) {
this.mContext = mContext;
this.movieList = movieList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.movie_card, parent, false);

return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, int position)
{
final Movie movie = movieList.get(position);
holder.name.setText(movie.getName());
holder.year.setText(movie.getYear());
holder.genre.setText(movie.getGenre());


Bitmap bitmap,scaled;
Drawable drawable;

switch(movie.getImg())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do not need a switch case here. You could just write the following replacing the whole switch case code.
drawable = ContextCompat.getDrawable(mContext, movie.getImg());

  •                                      bitmap = ((BitmapDrawable)drawable).getBitmap();
    
  •                                      scaled=Bitmap.createScaledBitmap(bitmap,200,200,false);
    
  •                                      holder.thumbnail.setImageBitmap(scaled);
    

{
case ((int) R.drawable.grey): drawable = ContextCompat.getDrawable(mContext, R.drawable.grey);
bitmap = ((BitmapDrawable)drawable).getBitmap();
scaled=Bitmap.createScaledBitmap(bitmap,180,180,false);
holder.thumbnail.setImageBitmap(scaled);
break;

case ((int) R.drawable.shawshank): drawable = ContextCompat.getDrawable(mContext, R.drawable.shawshank);
bitmap = ((BitmapDrawable)drawable).getBitmap();
scaled=Bitmap.createScaledBitmap(bitmap,180,180,false);
holder.thumbnail.setImageBitmap(scaled);
break;

case ((int) R.drawable.joker): drawable = ContextCompat.getDrawable(mContext, R.drawable.joker);
bitmap = ((BitmapDrawable)drawable).getBitmap();
scaled=Bitmap.createScaledBitmap(bitmap,180,180,false);
holder.thumbnail.setImageBitmap(scaled);
break;

case ((int) R.drawable.dictator): drawable = ContextCompat.getDrawable(mContext, R.drawable.dictator);
bitmap = ((BitmapDrawable)drawable).getBitmap();
scaled=Bitmap.createScaledBitmap(bitmap,180,180,false);
holder.thumbnail.setImageBitmap(scaled);
break;

case ((int) R.drawable.wolf): drawable = ContextCompat.getDrawable(mContext, R.drawable.wolf);
bitmap = ((BitmapDrawable)drawable).getBitmap();
scaled=Bitmap.createScaledBitmap(bitmap,180,180,false);
holder.thumbnail.setImageBitmap(scaled);
break;

case ((int) R.drawable.lala): drawable = ContextCompat.getDrawable(mContext, R.drawable.lala);
bitmap = ((BitmapDrawable)drawable).getBitmap();
scaled=Bitmap.createScaledBitmap(bitmap,180,180,false);
holder.thumbnail.setImageBitmap(scaled);
break;

case ((int) R.drawable.titanic): drawable = ContextCompat.getDrawable(mContext, R.drawable.titanic);
bitmap = ((BitmapDrawable)drawable).getBitmap();
scaled=Bitmap.createScaledBitmap(bitmap,180,180,false);
holder.thumbnail.setImageBitmap(scaled);
break;

case ((int) R.drawable.godfather): drawable = ContextCompat.getDrawable(mContext, R.drawable.godfather);
bitmap = ((BitmapDrawable)drawable).getBitmap();
scaled=Bitmap.createScaledBitmap(bitmap,180,180,false);
holder.thumbnail.setImageBitmap(scaled);
break;

case ((int) R.drawable.happyness): drawable = ContextCompat.getDrawable(mContext, R.drawable.happyness);
bitmap = ((BitmapDrawable)drawable).getBitmap();
scaled=Bitmap.createScaledBitmap(bitmap,180,180,false);
holder.thumbnail.setImageBitmap(scaled);
break;
}


// loading album cover using Glide library
//Glide.with(mContext).load(movie.getImg()).into(holder.thumbnail);


holder.thumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

holder.itemView.performClick();
}
});

holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
line= (String) holder.name.getText();
line+=" was clicked";

Toast toast=Toast.makeText(mContext, line, Toast.LENGTH_SHORT);
if(toast!=null)
toast.show();
}
});


//holder.itemView.setBackgroundColor(context.getResources().getColor(movie.getSelected() ? R.color.delete_gray: R.color.White));

holder.itemView.setBackgroundColor(movie.getSelected() ? Color.CYAN : Color.WHITE);

holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {

movie.setSelected(!movie.getSelected());
holder.itemView.setBackgroundColor(movie.getSelected() ? Color.CYAN : Color.WHITE);
return false;
}
});
}

@Override
public int getItemCount()
{
return movieList.size();
}

}
Binary file added app/src/main/res/drawable/dictator.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/godfather.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/grey.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/happyness.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/joker.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/lala.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/shawshank.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/titanic.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/wolf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 9 additions & 5 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@color/gray"
tools:context="com.sdsmdg.hareshkh.lectureassignment.MainActivity">

<TextView
android:layout_width="wrap_content"
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Good luck with your assignments" />
android:id="@+id/recycle" >

</android.support.v7.widget.RecyclerView>

</RelativeLayout>
Loading