When I'm setting my layout parameters with xml, it works fine. But when I'm trying to set layout parameters programmatically, it works wrong. where did I failed?
I need to set parameters in my LinearLayout, here it is:
<LinearLayout
android:id="#+id/buttons_layout"
android:layout_width="231dp"
android:layout_height="40dp"
android:layout_marginTop="40dp"
android:layout_marginRight="85dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="horizontal"
android:baselineAligned="true"
android:weightSum="603" >
And here is my code to set params:
rl = (LinearLayout)findViewById(R.id.buttons_layout);
lp = new RelativeLayout.LayoutParams(pixelsFromDP(231), pixelsFromDP(40));
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.setMargins(0, pixelsFromDP(40), pixelsFromDP(85), 0);
rl.setLayoutParams(lp);
Here is what I'm getting by setting parameters by xml:
And here is what I'm getting by setting parameters programmatically:
method pixelsFromDP returns int value:
public int pixelsFromDP(int pixels){
return (int)(40 * getResources().getDisplayMetrics().density + 0.5f);
}
The reason of using this method is that in LayoutParams values are in pixels, but i need values in dp, so in this methos I'm converting dp into pixels depends on screen density
Oh shi, I found a mistake! OMG I'm so stupid :D
Probled solved by replacing "40" by "pixels" in method pixelsFromDP
I think LayoutParams are used in measuring phase, so when you do this programatically, you have to re-measure the layout, i would try to use invalidate(). I'm not 100% sure about this, but i think it's worth a shot.
Normally i would add this just as a comment, but as I don't have enough rep i have to post this as answer.
// Try this It work fine for me. I thinks there is some problem in pixelsFromDP()
LinearLayout rl = (LinearLayout)findViewById(R.id.buttons_layout);
LayoutParams lp = new RelativeLayout.LayoutParams(231, 40);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.setMargins(0, 40, 85, 0);
rl.setBackgroundColor(Color.DKGRAY);
rl.setLayoutParams(lp);
Related
When I set padding on a button using XML it works without problems.
XML for the button:
<Button
android:id="#+id/comment_video"
android:layout_below="#+id/input_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/comment_video"
android:textColor="#android:color/white"
android:backgroundTint="#color/colorAccent"
android:layout_alignParentEnd="true"
android:textSize="14sp"
android:padding="10dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="10dp"
android:visibility="gone"/>
Screenshot of button:
Then when I do it programmatically it's not working.
Code for the button:
// Add reply button
final Button replyButton = new Button(mContext);
replyButton.setId(170000 + i2);
replyButton.setText("REPLY");
replyButton.setTextColor(Color.parseColor("#FFFFFF"));
replyButton.setBackgroundColor(Color.parseColor("#C70000"));
replyButton.setPadding(dp10_in_px, dp10_in_px, dp10_in_px, dp10_in_px);
replyButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
replyButton.setVisibility(View.GONE);
RelativeLayout.LayoutParams lp8 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp8.addRule(RelativeLayout.BELOW, textInputLayout.getId());
lp8.addRule(RelativeLayout.ALIGN_PARENT_END, relativeLayout3.getId());
lp8.setMargins(0, dp4_in_px, dp10_in_px, dp10_in_px);
replyButton.setLayoutParams(lp8);
Screenshot of button:
I set thousands of views dynamically based on YouTube data and must do it programmatically, at least as far as I know.
The formula used for variables in setPadding and setMargin are as follows:
int dp4 = 4; // 4 dps
final float scale = getResources().getDisplayMetrics().density;
int dp4_in_px = (int) (dp4 * scale + 0.5f);
int dp10 = 10; // 10 dps
int dp10_in_px = (int) (dp10 * scale + 0.5f);
Any help is appreciated.
Update 1 - Screenshot of layout bounds
Update 2 - Sharp corners
Another small detail that might be a clue. The corners on the programmatically made button is sharp. Normally they're a bit rounded.
OK, with help from war_Hero in comments I've found the solution.
setBackgroundColor() don't work. It sets the entire background, including the padding, to the same color.
This:
replyButton.setBackgroundColor(Color.parseColor("#C70000"));
Should be replaced with something like this:
replyButton.getBackground().setColorFilter(ContextCompat.getColor(mContext, R.color.colorAccent), PorterDuff.Mode.MULTIPLY);
Try this:
replyButton.setPadding(dp10_in_px, dp10_in_px, dp10_in_px, dp10_in_px);
you should replace line something like this
replyButton.setPadding(0,padding,0,0);
marginBottom not working. It works but the margin is only a few space regardless of how much margin is applied. Why?
Java-code :
LinearLayout onepage= (LinearLayout) findViewById(R.id.onepage);
RelativeLayout bluegreen = new RelativeLayout(this);
p_ll = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
bluegreen.setLayoutParams(p_ll);
//some other views that make up the whole page
//bottom most image
ImageButton migs = new ImageButton(this);
p_rl = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
p_rl.addRule(RelativeLayout.CENTER_HORIZONTAL);
p_rl.setMargins(0, 0, 0, 20);
register.setLayoutParams(p_rl);
register.setImageResource(R.drawable.migs);
register.setPadding(0, 0, 0, 0);
bluegreen.addView(migs);
onepage.addView(bluegreen);
XML:
<LinearLayout
android:id="#+id/onepage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".PageActivity" >
</LinearLayout>
Have you notice that in setMargins method parameters are in pixel and not in dp? take a look at android doc page
So depending of your device screen size but 20 px can be very small.
Don't forget to call requestLayout() to take in account margin changes.
I don't know why scaleType not works for this ImageView. widthGrayBlock is correct, I checked it with black background. ImageView has correct width and height but image resource doesn't fit it. Thank you.
ImageView gray = new ImageView(mParentActivity);
gray.setScaleType(ImageView.ScaleType.FIT_XY);
gray.setAdjustViewBounds(true);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
widthGrayBlock, LayoutParams.WRAP_CONTENT);
params.setMargins(marginLeft, 0, 5, 0);
progressLayout.addView(gray, 1, params);
gray.setImageResource(R.drawable.schedule_bar_gray);
//gray.setBackgroundColor(Color.BLACK);
Try this in your xml
android:scaleType="fitXY"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
it's worked for me when i had a problem.
I have a fragment which is animating from right to left. I want to set it to stop at like 100 units from the left part of the screen. This is what i did so far.
RelativeLayout tempLi;
Display display = MyActivity.context.getWindowManager().getDefaultDisplay();
final int width = display.getWidth();
tempLi.setLayoutParams(new LayoutParams(width-100, LayoutParams.FILL_PARENT));
This line of code is getting the layout 100 units short from the right side. I tried doing -width + 100 , it didn't work.
Any suggestions will be appreciated. TIA
You can set the left-margin to the layout as follows:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layoutParams.setMargins(100, 0, 0, 0);
tempLi.setLayoutParams(layoutParams);
Here's the thing: I want to add some images programmatically. The images should have to have a topMargin of 5dip except for the first image, in a LinearLayout with a vertical orientation manner. below the code segment:
LinearLayout body = (LinearLayout) findViewById(R.id.body);
for (int i = 1; i <= 4; i++) {
ImageView img = new ImageView(this);
MarginLayoutParams lp = new MarginLayoutParams(-2, -2);
img.setImageResource(R.drawable.image);
if (i != 1) {
lp.setMargins(0, 5, 0, 0);
}
img.setLayoutParams(lp);
body.addView(img);
body.requestLayout();
}
By running the program I can see the the 4 images(here) vertically aligned one by one but there is no topMargin(as in the code,5dip). body is the id of the LinearLayout. here's the XML segment to:
<LinearLayout
android:id="#+id/body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#901b0e08"
android:orientation="vertical"
android:paddingLeft="6dp"
android:paddingRight="8dp" >
</LinearLayout>
I cant get what went wrong here.
Thanks.
Try changing your MarginLayoutParams to this:
LayoutParams lp = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
The reason for doing this, is that body is a LinearLayout and thus you would want to use the LinearLayout-specific LayoutParams.
Try to set padding but margin for ImageView. Sometimes it works fine. Your can directly set padding size to ImageView w/o declare LayoutParams.
You probably need to set LayoutParams' gravity property, for example:
lp.gravity = 0; //AXIS_X_SHIFT
That's what works fine for me.