Add ImageView to RelativeLayout in Android - android

So I have created an ImageView programmatically in my Activity.
I want to add this ImageView at the bottom of one of my RelativeLayout.
This is what I tried:
// creating new ImageView
ImageView shadowView = new ImageView (contextPara);
shadowView.SetBackgroundResource (Resource.Drawable.bottomBar_Shadow);
shadowView.SetScaleType (ImageView.ScaleType.FitXy);
// creating ImageView LayoutParams
WindowManagerLayoutParams imgViewLayoutParams = new WindowManagerLayoutParams ();
imgViewLayoutParams.Width = ViewGroup.LayoutParams.MatchParent;
imgViewLayoutParams.Height = (int)imgViewHeight;
imgViewLayoutParams.Gravity = GravityFlags.Bottom;
// Adding ImageView to RelativeLayout
listViewRelativeLayout.AddView (shadowView, imgViewLayoutParams);
My problem is this adds the ImageView to the TOP of the RelativeLayout, NOT the bottom.
I also tried the following:
After I call the AddView and add the ImageView, I set the Y position of the ImageView.
This actually works. It moves the ImageView down but I'm not sure how much to move down. RelativeLayout size is not absolute.
// this moves the ImageView down 100px.
shadowView.SetY (100f);
How can I put it to the bottom of the RelativeLayout?
Please note: I CAN'T just do the math (RelativeLayout height - ImageView Height) because RelativeLayout height is always 0 until OnCreate finishes.
What are my other choices?
Thank you for your time.

Possible Solutions:
1.
Consider placing a single ImageView within the RelativeLayout XML.
You can access this ImageView inside the Activity/Fragment using
ImageView mImageView = (ImageView) view.findViewById(R.id.imageView);
You can set the starting visibility as gone mImageView.setVisibility(View.GONE), and show the ImageView at a later time mImageView.setVisibility(View.VISIBLE);
2.
You can add a sub-layout (LinearLayout) to the existing RelativeLayout, this sub-layout can be placed at the bottom of the RelativeLayout via XML.
Inside the Activity/Fragment, you can target that sub-layout (linearLayout) by its ID, and than add the ImageView programmatically to that layout.
<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"
tools:context="com.modup.fragment.FeedFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout>
android:id="#+id/linearLayout"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</LinearLayout>
</RelativeLayout>

I found a way!
Instead of using WindowManagerLayoutParams to set LayoutParameters, using RelativeLayout.LayoutParams does the job.
Here's how:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.MatchParent, (int)imgViewHeight);
layoutParams.AddRule (LayoutRules.AlignParentBottom);
AlignParentBottom does the job!

Related

Unable to set layout width, height and weight of inflated view in XML in Android

I am developing an Android app. In my app, I need to inflate list of views dynamically. I added them and working. The problem is with setting the width and height of layout. Now I will demonstrate my problem with a simple project. Actually, my project is much more complicated than this simple project.
I am inflating views to this layout.
<LinearLayout
android:orientation="horizontal"
android:id="#+id/cm_photos_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
I am looping through a list of bitmap and adding view dynamically as follow
for(Bitmap bmp : bitmaps)
{
View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,null);
ImageView previewImageView = (ImageView)preview.findViewById(R.id.item_cm_preview_image);
previewImageView.setImageBitmap(bmp);
container.addView(preview);
}
Please note, in the above code, container is a LinearLayout added dynamically to the parent XML in the above.
container = new LinearLayout(this);
container.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
container.setLayoutParams(params);
parentLinearLayout.addView(container);
This is my item_cm_preview_image.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_weight="1"
android:layout_height="400dp"
android:layout_width="0dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:scaleType="centerCrop"
android:id="#+id/item_cm_preview_image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
As you can see, I set the layout height to 400dp, width 0 and layout_weight to 1 in the XML. So all image height must be same and width must be equal because of layout_weight. But the result is not as expected. You can see screenshot below.
As you can see in the screenshot, both layout_weight and height are not working for all inflated views. But if I add extra ViewGroup dynamically and inflate the view to that layout, it is working. Below is my code
//This happening in for loop
LinearLayout wrapper = new LinearLayout(this);
wrapper.setLayoutParams(new LinearLayout.LayoutParams(0,500,1));
View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,null);
ImageView previewImageView = (ImageView)preview.findViewById(R.id.item_cm_preview_image);
previewImageView.setImageBitmap(bmp);
wrapper.addView(preview);
container.addView(wrapper);
This is the result:
As you can see both layout_weight and height working when I use an extra dynamic linear layout. Why is setting layout weight and height in XML not working? Why second way is working? How can I set weight and height in XML layout file? Is it possible?
If you inflate layout using method
View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,null);
it skip its width and height parameters..., but if you will use:
View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,parent,false);
it should work correct, for example if you inflate view in activity as parent you can provide (ViewGroup) getView():
View preview = layoutInflater.inflate(R.layout.item_cm_preview_image,(ViewGroup) getView(), false);

add drawable in relative layout programmatically

