Hello i would like to know how to assign a color for every relativelayout in a Cursor Adapter. I want palette to extract the colors of the album art and use those colors as background to a relative layout.Please help.
public class AlbumAdapter extends CursorAdapter{
private final LayoutInflater mInflater;
public AlbumAdapter(Activity context, Cursor c) {
super(context, c);
mInflater= LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) view.getTag();
if (holder == null) {
holder = new ViewHolder();
holder.albumTitle = (TextView) view.findViewById(R.id.albumgrid);
holder.artistName = (TextView) view.findViewById(R.id.artistgrid);
holder.coverAlbum = (ImageView)view.findViewById(R.id.square_image);
}view.setTag(holder);
String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.AlbumColumns.ARTIST));
String albumname = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.AlbumColumns.ALBUM));
long albumId = cursor.getLong(cursor.getColumnIndexOrThrow(BaseColumns._ID));
final Uri ART_CONTENT_URI = Uri.parse("content://media/external/audio/albumart");
Uri albumArtUri = ContentUris.withAppendedId(ART_CONTENT_URI, albumId);
holder.coverAlbum.setBackgroundResource(R.drawable.default_artwork);
Picasso.with(context).load(albumArtUri)
.into(holder.coverAlbum);
holder.albumTitle.setText(albumname);
holder.artistName.setText(artist);}
static class ViewHolder {
TextView albumTitle;
TextView artistName;
ImageView coverAlbum;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(R.layout.griditemlayout, parent, false);
}
}
public class AlbumFragment extends Fragment implements LoaderCallbacks<Cursor>{
GridView albumlist;
AlbumAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_album, container, false);
albumlist = (GridView) myFragmentView.findViewById(R.id.albumList);
return myFragmentView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mAdapter = new AlbumAdapter(getActivity(), null);
albumlist.setAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String select = null;
return new CursorLoader(getActivity(),MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
new String[] {
/* 0 */
BaseColumns._ID,
/* 1 */
MediaStore.Audio.AlbumColumns.ALBUM,
/* 2 */
MediaStore.Audio.AlbumColumns.ARTIST,
/* 3 */
MediaStore.Audio.AlbumColumns.NUMBER_OF_SONGS,
/* 4 */
MediaStore.Audio.AlbumColumns.FIRST_YEAR,
/* 5 */
MediaStore.Audio.AlbumColumns.ALBUM_KEY
}, null, null, MediaStore.Audio.AlbumColumns.ALBUM);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}
I know it is a little bit late but, try this:
And in your adapter:
Hope it is useful to someone.