Add relative layout background - android

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

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);

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);

You can use setBackground for that.
Drawable bg_image= getResources().getDrawable(R.drawable.image);
myLayout.setBackground(bg_image);
Hope it helps.

Related

Making an XML layout programmatically not working

I wanted to re-create my XML layout programatically, but it doesn't seem to work. I found out how to (supposedly) create it programatically from another thread. Any help is appreciated, thanks.
The XML:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:background="#android:color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test1"
android:id="#+id/txt1"
android:paddingTop = "36dp"
android:paddingRight = "36dp"
android:paddingLeft = "18dp"
android:paddingBottom = "36dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test2"
android:layout_toRightOf="#id/txt1"
android:id="#+id/txt2"
android:paddingTop = "36dp"
android:paddingRight = "36dp"
android:paddingBottom = "36dp"/>
</RelativeLayout>
Here is the code I'm attempting to match the XML above:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
// Defining the RelativeLayout layout parameters.
// In this case I want to fill its parent
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
// Creating a new TextView
TextView txt1 = new TextView(this);
txt1.setText("Test1");
TextView txt2 = new TextView(this);
txt2.setText("Test2");
// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.RIGHT_OF, txt1.getId());
txt1.setPadding(18,36,36,36);
txt2.setPadding(0,36,36,36);
// Setting the parameters on the TextView
txt1.setLayoutParams(lp);
txt2.setLayoutParams(lp2);
// Adding the TextView to the RelativeLayout as a child
relativeLayout.addView(txt1);
relativeLayout.addView(txt2);
// Setting the RelativeLayout as our content view
setContentView(relativeLayout, rlp);
}
}
What I see when I run it programmatically

Adding a view between 2 views when one view is aligned to the bottom programmatically

I have a relative layout that contains a scrollview that I want to place between 2 other relative layouts. One of these layouts is aligned to parent bottom. I would like for it to stay at the bottom.
Here's my code:
RelativeLayout.LayoutParams formRenderParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
mainLayout.setLayoutParams(formRenderParams);
formRenderParams.addRule(RelativeLayout.BELOW, pageButtons.getId());
//relative is the relative layout that has been aligned to bottom in previous code
RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams) relative.getLayoutParams();
p.addRule(RelativeLayout.BELOW, r_scroll.getId());
relative.setLayoutParams(p);
mainLayout.addView(r_scroll, formRenderParams);
pageButtons is Layout A, r_scroll is Layout B, and relative is Layout C.
The problem is that C disappears. It either disappears with this code, or B overlaps it. What I want is for B to take up the remaining space, but with no overlapping.
Also here's a picture to show the problem visually:
Any ideas on what's going on? Any help would be appreciated. Thank you.
Try this
RelativeLayout rlmain= (RelativeLayout) findViewById(R.id.rlMain);
RelativeLayout rA=new RelativeLayout(MainActivity.this);
RelativeLayout.LayoutParams layoutA = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rA.setId(1);
rA.setBackgroundColor(getResources().getColor(R.color.blue));
layoutA.addRule(RelativeLayout.ALIGN_PARENT_TOP, rlmain.getId());
rA.setLayoutParams(layoutA);
TextView tvA=new TextView(MainActivity.this);
RelativeLayout.LayoutParams tvParamA = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tvA.setText("This is Layout 1");
tvA.setLayoutParams(tvParamA);
rA.addView(tvA);
RelativeLayout rC=new RelativeLayout(MainActivity.this);
RelativeLayout.LayoutParams layoutC = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutC.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,rlmain.getId());
rC.setId(3);
rC.setLayoutParams(layoutC);
rC.setBackgroundColor(getResources().getColor(R.color.gray));
TextView tvC=new TextView(MainActivity.this);
RelativeLayout.LayoutParams tvParamC = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tvC.setText("This is Layout 3");
tvC.setLayoutParams(tvParamC);
rC.addView(tvC);
RelativeLayout rB=new RelativeLayout(MainActivity.this);
RelativeLayout.LayoutParams layoutB = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutB.addRule(RelativeLayout.BELOW, rA.getId());
layoutB.addRule(RelativeLayout.ABOVE, rC.getId());
rB.setId(2);
rB.setLayoutParams(layoutB);
rB.setBackgroundColor(getResources().getColor(R.color.orange));
ScrollView scroll=new ScrollView(MainActivity.this);
RelativeLayout.LayoutParams scrollB = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
scroll.setLayoutParams(scrollB);
rB.addView(scroll);
rlmain.addView(rA);
rlmain.addView(rB);
rlmain.addView(rC);
rlMain in above code is the RelativeLayout declared in xml file.
Hope this may help you!
just follow the pattern of this layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/a"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RelativeLayout
android:id="#+id/b"
android:layout_below="#+id/a"
android:layout_above="#+id/c"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
</ScrollView>
</RelativeLayout>
<RelativeLayout
android:id="#+id/c"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Just add to RelativeLayout.LayoutParams of formRenderParams this rule:
formRenderParams.addRule(RelativeLayout.ABOVE, relative .getId());
and change
p.addRule(RelativeLayout.BELOW, r_scroll.getId());
to
p.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
to avoid circular dependencies in layout.
Edit
RelativeLayout.LayoutParams formRenderParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
r_scroll.setBackgroundColor(getResources().getColor(android.R.color.black)); // this is just to test the guess

