Reputation: 4652
I am having a relative layout I want to create the layout dynamically the layout in XMl lookes like
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5sp"
android:background="@drawable/step_background"
android:orientation="vertical" >
I made this like -
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
lprams.setMargins(5,5,5,5);
But how to add the background I am doing this in a Fragment not in a activity
Upvotes: 0
Views: 59
Reputation: 28823
You can use setBackground
for that.
Drawable bg_image= getResources().getDrawable(R.drawable.image);
myLayout.setBackground(bg_image);
Hope it helps.
Upvotes: 0
Reputation: 2735
Try This : -
RelativeLayout mRelativeLayoutPopup = new RelativeLayout(context);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(5,5,5,5);
mRelativeLayoutPopup.setLayoutParams(layoutParams);
mRelativeLayoutPopup.setBackgroundResource(drawable_background);
Upvotes: 0
Reputation: 9700
try as follows...
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
lprams.setMargins(5,5,5,5);
layout.setLayoutParams(lprams);
layout.setBackgroundResource(R.drawable.step_background);
Upvotes: 1