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.
Related
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);
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);
now I am implement a hard code of a custom relative layout, the problem is, when I use parameter of relative layout, it works well. But once I add setX and setY method, they conflict. What I actually want in my code is to dynamically set the view one by one according to the first view's position.
Here when I set the X to 50, get 50 width disappeared in the right side where the mMapView2 keep his original place.
To solve this problem the only method I could do now is to make my code tedious, cos i have many views inside relative layout here.
MapView mMapView1 = new MapView(getActivity());
mMapView1.setX(50);
mMapView1.setId(1);
LayoutParams mLayoutParams1 = new LayoutParams(screenWidth/2, screenHeight/3);
mRelativeLayout.addView(mMapView1, mLayoutParams1);
MapView mMapView2 = new MapView(getActivity());
mMapView2.setId(2);
LayoutParams mLayoutParams2 = new LayoutParams(screenWidth/2, screenHeight/3);
mLayoutParams2.addRule(RelativeLayout.RIGHT_OF,1);
mRelativeLayout.addView(mMapView2, mLayoutParams2);
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);
I have a tableview inside of a scrollview. I want to reserve space for a listview 2/3 of the screen. I am getting the height of the screen, and will divide that in 2/3 and set the height of the ScrollView. But even with manually x and y numbers its blowing up.
App is crashing when I set the ScrollView width and height like this:
ScrollView sv = (ScrollView)findViewById(R.id.usdScroll);
ScrollView.LayoutParams layoutParams = new ScrollView.LayoutParams( -1, 550);
sv.setLayoutParams(layoutParams);
Any ideas what I did wrong?
You can use the layout_weight attribute to specify how much of a view the specific component will use, e.g. android:layout_weight=2. So for your listView set a weight of 1 and a weight of 2 for your tableview, I think. Anyway, play with the weight setting and you can split the view in any ratio you like.
LayoutParams is used to tell their parents how they want to be laid out. So the layout param's type which you set to must match its parent class type exactly.
and here is a better solution, using layout_weight instead. Try this please
<ScrollView android:layout_weight="1" ........></ScrollView>
<ListView android:layout_weight="2" ........></ListView>