I've been stuck on this for quite some time now. The problem is that I have a linearlayout consisting of multiple textViews and an imageView. However, the imageview has a smaller width than the textviews. I tried everything i could find, but I couldn't find it. I am able to set the image to the left or right by using gravity, but when i use fill, it just centers and keeps the small width. The only thing i can think of is that the image is downloaded in a different thread. Can the width only be set after the image is downloaded?
// Download photo
this.post_body.setPhoto(url);
// Add imageview to linear layout
this.ll.addView((View) this.post_body.photo);
LinearLayout.LayoutParams layoutParams;
layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.FILL_HORIZONTAL;
this.post_body.photo.setLayoutParams(layoutParams);
Just try to set width=0 and set weight 1 for all the textview and buttons, imageviews whatever you want.
Just try like this here 1f is weight.
layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
Related
I have a textview view whose width doesn't cover all the parent's width. I have used valueTV.setGravity(Gravity.CENTER_HORIZONTAL);
and it puts the text in center of textview.
Now I want to take the whole textview in the center of the parent ( which apparently covers the whole scrren). How can it be done programatically. I want to align the textview to center.Basically I know a lot of questions have already been asked which are quite similar to this but not exactly the same as they didnot solve my problem.
// Make sure textview width match parent
// Use param: ViewGroup.LayoutParams.MATCH_PARENT
LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// Make sure text is center
layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
linearLayout.addView(valueTV, layoutParams);
Hmmm... Try this perhaps
TextView tv = findViewById(R.id.yourTextView);
LinearLayout.LayoutParams layoutParams =
(LinearLayout.LayoutParams)tv.getLayoutParams();
layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
tv.setLayoutParams(layoutParams);
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.
I am currently doing an android application that contains a popup window named as mypopup. It contains a Image button and four textviews. i want to align the popup window dynamically with margin top,margin bottom,margin left and margin right parameters..In my code setmargin method is not working..please anybody help me to do this...
It depends that which layout you are using. Below example places a RelativeLayout in a LinearLayout.
LinearLayout linearLayoutParent;
RelativeLayout relativeLayout;
RelativeLayout.LayoutParams margin = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
margin.setMargins(0, 0, 0, 7); //7px bottom margin
//create the linear and the relative layouts
//...add other stuff here...
// Add view with its margins
linearLayoutParent.addView(relativeLayout, margin);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0,0, 5, 0);
I have textview for which i want to use weight as well as width, but i want to pass width as 0dp, if i set width in layout params as 0, it does not render on screen.
How can i set width as 0dp dynamically through code
I even tried giving width as LayoutParams.Wrap_Content, but that also hasn't worked
LinearLayout.LayoutParams tv_params_level = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0.1f);
If you set the width of the TextView via xml to 0dp you must set then its weight to 1 or other value greater than 0. To set the width programatically:
LinearLayout.LayoutParams tv_params_level = new LinearLayout.LayoutParams(0,
LayoutParams.WRAP_CONTENT, 1.0);
Then set this layout parameteres to your TextView:
tv.setLayoutParams(tv_params_level);
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.