I have Image in my layout and I want when user clicked on image, 4 drawable (corner button) added in corner of the image. How can I do this?
Add id field to the relative layout in the xml. Use that id in the class to create view dynamically.
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.YourID);
imageView.setImageDrawable(R.drawable.image);
//Add View to Layout:
relativeLayout.addView(imageView);
well there are many ways to that.
Using layoutparams you can set layout params programatically.
like
LayoutParams param = new LayoutParam(LayoutPara.width, LayoutParam.height);
//your rules...
imageView.setlayoutParams(param);
here you can set margintop, bottom, left and right and alignParentLeft.... so on

Android: Is there any way to to cast ImageView to RelativeLayout

I want to cast and ImageView to RelativeLayout. Because the RelativeLayout is the parent View and there are many images as Child views in this. So I have written code to download image and set it to the Image view. But the case here i want to set this downloaded image as background of the parent view that is of RelativeLayout.
RelativeLayout layout = (RelativeLayout) findViewById(R.id.bg_image);
ImageView bgImage=(ImageView) findViewById(R.id.bground_image);
downloader = new ImageDownloader();
downloader.download(item.imageurl(), bgImage);
/// Image is now set in bgImage//
///////// Now here i want bgImage to be set as a background of layout////
thankx in advance
To set a background image you can use something like
layout.setBackground(new BitmapDrawable(<your image>));
Best way I think is take one another Imageview with same dimensions as Relativelayout(may be fill parent) with scaletype fitxy and set the image to this as you are doing lazy loding and cant pass that relativelayout to loader....... this fitxy will work as background of parent layout
Or another way is change your downloder code to accept relative layout and set background once you have the image
Just add the image to the background of RelativeLayout. It should be 1st in order to be behind all other views in the layout. Then you can use it with code that expects an ImageView.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:id="#+id/an_image"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!--other views go here-->
</RelativeLayout>
And no, you can't cast between these types, obviously. This is Java!
No this is simply not possible because these are completely different types.
You can cast any RelativeLayout or ImageView to View, because this is the base class for all views.
But what you are trying to do should be a little different. You can inflate the ImageView from the RelativeLayout because it is the parent.
I will add a code snippet later.
Update :
// First inflate your layout
View layout = (View)findViewById(R.layout.relativelayout);
// Then inflate the imageview from the layout
ImageView imgview = (ImageView)layout.findViewById(R.id.bground_image);
// Then do your download logic with the imageview
downloader = new ImageDownloader();
downloader.download(item.imageurl(), imgview);
Let me know if this works ;).

Android Set ImageView in front of Relative Layout

I have a RelativeLayout in my Android App. Now I want to show an ImageView in front of that Layout. The problem is that the ImageView is not in the front, it's a bit transparent and I can see things like EditText and Button. I can't change the Layout (setContentView), because the Layout is created dynamically and after setContentView, the Controls are away.
You can add view programmatically, and in the way that it will be on the top!
create id for you top level layout
Now some code (in my case it's relative layout):
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.relative_layout_id);
ImageView imageView = new ImageView(context)
Drawable rightArrowBlackDrawable = getResources().getDrawable(R.drawable.image);
imageView.setLayoutParams(getLayoutParams());
relativeLayout.addView(imageView);
imageView.bringToFront();
//here just example layout params, use yours params ;-)
private RelativeLayout.LayoutParams getLayoutParams() {
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
return layoutParams;
}
You can bring it to the front once you insert it.
imageView.bringToFront();
If the image is transparent, you can set a white background to prevent things below it from showing.
imageView.setBackgroundColor(0xFFFFFF);

android: retain attributes children in RelativeLayout dynamically adding ImageViews

How can I retain attributes of the children in a RelativeLayout when dynamically adding ImageViews?
I have a custom ImageView I want to add at runtime to an empty RelativeLayout (nothing inside in XML), I can add the first, then move, scale and rotate them, it works fine.
When I add another ImageView all previously added instances loose their position and their size, but when I touch them they get back just their size, not the position.
In my ImageView I'm overriding onDraw and onTouch, do I need to override something else?
Maybe I have to write my own RelativeLayout implementation? I wouldn't!
This is the pseudocode for adding new ImageView:
create new instance of ImageView
set bitmap
set scaletype
add imageview to the RelativeLayout container
I even tried to set a standard layout parameter for the brand new added ImageView with the same result.
I tried to get the margins for every children, based on its position, and the re set the layout with ImgeView.setLayout(l, t, r, b);
no success...
Here's the XML RelativeLayout container, pretty simple, maybe too much?
<RelativeLayout
android:id="#+id/face_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
Do you need more details to help me? Please ask, I can post the code too, but I have to clean it up a bit before, I can do it tomorrow, in the meantime please give me some advice.
In a RelativeLayout all view positions are related to the others views. So, as you do in xml, you should provide for each view the relative parameters.
Before add imageView to the RelativeLayout call setLayoutParams(layoutParams):
// Create a new RelativeLayout.LayoutParams instance
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// add roules
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
layoutParams.addRule(.........
layoutParams.addRule(.........
//add params to view
imageView.setLayoutParams(layoutParams);
//add imageview to the RelativeLayout container
yourRelativeLayout.addView(imageView);
Look here for all rules.

Categories

Resources