Skip to content Skip to sidebar Skip to footer

Android: How I Can I Add A Background Image To A Gridlayout?

I have a GridLayout full of buttons - similar to a calculate with spaces between the buttons. I wants to skin the background including the those areas between the buttons but not t

Solution 1:

GridView is derived from View class. The View class has a method called void setBackgroundResource (), you can call it on your grid view object, pass an id of a drawable resource to it. This method was introduced since API level 1, so you don't have to worry about API compatibility issues.

Solution 2:

Do one thing keep grid view insdie the LinearLayout,And set your desire image as a backgroud of that LinearLayout.

Soo your LinearLayout become Parent and Gridview as a Child View. Just like that

Bitmapbitmap= BitmapFactory.decodeResource(context.getResources(), R.drawable.image1);
LinearLayout.setBackgroundDrawable(newBitmapDrawable(bitmap));

Solution 3:

You can add a background image to a GridLayout easily like this:

A) gridLayout.setBackgroundResource(R.drawable.background_image);

B) Resources res = getResources(); Drawable drawable = res.getDrawable(R.drawable.background_image); gridLayout.setBackground(drawable);

However, please note, that I found when OTA upgraded from Android 6.0 to 7.1.1 (Galaxy J5 (2016)), that GridLayout doesn't support displaying a background drawable since API 24. (Neither of the above code seems to be working above API 23, not even on Android Emulator images)

However, TableLayout's background image is properly displayed even at API 24+.

(Therefore I had to rewrote all my relevant code from GridLayout to TableLayout, but these layouts quite similar, so it not much a problem.)

Post a Comment for "Android: How I Can I Add A Background Image To A Gridlayout?"