Why is only one ImageView drawn to my Layout - android

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?

Related

How to automatically create imageview in android

My android project connected to server.And i retrieve data from server if it's 5 then create 5 imageview in linearlayout in other words automatically create imageview, depends on retrieved data.
This code Loops over number of images and creates new ImageView for each time, and then it adds them to the Parent Linear Layout. You can also set the LayoutParams of ImageView dynamically as well.
LinearLayout layout = (LinearLayout)findViewById(R.id.parentLayout);
for(int i=0;i<number_of_images;i++) {
ImageView image = new ImageView(context);
layout.addView(image);
}
For Linear Layout:
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
for(int i=0;i<Number_Images;i++){
//ImageView Setup
ImageView i mageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.play);
//setting image position
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
//adding view to layout
linearLayout.addView(imageView);
}
//make visible to program
setContentView(linearLayout);
Hope this Help You.

How can I center an item on the screen programmatically?

How can I center an item on the screen programmatically?
I want to set imageView in the center of any screen that will run my app.
Assuming your parent is RelativeLayout use this:
ImageView myImageView = (ImageView) findViewById(YOUR_IMAGE_VIEW_ID);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)myImageView.getLayoutParams();
params.addRule(RelativeLayout.CENTER_IN_PARENT);
myImageView.setLayoutParams(params);

how to layer two buttons with graphics/color that are set programmatically

I have to layer two Buttons. The first (top) button is created like this using a .png for the icon.
// create circular button and colorize
View button1 = v.findViewById(bId);
GradientDrawable backgroundGradient = (GradientDrawable)imgIcon.getBackground();
backgroundGradient.setColor(getResources().getColor(R.color.holo_gray_light));
// set icon
button1.setImageDrawable(getResources().getDrawable(R.drawable.ic_phone_ib));
For the 2nd button (bottom):
Button button2 = (Button) v.findViewById(R.id.textButton);
button2.setBackgroundResource(R.drawable.gray_rect);
what I have tried:
1 set the drawable left on the bottom button to the drawable of top button. result: icon only is displayed not the background colored circle.
2 create a RoundRectangle using ShapeDrawable then create 2 layers and use LayerDrawable to set the background of the button:
int r= 20;
float[] outerR=new float[]{r,r,r,r,r,r,r,r};
RoundRectShape rr=new RoundRectShape(outerR,null,null);
ShapeDrawable drawable=new ShapeDrawable(rr);
drawable.getPaint().setColor(getResources().getColor(R.color.gray_189));
// get bitmap from button1
BitmapDrawable bm1 = (BitmapDrawable)button1.getDrawable();
// layer them
Drawable drawableArray[]= new Drawable[]{drawable, bm1};
LayerDrawable layerDraw = new LayerDrawable(drawableArray);
layerDraw.setLayerInset(1, 15, 15, 0, 0);//set offset of 2 layer
textButton.setBackground(layerDraw);
result: same as for (1).
Here is the desired result:
button1 is blue with icon, button2 is gray rounded rectangle with text.
figured it out using RelativeLayout in the xml for the list item. I used 2 table rows, one for each button and oriented them such that the button layers accordingly and I was able to set the icon and background color programmatically.

How to set a drawable over an image view

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.

Adding button on top of Imageview at runtime

I am creating a imageview at runtime and now want a add a button on top of the Imageview dynamically at run time and if I pan the image in the imageview the button in it should also pan accordingly. The problem is that Imageview does not allow to add subviews in it.
I am able to add the button on top of the imageview but when I pan the imageview the button remains constant in the screen. Any idea on how to do this?
Here is what I did:
panImageView = new PanView(this); //Custom Imageview class to handle panning
panImageView.setScaleType(ImageView.ScaleType.CENTER);
//panimg.setAdjustViewBounds(true);
FrameLayout mainRL = (FrameLayout) findViewById(R.id.mainRelativeLayout);
mainRL.addView(panImageView);
ImageWorker imgTask = new ImageWorker(panImageView, is);
imgTask.execute(2000, 2000);
//Start button
Button startButton = new Button(this);
int iRndBtn = R.drawable.round_button;
startButton.setBackgroundResource(iRndBtn);
startButton.getBackground().setColorFilter(Color.GREEN,PorterDuff.Mode.MULTIPLY);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(50, 50);
//layoutParams.setMargins(intialPage.mapStartX, intialPage.mapStartY, 0, 0);
layoutParams.setMargins(200, 200, 0, 0);
startButton.setLayoutParams(layoutParams);
//startButton.
mainRL.addView(startButton,layoutParams);
Add Image view first in any Layout and the add Button in that Layout.
Use a RelativeLayout with the ImageView & button. set the button visibility to gone at start. When you need change the visibility.

Categories

Resources