I have ImageView. I change margins and size of ImageVew on set new bitmap. For example, user click button, I set bitmap to ImageView and change params (this is code from my own view):
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams();
p.leftMargin = (int) ((pw - lw) / 2f);
_topMargin = (int) ((ph - lh) / 2f);
lp.topMargin = _topMargin;
lp.width = lw;
lp.height = lh;
requestLayout();
All work. But also I want change params, if change size parent view of my ImageView. If I try use code on change params from this event - nothing todo (ImageView don't change params).
How I can fix this issue?
I see now one way: on change parent size, I don't change ImageView params, only set some internal boolean flag. After finish parent changing, I change my ImageView params. But I don't know, what parent event I need watch, after which I can change my ImageView params.
My solution.
I use this method.
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams();
lp.leftMargin = (int) ((pw - lw) / 2f);
_topMargin = (int) ((ph - lh) / 2f);
lp.topMargin = _topMargin;
lp.width = lw;
lp.height = lh;
setLayoutParams(lp);//<---set layout params
requestLayout();
After changing layout params you need to set it back.
Related
I have a programatically created RelativeLayout as shown in code below. The RelativeLayout has same width and height of an EditText and lies above the EditText. I add a programatically created TextView to it with certain width and height.
overlay = new RelativeLayout(context);
overlay.setBackgroundColor(Color.TRANSPARENT); //NO I18N
viewGroup.addView(overlay);
overlay.bringToFront();
RelativeLayout.LayoutParams lp = ((RelativeLayout.LayoutParams) overlay.getLayoutParams());
lp.width = getMeasuredWidth();
lp.height = getMeasuredHeight();
Log.d(TAG, "initOverlay: width " + lp.width + " height " + lp.height);
lp.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.formulaView);
lp.addRule(RelativeLayout.RIGHT_OF, R.id.fxImg);
lp.setMargins(0, ((int) Util.dptopx(context, 1) + getTotalPaddingTop()), 0, ((int) Util.dptopx(context, 1) + getTotalPaddingBottom()));
overlay.setLayoutParams(lp);
setOverScrollMode(OVER_SCROLL_NEVER);
overlay.setOverScrollMode(OVER_SCROLL_NEVER);
overlay.requestLayout();
The first time when the TextView is added, the views are shown with specified width and height. But when i remove the TextView from RelativeLayout and add a new TextView again, the TextView is not being shown. It has zero width and height though i'm setting a width and height. What could possibly be the problem?
TextView
textview = new CustomTextView(edittext.getContext());
Editable editable = edittext.getEditableText();
String spantext = editable.subSequence(startidx, endidx).toString();
float[] location = Util.determineTextViewLocation(startidx, endidx);
textview.setText(spantext);
textview.setTextSize(TypedValue.COMPLEX_UNIT_PX, edittext.getTextSize());
textview.setX(location[0]);
textview.setY(location[1]);
textview.setLayoutParams(new ViewGroup.LayoutParams((int) location[2], (int) location[3]));
textview.setPadding(0, ((int) location[4]), 0, 0);
overlay.addView(textview);
Log.d(TAG, "width "+textview.getWidth()+" height "+textview.getHeight()+" view"+textview);
Edit: The TextView's onMeasure() method is never getting called.
Try below code :
XTextView textView= new XTextView(this);
textView.setLayoutParams(new RelativeLayout.LayoutParams(100,500));
textview.setText("Sample");
textview.setX(location[0]);
textview.setY(location[1]);
textview.setLayoutParams(lp);
overlay.addView(textview);
textview.requestLayout();
Also for overlay don't use getLayoutParams() method. Use
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
I had the same problem. What fixed it for me was to add the textview to its parent first, then post a runnable to the text view with
textView.post(new Runnable() { ... })
and then set the layout params within that runnable
I know this question is super old but just in case anyone else runs into this problem :)
I try change layout height programmatically. I have two buttons. In one button I change my layout position and second button I try to receive save position witch I was first time.
<RelativeLayout
android:id="#+id/popaplayout"
android:layout_width="fill_parent"
android:layout_height="200dp" >
This is a my layout
and in first button I wrote
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
parms.addRule(RelativeLayout.BELOW, R.id.movies_title_layout);
scrollpopap.setLayoutParams(parms);
scrollpopap.setScrollingEnabled(true);
and second button
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, 300);
rel_btn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
scrollpopap.setLayoutParams(rel_btn);
scrollpopap.setScrollingEnabled(false);
In xml file I wrote height 200dp and programmatically 300 and there are different height. How I can wrote save height in programmatically?
if anyone knows solution please help me
thanks
First convert 200 dp into pixels -
final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) (200 * scale + 0.5f);
Then set programatically,
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, pixels);
rel_btn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
scrollpopap.setLayoutParams(rel_btn);
scrollpopap.setScrollingEnabled(false);
Convert DP to PX like this.
final float scale = getContext().getResources().getDisplayMetrics().density;
int px = (int) (100 * scale + 0.5f); // replace 100 with your dimensions
Set Some layout_gravity of relative layout. and Change height and width by java code.
RelativeLayout rl = (RelativeLayout) findViewById(R.id.yourId);
rl.getLayoutParams().height = px;
rl.getLayoutParams().width = px;
If you intending to change RelativeLayout height, then this help.
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.yourId);
relativeLayout.getLayoutParams().height = 100;
relativeLayout.getLayoutParams().width = 100;
Use this
RelativeLayout layout = (RelativeLayout) findViewById(R.id.yourlayout_id);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
params.height = 320;
params.width = 320;
For Kotlin Users who want to set Height & Width from Dimens file, use this:
relativeLayout.layoutParams.width = relativeLayout.context
.resources.getDimensionPixelSize(R.dimen.width)
relativeLayout.layoutParams.height = relativeLayout.context
.resources.getDimensionPixelSize(R.dimen.height)
How can I set an GestureOverlayView's width and height programmatically?
I tried the below code. But I am getting null pointer exception in setting the width and height.
GestureOverlayView gestureOverlayView = new GestureOverlayView(this);
View inflate = getLayoutInflater().inflate(R.layout.main, null);
ImageView imageView = (ImageView) inflate.findViewById(R.id.imageView);
gestureOverlayView.addView(inflate);
gestureOverlayView.getLayoutParams().height = 50;
gestureOverlayView.getLayoutParams().width = 50;
setContentView(gestureOverlayView);
You need to add the view first before you can read out the height and width. Also then you meight get some trouble only after the second or third call of onMeasure(...) the dimensions are final.
If you want to set the width and height by code use this:
ViewGroup.LayoutParams lp = gestureOverlayView.getLayoutParams();
if(lp == null) {
// not yet added to parent
lp = new ViewGroup.LayoutParams(desiredWidth, desiredHeight);
} else {
lp.width = desiredWidth;
lp.height = desiredHeight;
}
gestureOverlayView.setLayoutParams(lp);
yourparent.addView(gestureOverlayView, lp);
Please note that you need to choose also the right LayoutParams implementation. Here I use that one of ViewGroup.
I am resizign LinearLayout from it's original height to 0 with:
ViewGroup.LayoutParams params = getLayoutParams();
params.height = newHeight;
requestLayout();
Everything works except newHeight = 0 - layout's height changes back to its original height. How can I avoid it ?
Setting visibility to GONE if newHeight == 0 does not help.
Try this.....
LinearLayout layout = (LinearLayout)findViewById(R.id.yourLayoutId);
LinearLayout.LayoutParams lp = (LayoutParams) layout.getLayoutParams();
lp.height = 0;
layout = (LinearLayout)findViewById(R.id.linearTop);
layout.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
ViewGroup.LayoutParams params = layout.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
layout.setLayoutParams(params);
Wouldn't you have to set the height of params using setBaseAttributes()? The int height = ... will not have any influence on params...
Are you sure that you get the actual parameters object, not the copy of one?
I would try the following code:
ViewGroup.LayoutParams params = getLayoutParams();
params.height = newHeight;
setLayoutParams(params);
requestLayout();
Try setting a new layout params object then. This should work
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,0);
layout.setLayoutParams(lp);
I try setWidth() and setheight() method but it not work
Try using LayoutParams. For example
button.setLayoutParams (new LayoutParams(50, LayoutParams.WRAP_CONTENT)
For me works perfect this:
ViewGroup.LayoutParams params = myButton.getLayoutParams();
//Button new width
params.width = 400;
myButton.setLayoutParams(params);
I found this to be the best solution because it also allows the button to be repositioned at the same time by simply changing its margin:
int buttonWidth = (int) (screenWidth / 3.0);
RelativeLayout.LayoutParams params = (LayoutParams) myButton.getLayoutParams();
params.width = buttonWidth;
params.leftMargin = buttonWidth;
myButton.setLayoutParams(params);