How to set a drawable over an image view - android

I have a drawbale i want to place it in a gridView for each each image in this gridView.
My drawable is a like a square but filled at the edges. So when i set this drawable as a background for the image, the image will cover the background which i dont need.
I need this drawable to be in-front of the of the image not at the back.
My code look like this:
enter code here
public class ComponentAlbums extends LinearLayout
{
public ClassImageLoader imageLoader;
public ComponentAlbums(Activity activity,Context context,WallPaperHD_Album item)
{
super(context);
imageLoader = new ClassImageLoader(activity);
setOrientation(LinearLayout.HORIZONTAL);
setBackgroundColor(Color.TRANSPARENT);
LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(120, 120);
ImageView imageView = new ImageView(context);
Params.gravity = Gravity.CENTER;
imageView.setScaleType(ScaleType.FIT_XY);
imageView.setPadding(10, 10, 10, 10);
//imageView.setAdjustViewBounds(true);
imageView.setBackgroundResource(R.drawable.frame_all_albums);//this line of code iam using to set the drawable infront of the image.
any help please.

A simple way to reverse the background and the actual image.
imageView.setImageResource(R.drawable.frame_all_albums);
...
imageView.setBackgroundResource(actualImageBitmap);
Hope this helps.

Related

Android first ImageView in vertical LinearLayout stretching

I'm creating the above popup, the content of which consists of rows of horizontal LinearLayout views within a main vertical LinearLayout. Each horizontal LinearLayout contains one ImageView and one TextView.
I'm creating this within a PopupWindow, and doing so programmatically so that I can change the ImageView source as required.
As you can see the first icon seems to take up a lot of space, despite having the same code generating it as the other icons.
Below is the code:
LinearLayout verticalLayout = new LinearLayout(context);
verticalLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams mainLayoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
verticalLayout.setLayoutParams(mainLayoutParams);
LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams iconParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams textParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//History row
LinearLayout historyLayout = new LinearLayout(context);
historyLayout.setLayoutParams(layoutParams);
historyLayout.setOrientation(LinearLayout.HORIZONTAL);
ImageView historyIcon = new ImageView(context);
historyIcon.setImageResource(R.drawable.small_book_grey);
historyIcon.setAdjustViewBounds(true);
historyIcon.setLayoutParams(iconParams);
historyLayout.addView(historyIcon);
TextView historyText = new TextView(context);
historyText.setLayoutParams(textParams);
historyText.setText("History");
historyLayout.addView(historyText);
verticalLayout.addView(historyLayout);
//Exam row...
//... (duplicate of history row)
I've tried playing around with the layout parameters, even creating a mock xml layout that displays the content as I'd like, to match the parameters to.
If anyone can give some advice on making that book icon the same size as the others, I'd be grateful.
Add a scaleType to ImageView of fitCenter
Write this code under historyIcon.setAdjustViewBounds(true);
historyIcon.setWidth()`
And put width according to your layout.
Although I didn't figure out why the first image was scaling differently to the other images, I did find another solution: Using compound left drawables.
historyText.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Drawable img = m_context.getResources().getDrawable(
R.drawable.small_book_grey);
img.setBounds(0, 0, img.getIntrinsicWidth() * historyText.getMeasuredHeight() / img.getIntrinsicHeight(), historyText.getMeasuredHeight());
historyText.setCompoundDrawables(img, null, null, null);
historyText.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
Manually setting the bounds to match the TextView worked. Seems clunky, but it was the only way I could get it to do what I was aiming for.

Why is only one ImageView drawn to my Layout

I want to add multiple ImageView, that show a drawable, to a RelativeLayout. This short example should add two such ImageViews:
//get drawable
Drawable oval = this.getResources().getDrawable(R.drawable.circle);
// set color
oval.setColorFilter(imgEl.getColor(), PorterDuff.Mode.SRC_ATOP);
// create new image view
ImageView imageView = new ImageView(this);
//set drawable and add to layout
imageView.setImageDrawable(oval);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(100,100));
imageView.setPadding(0,0,0,0);
mLayout.addView(imageView);
//get drawable
oval = this.getResources().getDrawable(R.drawable.circle);
// set color
oval.setColorFilter(imgEl.getColor(), PorterDuff.Mode.SRC_ATOP);
// create new image view
imageView = new ImageView(this);
//set drawable and add to layout
imageView.setImageDrawable(oval);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(100,100));
imageView.setPadding(200,200,0,0);
mLayout.addView(imageView);
Unfortunately, only the first one is shown, but the second one is not drawn on the screen. What is my mistake?

Adding image to LinearLayout each time AlertDialog item is selected

I am trying to add an image to a LinearLayout each time the user selects an item from an AlertDialog modal but once the first item is selected, no other images will appear and the first image doesn't even change to the newly selected item. Ideally, i would like the first image to appear in the center of the layout as it does and when a second choice is selected, I would like it to appear to the right of the first image and then both are centered.
Thanks to anyone looking at this.
Below is the ImageView and LinearLayout portion of my code. This code is in the onClick() method for the AlertDialog
ImageView imageView = new ImageView(getApplicationContext());
LinearLayout l = (LinearLayout) findViewById(R.id.picLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
imageView.setId(which);
imageView.setLayoutParams(params);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
int pic = Pic.getPicID(which);
imageView.setImageResource(pic);
l.addView(imageView, items.size()-1, params);

TransitionDrawable not changing the color as expected

I have the following in a class ProgressIndicatorView. The view, when used holds the five rectangles below :
When the user presses the button "Fortsæt" I call the views method setProgressIndicator(int step) which sets the color of the next rectangle in the view to fully opaque. The method looks like this :
public void setProgressIndicator(int activeStep) {
// Fade IN
TransitionDrawable transition = (TransitionDrawable) this.getChildAt(activeStep-1).getBackground();
transition.startTransition(transitionTime);
}
and the init method looks like this
private void init(Context context) {
this.setOrientation(LinearLayout.HORIZONTAL);
transitionColor = getResources().getDrawable(R.drawable.colortransition);
for (int i = 0; i < squareAmount; i++) {
ImageView loadingPiece = new ImageView(context);
//loadingPiece.setBackgroundColor(transparentWhite);
loadingPiece.setBackgroundDrawable(transitionColor);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.FILL_PARENT, 1.0f);
lp.setMargins(0, 15, 4, 15);
this.addView(loadingPiece, lp);
}
}
The method works perfectly if I use a static backgroundcolor as the resource but doesn't when I use the transitioncolor drawable. Any suggestions as to what could be causing the issue ?
edit: for clarification THIS is what the view looks like when I use this.getChildAt(activeStep-1).setBackgroundColor(opaqueWhite);
but THIS is what it looks like when I use the current implementation of setProgressIndicator:
What is causing this to happen?
edit: Is there another approach that I could possibly to accomplish this effect if the cause of this problem cannot be located?

Android - add image from a class to a layout

So basicaly the theory is this: i want to create a class that holds a bitmap, which is loaded from a resource. then i want to create a new object of that class and add it to a layout.
So far i've managed to do everything except idk, how to add the bitmap to a layout.
And i want it to be displayed on an existing layout.
bitmap class:
class BitmapView extends View
{
public BitmapView(Context context)
{
super(context);
}
#Override
public void onDraw(Canvas canvas) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.wave);
// canvas.drawColor(Color.BLACK);
canvas.drawBitmap(bmp, 10, 10, null);
}
}
and i call it with setContentView(new BitmapView(this));
but this draws the picture over the whole screen. i want it to display the background that was set in a previous layout. Also is there any way i can set bitmap size?
Thanks
Use an Imageview for displaying bitmap
ImageView imgView=new ImageView(context);
imgView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
imgView.setBitmapImage(bitmap); //Your bitmap goes here.
setContentView(imgView);
use setImageBitmap(bmp). Put an ImageView inside your layout. Reference it with ImageView im = (ImageView) findViewById(R.id.myBitmapId);. When you create your bitmap simply do im.setImageBitmap(bmp);

Categories

Resources