user3978148
user3978148

Reputation:

Can't set background color for GridView layouts

I've never worked with GridView on Android. I try to inflate my layout in a GridView, and i want to have different background for every layout. I can't set background color for each layout separately, everytime i get gray color for all layouts.

My XML code :

<RelativeLayout
    android:layout_width="200dp"
    android:layout_height="150dp"
    android:id="@+id/back">


    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/imageView"
        android:layout_marginTop="10dp"
        android:src="@drawable/splashscreen"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Science"
        android:textSize="15sp"
        android:layout_marginTop="10dp"
        android:id="@+id/categoryTextView"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Grid adapter :

public class CellAdapter extends BaseAdapter {
private Context context;
private LinkedList<CellGrid> list;

public CellAdapter(Context context, LinkedList<CellGrid> list){
    this.context = context;
    this.list = list;
}


@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = (View) inflater.inflate(
                R.layout.grid_cell_layout, null);
    }

    convertView.setBackgroundColor(list.get(position).getBackground());
    TextView categoryTextView = (TextView)convertView.findViewById(R.id.categoryTextView);
    ImageView imageView = (ImageView)convertView.findViewById(R.id.imageView);
    categoryTextView.setText(list.get(position).getText());
    imageView.setImageResource(list.get(position).getImageId());
    RelativeLayout relativeLayout = (RelativeLayout) convertView.findViewById(R.id.back);
    Log.d("COLOR", Integer.toString(list.get(position).getBackground()));
    return convertView;
}

}

Upvotes: 1

Views: 819

Answers (5)

m0rphine
m0rphine

Reputation: 126

Do you set color (AARRGGBB int value) or color resource (R.color.*)?

convertView.setBackgroundColor(list.get(position).getBackground());

There should be color in AARRGGBB format. To get value from resources use:

context.getResources().getColor(R.color.your_color);

Upvotes: 0

preethi
preethi

Reputation: 222

Here is my sample code..

**<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:orientation="vertical"
android:background="@color/dark_gray" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_gravity="center"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:background="@android:color/transparent"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/textViewName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:text="TextView"
    android:textColor="@android:color/black"
    android:textSize="13dp" />

**

Upvotes: 0

Rajan Kali
Rajan Kali

Reputation: 12953

Use resources().getColor() to get color from list and set it as background

relativelayout.setBackgroundColor(getResources().getColor(list.get(position).getBackground()));

Upvotes: 0

Amarjit
Amarjit

Reputation: 4357

If any specific color is not satisfied then you can set random bg to every view as :

@Override
public View getView(int position, View convertView, ViewGroup parent) {

// generate the random integers for r, g and b value
    int r = rand.nextInt(255);
    int g = rand.nextInt(255);
    int b = rand.nextInt(255);

    int randomColor = Color.rgb(r,g,b);

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = (View) inflater.inflate(
                R.layout.grid_cell_layout, null);

 convertView.setBackgroundColor(randomColor);
    }

Upvotes: 1

Yogesh Nikam
Yogesh Nikam

Reputation: 633

Please try following code it helpful for you.

Replace your adapter code with following code

@Override
public int getCount() {
   return list.size();
}

@Override 
public Object getItem(int position) {
     return list.get(position);
}

@Override
public long getItemId(int position) {
   return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

if (convertView == null) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = (View) inflater.inflate(
            R.layout.grid_cell_layout, null);
}
    Random rnd = new Random(); 
    int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256),       rnd.nextInt(256)); 
    convertView.setBackgroundColor(color);
    TextView categoryTextView =    (TextView)convertView.findViewById(R.id.categoryTextView);
    ImageView imageView = (ImageView)convertView.findViewById(R.id.imageView);
    categoryTextView.setText(list.get(position).getText());
    imageView.setImageResource(list.get(position).getImageId());
    RelativeLayout relativeLayout = (RelativeLayout) convertView.findViewById(R.id.back);
    Log.d("COLOR", Integer.toString(list.get(position).getBackground()));
    return convertView;
}
}

Feel free for comment if you have any issue

Upvotes: 0

Related Questions