Adding a margin to ImageView programmatically - android

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

Related

reposition textview in constraint layout dynamically

I'm building an android app that does 3 variations of a similar calculation.
I have an imageview, three radiobuttons, and four textviews inside the imageview that display inputs/results. I have editext for actual input and a button to do the calculation. That is not my problem.
I can load the appropriate image by checking appropriate radiobutton As each image is slightly different I'd like to reposition the the textview with each image so they line up with the image better.
During layout design I loaded the appropriate, positioned the textviews where I want them and copied down the Horizontal and Vertical bias numbers.
Can't figure out how to change the bias to move the textview when I change the image.
I'm using constraint layout.
Thanks Steve
The ConstraintLayout.LayoutParams class has two fields: horizontalBias and verticalBias. You can therefore update the bias values for any view that is a child of a ConstraintLayout like this:
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) view.getLayoutParams();
params.verticalBias = 0.3f;
view.setLayoutParams(params);

addRule in RelativeLayout.LayoutParams is not working

I have this code:
final RelativeLayout headerRl=new RelativeLayout(SpeakersActivity.this);
headerRl.setBackgroundColor(Color.parseColor(almacen.getColor()));
final TextView initial=new TextView(SpeakersActivity.this);
final RelativeLayout.LayoutParams headerParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 80);
headerParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
initial.setText(arrayInitials.get(i));
initial.setTextColor(getColor(R.color.white));
initial.setTextSize(16);
initial.setPadding(10,0,0,0);
runOnUiThread(new Runnable() {
#Override
public void run() {
headerRl.addView(initial,headerParams);
ll.addView(headerRl);
}
});
"ll" is a LinearLayout. I am adding the rule on a RelativeLayout.LayoutParams, I am adding the view to a RelativeLayout, which is added to a LinearLayout. But my view is not centered vertically. I tried to center it horizontally to see if it was working, but it isn't. Why is my view not centered in the RL? How to center it?
Thank you.
Depending on what result you are expecting
There are 2 ways to deal with your problem.
First one:
The issue is in the headerRl which is set by default as wrap_content, wrap_content
So, what you need is to define it's layout params with match_parent:
headerRlLayoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
headerRl.setLayoutParams(headerRlLayoutParams)
The first parameter could be wrap_content, depends on what you want, but the second one should be match_parent, otherwise, it will be wrap_content by default which will position your view at the top
Create headerParams as headerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT...
And that's it.
Match parent here causes your view to take the whole parent's width. If that's what you want, then see the second option below
Second one:
If you want your view as match_parent and take the whole view, then
your issue is:
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
Which cause your text view to be match_parent in the layout. And because you didn't set view's gravity - text has default one - top/start.
And the solution is simple - Set TextView Gravity (which same as setting gravity param in the XML) - the view will stay as match_parent - taking the whole width of its parent's layout but the text will be centered vertically
initial.setGravity(Gravity.CENTER_VERTICAL);
And a small hint - You also can set in developer options "show layout bounds" to see how much space your layout takes and how views are positioned them self in the layout. This could help to debug and understand what exactly is going wrong
initial.setGravity(Gravity.CENTER_VERTICAL);
Try with
final RelativeLayout.LayoutParams headerParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 80);
/**
* Rule that centers the child vertically with respect to the
* bounds of its RelativeLayout parent.
*/
headerParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
// your textview
initial.setGravity(Gravity.CENTER_VERTICAL);

making textView wrap its text vertically

I used this code:
Rect bounds=new Rect();
tv.getPaint().getTextBounds(text,0, text.length(),bounds);
float textWidth=bounds.width();
float textHeight=bounds.height();
and it works well horizontally...
also vertical size is ok...
but the text is painted in a position lower than what I expect...
these links was not useful:
get text height
auto scale textview text to fit within bounds
what should I change?
view and its text
Just set your layout params to WRAP_CONTENT for the height. If your text is in a LinearLayout it would look like this:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.WRAP_CONTENT, LinearLayout.WRAP_CONTENT);
myTextView.setLayoutParams(params);
If you're using a XML layout just set your TextView's layout_height to wrap_content. If the text is still painted lower than you expect check whats the padding set to the TextView.

Center view in android after changing its width properties

I'm changing the width of a grid view based on its column width and number of columns (which works):
gridView.setLayoutParams(new LinearLayout.LayoutParams(gridManipulation.getColumnWidth() * (int)Math.sqrt(str.length) + 10, LayoutParams.FILL_PARENT));
gridView.setGravity(Gravity.CENTER);
I've tried re-centering it in the parent view but its not working, does anyone know why, its still centred as if it still has the width parameters before my dynamic change.
Set the Gravity as LayoutParams property then set that LayoutParams to your GridView as follows...
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(gridManipulation.getColumnWidth() * (int)Math.sqrt(str.length) + 10, LayoutParams.FILL_PARENT);
params.gravity = Gravity.CENTER;
gridView.setLayoutParams(params);
you cant center it if its bigger than its parent, which is never bigger than the screen, unless you set it manually, which will cause the system to render data outside the screen and slow you way down.....
is it expanding verticaly or horizontally or both? my recomendation would be to put it in a scrollview.

How to set chart size in achartengine

I have an activity that has a LinearLayout and I put my chart into this:
...
mChart = ChartFactory.getCubeLineChartView(this, mDataset, mRenderer, 0.3f);
graphActivityLayout.addView(mChart, 1);
This fills up my whole layout. How can I specify the dimensions of the chart? I haven't found any ways to do it.
My onResume method check if the mChart is null and if not, then calls repaint on it, otherwise sets up the chart data and does the above snippet. Nothing that is related to my question.
As far as I know, the chart view has layout dimensions set to "FILL_PARENT" as default. This in general eats up all the space of a linear layout and hides the other views in the same layout.
If you have just one other view in the layout (I guess at position 0 from your code snippet), which has the layout-width/height set to "WRAP_CONTENT", it is sufficient to set an arbitrary non zero weight for the chart view. This tells the linear layout to use the remaining space, rather than to use all space:
graphActivityLayout.addView(mChart, 1);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mChart.getLayoutParams();
layoutParams.weight = 1;
I added a new LinearLayout that wraps my chart and I set its height to the desired one.

Categories

Resources