4

I want to Show different border of every gridview item. how can i set border to every item dynamically .

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>

<ImageView
    android:id="@+id/grid_item_image"
    android:layout_width="30px"
    android:layout_height="30px"
    android:src="@drawable/shape" >
</ImageView>

</LinearLayout>

activity_main layout file :

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<GridView
    android:id="@+id/gridView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:numColumns="15" 
    android:verticalSpacing="1dp"
    android:horizontalSpacing="1dp"
    android:background="#f3f3f3"
    >
</GridView>

</RelativeLayout>

Main Class:

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    gridView = (GridView) findViewById(R.id.gridView1);

    gridView.setAdapter(new ImageAdapter(this));

    gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
            Toast.makeText(
               getApplicationContext(),
               /*((TextView) v.findViewById(R.id.grid_item_label)).getText() + */String.valueOf(position), Toast.LENGTH_SHORT).show();
            ImageView imageView = (ImageView) v.findViewById(R.id.grid_item_image);
            imageView.setBackgroundColor(Color.BLUE);


        }
    });

Adapter Class:

 public class ImageAdapter extends BaseAdapter {
private Context context;

public ImageAdapter(Context context) {
    this.context = context;
}

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

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    if (convertView == null) {
        gridView = new View(context);
        gridView = inflater.inflate(R.layout.mobile, null);
    } else {
        gridView = (View) convertView;
    }

    return gridView;
}

@Override
public int getCount() {
    return 300;
}

@Override
public Object getItem(int position) {
    return null;
}

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

}

EDIT

i have added a shape file to my custom design file

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@null" />
<stroke android:width="1dp" android:color="#000000" />
<padding android:left="1dp" android:top="1dp" android:right="1dp"
android:bottom="1dp" />
</shape>
Rohit
  • 3,401
  • 4
  • 33
  • 60

3 Answers3

2

Save a XML file in your Drawable folder with this code :

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_enabled="true"><shape>
            <solid android:color="#FFFFFF" />

            <stroke android:width="1dp" android:color="#CACACA" />

            <corners android:radius="5dp" />

     <!--       <padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" />-->
        </shape></item>

</selector>

Now use this as a background of your image view and set your iamges as background resources

Vipul Purohit
  • 9,807
  • 6
  • 53
  • 76
1

Inside your class ImageAdapter --> getView(...) method, add this line before return gridView; statement

gridView.setBackgroundColor(Color.RED);

Edit

Also add android:layout_margin="10dp" in your image view...

Take this for ref.. In the provided link border is applied to complete grid view , but its gives an idea of border setting, use my logic mentioned to achieve this..

Hope this helps..

Community
  • 1
  • 1
CRUSADER
  • 5,486
  • 3
  • 28
  • 64
  • this has set my image's bg color , but i want to set border – Rohit Jun 14 '13 at 05:02
  • i want to set Border color not background color – Rohit Jun 14 '13 at 05:18
  • You must set background and also set `android:layout_margin="10dp"` for imageView, it will look like a border – CRUSADER Jun 14 '13 at 05:27
  • R u kidding sir.... i want to set Border dynamically to every image... i have already set a simple border, but i need to change it in java file, i will make a array of colors and set acording to its position – Rohit Jun 14 '13 at 05:30
  • and i have searched many things but don't find answer that's y i have posted question – Rohit Jun 14 '13 at 05:31
  • Well, if you have an array of colors then even better,, setcolors depending on position..like `if(position == 0){gridView.setBackgroundColor(Color[position]);}else{....}` – CRUSADER Jun 14 '13 at 05:38
  • why you again come on BackgroundColor , i want to change only border color , not the background color, background color will transparent in every image – Rohit Jun 14 '13 at 06:08
1
gridView = (GridView)findViewById(R.iMyGridview);
gridView.setBackgroundColor(Color.LTGRAY);
gridView.setVerticalSpacing(1);
gridView.setHorizontalSpacing(1);
Vinit M
  • 53
  • 2
  • 9