reposition textview in constraint layout dynamically - android

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

Related

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.

How can I move the imageView object in the favorite locations on the layout with programming

Consider we have an imageView object like following:
ImageView imageView ;
textView = (TextView) findViewById(R.id.textView);
How can I move the imageView object in the favorite locations on the layout with programming and of course without changing the its height and width that I had configured in the relativeLayout.
For moving around a view inside a layout view, you could use margin layout properties such that margin_left and margin_top represent how much space should be at left and top of that view respectively from its original position.
If you would like your layout view does not restrict how its child view to be laid out, you would need FrameLayout. However some other layout views also would offer margin properties.
For example suppose we would like to move that ImageView by 2px in X direction. The code below, do this.
RelativeLayout.LayoutParams params = textview.getLayoutParams();
params.marginLeft = 2;
textview.requestLayout();

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.

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