Android programmatically depicts an image as a background

I have an image that I want to draw horizontally, and I need to do this programmatically. I tried two ways and both of them do not work.

button_inner_shadow - image navigation_background is the xml that would be assumed to draw the above image

1: tile image directly

BitmapDrawable navigationBackground = new BitmapDrawable(BitmapFactory.decodeResource( getResources(), R.drawable.button_inner_shadow)); navigationBackground.setTileModeX(Shader.TileMode.REPEAT); navigationTextViews[id].setBackgroundDrawable(navigationBackground); 

2: use xml to create image

 navigationBackground = new BitmapDrawable(BitmapFactory.decodeResource( getResources(), R.drawable.navigation_background)); navigationTextViews[id].setBackgroundDrawable(navigationBackground); 

navigation_background

 <?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:dither="true" android:src="@drawable/button_inner_shadow" android:tileMode="repeat" /> 

What am I doing wrong?

I also use setBackgroundResource to change the background color in another part of the program and thought it was a problem. I added navigationTextViews[id].setBackgroundResource(0); which should remove the background resource, and this does not work with the above solutions that I used.

EDIT: navigationTextViews [] is an array of TextViews

+7
source share
1 answer

Well, for some reason, the first option worked with a different image, so I guess the problem is that my first image (which is a gradient with some transparency) was too transparent.

+3
source

All Articles