Weird top margin when using AddView programmatically - android

I've a big problem I'm trying to add an Imageview programmatically but I face a strange problem:
I've put in the XML just an empty Relative Layout matching the parent.
Code wise I'm adding an imageview to the relative layout.
Everything works, the imageview is visible and sized correctly, but the problem is that the "0,0" position is weird. It seems that it has a Top Margin while instead the Left Margin is correctly 0. Also forcing the position to "0,0" with SetX/SetY results with this wierd top border:
Image:
Code:
base_layout = (RelativeLayout) findVieById(R.id.base_layout);
image_logo = New Imageview(this);
base_layout.AddView(image_logo);
image_logo.SetPadding(0,0,0,0);
image_logo.SetX(0);
image_logo.SetY(0);
image_logo.SetImageResource(R.drawable.generic_image);
image_logo.SetVisibility(View.VISIBLE);
I don't understand why..the activity in the manifest has its usual Theme.NoTitleBar.Fullscreen flag.
Any help? It will be really appreciated!

I put together an example using RelativeLayout.LayoutParams and tried it myself and everything appears to be working fine.
base_layout = (RelativeLayout) findViewById(R.id.base_layout);
ImageView image_logo = new ImageView(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width, height);
image_logo.setLayoutParams(lp);
base_layout.addView(image_logo);
The above shows the below effect when run.
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" was used in the manifest against this Activity.

Related

Android: Imagebutton I set programatically are distored in size

this is like the x-time that I am doing this however today nothing works.
Im basically doing this:
ImageButton btnComments = new ImageButton(this);
ImageButton btngreenLikes = new ImageButton(this);
ImageButton btnblueLikes = new ImageButton(this);
btnComments.SetBackgroundResource(Resource.Drawable.comments_small);
btngreenLikes.SetBackgroundResource(Resource.Drawable.upvote_green);
btnblueLikes.SetBackgroundResource(Resource.Drawable.upvote_blue);
LinearLayout.LayoutParams lpWrap = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
btnComments.LayoutParameters = lpWrap;
btngreenLikes.LayoutParameters = lpWrap;
btnblueLikes.LayoutParameters = lpWrap;
linlayForImages.AddView(btnComments);
linlayForImages.AddView(btngreenLikes);
linlayForImages.AddView(btnblueLikes);
Set add thre imagebuttons in within my code. Give all three a background resource, then set their layout to WRAP CONTENT in HEIGHT and WIDTH.
And then add their views into my layout.
The result ist, they are everything BUT wrapped content. While the comment img is correct, th other two are distored. greenlikes is too big in total size and blue like is top big in width? I use those imagebutton resources in another activity where I set those with the xml and they are fine. So the resource is all clear. Can someone tell me what the HELL is going on here?
Update: Naming my Imagebuttons Imageviews solved this issue. Don't!

Create and remove image at certain coordinates on android

I have an XML Relative layout, with just a few textviews and buttons on it. But I would like to place an image (or sometimes multiple copies of the image) at different x and Y coordinate which is worked out later on.
Unfortunately I can't seem to figure out how to create the ImageView in android (besides XML) and make it appear at the desired coordinate.
Also it would be great if I could help in making it disappear or removing it at a later stage as well.
You can create a ImageView programmatically with ImageView iv = new ImageView(context); Then you have to set the LayoutParameters for the view. If you plan to add the view to a RelativeLayout you must use RelayiveLayout.LayoutParams. So you can have to do the same as you know from xml: add layout rules. See the documentation.
Then overall something like this:
ImageView iv = new ImageView(context);
RelayiveLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
// TODO set the params
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
iv.setLayoutParams(params);
relativeLayout.addView(iv);
To hide the imageView you can user imageView.setVisibility(View.GONE)

put a button on the Relative Layout and move it independently

I have a layout that always rotate to the north. I want to put a pointer on it, but pointer should not rotate. how can I do that?
this is my code of Layout:
rll = (RelativeLayout) findViewById(co.iman.R.id.relativeLayout1);
rlllp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlllp.setMargins(190, 200, 200, 0);
and in other method I rotate the layout with:
rll.setRotation(mCurrentDegree);
I want put a pic or button exactlly in center of this layout? and I want to move this button independently.
Try making another RelativeLayout inside your first one, and do something like this:
rll.setRotation(mCurrentDegree);
rll2.setRotation(-mCurrentDegree);
Now the inside layout should be set to -10 degrees when its parent is set to 10, so it will look like it is not moving at all. Hope that helps!

Displaying Saved Bitmaps in Android (LinearLayout)

I have 2 bitmaps that I saved with view.getDrawingCache(); called firstBitmap and secondBitmap. They were saved from initial drawings made by the user. Now I want to display them. My issue is that only the first picture is displayed by itself (I want both to be shown at the same time). I know that the bitmaps are correct because if I remove layout.addView(pic1), I can see the second image by itself clearly. layout is a LinearLayout.
My suspicion is that the size is an issue but I believe that by default views that are added to the layout have fill parent so this should still result in 2 images not one. I have tried dynamically changing the width and size with LayoutParams but this makes the image disappear entirely. I am open to any suggestions.
ImageView pic1 = new ImageView(this);
pic1.setImageBitmap(firstBitmap);
layout.addView(pic1);
ImageView pic2 = new ImageView(this);
pic2.setImageBitmap(secondBitmap);
layout.addView(pic2);
Try to distribute the weight in your linear layout so that both bitmaps will be visible.
you could use the below code to do that.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height, weight);
layout.addView(pic1, params);
layout.addView(pic2, params);

Adding a margin to ImageView programmatically

I have a table layout where each row is built programmatically.
The structure is basically, for each row:
TextView, LinearLayout, LinearLayout, LinearLayout, Button.
Each of the LinearLayout has multiple ImageViews inside of them. I want to increase the spacing between the ImageView items as they render with their borders touching.
I tried the suggestions here - In Android, how to make space between LinearLayout children? - I create the parameters like so:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent);
And set it like this to an image view when adding the control:
linearLayout1.AddView(image1);
linearLayout1.AddView(image2, layoutParams);
linearLayout1.AddView(image3);
Note I am only setting it on the middle item as a left & right margin on this would be fine for what I am trying to achieve.
The problem is that even without setting any margins on the layout params i.e. just instantiating it and setting it as shown above, it adds about a 35px margin to the left causing a much larger margin than I wanted. Even calling SetMargins with 1px doesn't change the margin.
Where am I going wrong?
set width of linear layout as WrapContent like this
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);

Categories

Resources