Making RelitiveLayouot view

I am new in android so I spent hours of trying to make this look like this in code. Could someone help on this ?
Here is my try :
RelativeLayout photo = new RelativeLayout(this);
photo.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, densityToPixels(80)));
photo.setGravity(Gravity.CENTER_VERTICAL);
allphotos.addView(photo);
TextView textView1 = new TextView(this);
textView1.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
textView1.setGravity(Gravity.CENTER_VERTICAL);
textView1.setText("IDasdfasdfasdfasdfasdfd");
photo.addView(textView1);
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
ImageView img = new ImageView(this);
img.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
img.setLayoutParams(params);
photo.addView(img);
ImageView del_img = new ImageView(this);
img.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
img.setLayoutParams(params);
photo.addView(del_img);
Why do you do all that in code? Use xml. If you need the view constructed from xml use View.inflate passing your xml to it.
There's really no need to do that in code...
Just use a TextView and add two compound drawables to it:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/txtText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="8dp"
android:textSize="14sp"
android:textColor="#color/white"
android:drawableLeft="#drawable/icon_left"
android:drawableRight="#drawable/icon_rite"
android:drawablePadding="8dp"
/>
</RelativeLayout>
[EDIT]
To set the compounds in code:
1 - You don't need these lines in the TextView definition, anymore:
android:drawableLeft="#drawable/icon_left"
android:drawableRight="#drawable/icon_rite"
2 - You need to set the drawables in code:
// given that you retrieved your TextView as txt and you retrieved your drawables as drwLeft and drwRite
// Parameter order: left, top, right, bottom
txt.setCompoundDrawablesWithIntrinsicBounds(drwLeft, null, drwRite, null);

setBackgroundColor(int color) and RelativeLayout

When i used this XML file...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
..I see that background color wrap content
http://img716.imageshack.us/img716/8225/rlayout.jpg
But if i write this RelativeLayout by code...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layout.setBackgroundColor(Color.parseColor("#FF0000"));
TextView text = new TextView(this);
text.setText("Hello world");
//AƱado texto
layout.addView(text);
layout.setLayoutParams(params);
setContentView(layout);
}
...I see that background color match parent instead of wrap content
http://img404.imageshack.us/img404/1427/rlayoutfull.jpg
Any ideas?
Thanks!
you are never setting the layout params:
layout.setLayoutParams(params);
and I think that you should place the:
TextView text = new TextView(this);
text.setText("Hello world");
and all other view you will add to your layout before you setting the layout params, otherwise there will be no content to wrap-around.
Edit:
what happens when you set:
tried to replace between the two:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
?

Adwhirl align to bottom

I have a screen where i have 2 fullscreen views, and i want an adwhirl ad at the bottom.
Here is my xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:id="#+id/main_layout"
>
</RelativeLayout>
Then i put the views on it from code:
// in activity's onCreate
RelativeLayout layout = (RelativeLayout)findViewById(R.id.main_layout);
v1= new V1(this);
v2= new V2(this, v1);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
v1.setLayoutParams(lp);
v2.setLayoutParams(lp);
layout.addView(v1);
layout.addView(v2);
.....
AdWhirlLayout adWhirlLayout2 = new AdWhirlLayout(this, "key");
adWhirlLayout2.setAdWhirlInterface(this);
adWhirlLayout2.setMaxWidth(width);
adWhirlLayout2.setMaxHeight(height);
RelativeLayout.LayoutParams lp_bottom = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp_bottom.addRule(RelativeLayout.ALIGN_BOTTOM, RelativeLayout.TRUE);
adWhirlLayout2.setLayoutParams(lp_bottom);
layout.addView(adWhirlLayout2);
But the ads are still at the top of the screen, like when i didn't set the layoutparams.
Try :
lp_bottom.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);

Categories

Resources