LinearLayout larger than screen width is not adding items - android

first of all, App.getInstance().getWidth() gives me screen width.
I am creating this LinearLayout:
mainContainer = new LinearLayout(context);
mainContainer.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
It is horizontal, and it haves two items, this:
adContainer = new RelativeLayout(context);
adContainer.setBackgroundColor(Color.BLACK);
adContainer.setLayoutParams(new LayoutParams(App.getInstance().getWidth(), App.getInstance().getHeight()));
adContainer.setGravity(Gravity.CENTER);
mainContainer.addView(adContainer);
and this:
tabView = new ImageView(context);
tabView.setLayoutParams(new LayoutParams(100,140));
tabView.setImageResource(R.drawable.tab);
mainContainer.addView(tabView);
I try moving the layout with TranslateAnimation, but i see that it does not show the view tabView on the right of adContainer, so i tested putting mainContainer with a startX position of -App.getInstance().getWidth()/2 and i can see half of adContainer but i still can't see tabView
The only way to see tabView is setting adContainer width to less than App.getInstance().getWidth(). It is very strange...
Why i can't see any items added into a layout but in a x position larger than screen width?
Thanks

You have given wrap content in the layout parameters for linear layout, so the linear layout is wrapped around the relative layout and image view. Try using MatchParent.
mainContainer = new LinearLayout(context);
mainContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

Related

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);

LayoutParams Gravity Right not enforcing

I'm extending a LinearLayout to make a compound widget. The widget is set to layout_width="MATCH_PARENT".
mLabelTextView = new TextView(context);
mLabelTextView.setText("Testing");
LayoutParams labelParams = new
LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0.0f);
labelParams.gravity = Gravity.CENTER_VERTICAL;
addView(mLabelTextView, labelParams);
mContentView = new Checkbox(content);
contentParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f);
contentParams.gravity = Gravity.CENTER_VERTICAL | Gravity.Right;
addView(mContentView, contentParams);
My problem is that the Gravity.Right property is taking effect on mContentView, but Gravity.CENTER_VERTICAL is. I know that mContentView is getting stretched to fill the remaining space. I've already tried just getting gravity=right by itself and that didn't work.
I have a similar code path in which I inflate a view that already has right gravity set in XML and that is working. So I must be missing something somewhere.
If your parent LinearLayout has horizontal orientation (which you don't specify, but I'm guessing), then it will ignore all horizontal layout gravity bits. Likewise, if it has vertical orientation, then it would have ignored the CENTER_VERTICAL setting.
One trick is to wrap mContentView in a vertically oriented LinearLayout, give that wrapper a layout weight of 1 (so it fills the remaining space) and then the wrapper will honor the RIGHT gravity.
A better alternative might be to extend RelativeLayout instead of LinearLayout. That gives you 2-D control over gravity (unlike LinearLayout).

Dynamic interface using TableRows

I am developing an app that creates a dynamic interface according to a string set by the user.
The only thing I've got in my XML file, is a ScrollView, the rest is in java code:
ScrollView sv = (ScrollView)findViewById(R.id.sv);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TableLayout tl = new TableLayout(this);
TableRow[] tr = null;
Then I decipher a string, which will tell me how many buttons I need to add. Next to each Button should be a TextView. In a for-loop, I create the Buttons and the TextViews and add them to the TableRow, which I add to the TableLayout, which I add to the LinearLayout which I add to the ScrollView.
My problem is: I want the Buttons to be, say 200dp wide, followed by the TextView, but currently it takes the width from WRAP_CONTENT. Setting the size of the Button with RelativeLayout and setLayoutParams, does not work when I add it to the TableRow.
final float scale = getBaseContext().getResources().getDisplayMetrics().density;
int pixels = (int) (200 * scale + 0.5f);
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rel_btn.height = pixels;
rel_btn.width = pixels;
btn.setLayoutParams(rel_btn);
tr[i].addView(btn);
tr[i].addView(tv);
tl.addView(tr[i]);
Does anyone have an idea on how to fix my problem with the TableLayout?
I've read that nested LinearLayouts might be a different approach, but I cant seem to find any java examples, only XML - and I need it to be dynamic.
What is the reason for using RelativeLayout.LayoutParams when your Button and TextView are the children of a TableRow? It should be like this:
TableRow.LayoutParams rel_btn = new TableRow.LayoutParams(pixels, pixels);
btn.setLayoutParams(rel_btn);
Also, do you really need the LinearLayout that wraps the TableLayout? If you're not using that LinearLayout for something else you should remove it to improve the views hierarchy.

Set EditText to percentage of parents's width in LinearLayout

I have a LinearLayout class in which I have:
TexView | ImageView | EditText | ImageView.
I have the last ImageView all the way to the right side of the LinearLayout its wrapped in. The EditText runs very long and in some case pushes the last ImageView out of view (or so it seems to push it out of view).
I want to to have the EditText set to a percentage of the total width. I tried using weight with LinearLayout parameters but it seems to cause the view to get all out of wack. For example here it is for the EditText:
LinearLayout.LayoutParams lpEt = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
.50f);
All the other views follow suit but have lesser weights (.2, .2, .1) and they all add up to 1.0. But the LinearLayout row is never spaced correctly.
Should I find out the width of the parent (which is a ListView) and then set the width of the EditText explicitly based on the parent's width or is there a better way?
Im not sure what your linear layout orientation is but im guessing its horizontal. if so try this.
LayoutParams textViewParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(textViewParams);
LinearLayout.LayoutParams editTextParams = new LinearLayout.LayoutParams(
0,LayoutParams.WRAP_CONTENT,1f);
editText.setLayoutParams(editTextParams);
LayoutParams imageViewParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(imageViewParams);
This will make the edit text to fill the remaining space between the textview and imageview. If you need something else, just modify the code a bit. When using weights, set the weight or height as 0 for which you are setting weight for.

ImageView and LinearLayout, the imageview it's getting blank spaces on top and bottom of the Image

I'm having some issues with LinearLayout and ImageView on Android. I must not use XML for defining the layouts, i'm trying to understand to do it with java code.
I have two elements on the linearLayout, a header (imageview) and a body (gridview with images). Someting is going wrong because the imageview it's getting some blanck spaces on top and on bottom of the original Image.
The original Imageview that i am using for the Header doesn't have these white spaces on top and on bottom of the image, i put the line header.setBackgroundColor(Color.WHITE); and then i noticed that something was going wrong with the imageview because its getting these blanck spaces.
I tested with multiple header images and i noticed that the higher height haves the image, the bigger are the blanck spaces so, they are generated dinamically for some kind of error/bug
How to remove them?
Here is a capture:
Here is the code:
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
header = new ImageView(getApplicationContext());
header.setImageResource(R.drawable.header_acc_old);
header.setBackgroundColor(Color.WHITE);
myGridView = new GridView(this);
myImageAdapter=new ImageAdapter(this);
myGridView.setAdapter(myImageAdapter);
ll.addView(header);
ll.addView(myGridView);
setContentView(ll);
I Tryed with these solutions, but they didn't work:
ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll.addView(header,layoutParams);
Try using setBackgroundResource instead of setImageResource while setting image to ImageView.

Categories

Resources