I am trying programmatically align some elements in RelativeLayout but I am getting some problem. All my TextView elements aligned to the top left corner even when I set them deferentially. here is my screen shot:
Here is my code (this is the RelativeLayout):
title = new TextView(context);
date = new TextView(context);
rating = new RatingBar(context);
saleImage = new ImageView(context);
arrowImage = new ImageView(context);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
this.setBackgroundResource(R.drawable.list_selector);
this.setLayoutParams(relativeLayoutParams);
this.setClickable(true);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
saleImage.setLayoutParams(relativeLayoutParams);
saleImage.setImageResource(R.drawable.rihanna);
this.addView(saleImage);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
relativeLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
arrowImage.setImageResource(R.drawable.arrow);
arrowImage.setLayoutParams(relativeLayoutParams);
this.addView(arrowImage);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, saleImage.getId());
title.setLayoutParams(relativeLayoutParams);
title.setTextAppearance(context, android.R.style.TextAppearance_Medium);
title.setText("Sale title");
this.addView(title);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
relativeLayoutParams.setMargins(0, 0, 26, 0);
relativeLayoutParams.addRule(RelativeLayout.LEFT_OF, arrowImage.getId());
date.setTextAppearance(context, android.R.style.TextAppearance_Small);
date.setText("14.01.13 22:00");
this.addView(date);
It seems that the problem is with the RelativeLayout.RIGHT_OF and RelativeLayout.LEFT_OF attributes.
here is the Activity code:
public class MainPage extends Activity {
private LinearLayout test;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_sales);
test = (LinearLayout)findViewById(R.id.salesConteiner);
SaleRow row = new SaleRow(this);
test.addView(row);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_page, menu);
return true;
}
}
Is there something I have missed? Thanks!!!
For the view.getId() function to work you first need to se an id: view.setId(1).
Thanks for the help!!!
Related
I have a Class that used in it below code
this.guageBack= (AbsoluteLayout) findViewById(R.id.gaugeFrame);
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(this.needleWidth,this.needleHeight,this.needleX,this.needleY);
gaugeNeedle.setLayoutParams(params);
AbsoluteLayout deprecated so When I want to use RelativeLayout I have not replcement for
AbsoluteLayout.LayoutParams(this.needleWidth,this.needleHeight,this.needleX,this.needleY);
Because it accept two args like below
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(needleWidth, needleHeight);
What should I do?
Just add this code instead of yours
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(needleWidth, needleHeight);
params.topMargin = needleY;
params.leftMargin = needleX;
Do it like this
public class CodeLayout extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 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.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
// Creating a new TextView
TextView tv = new TextView(this);
tv.setText("Test");
// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
// Setting the parameters on the TextView
tv.setLayoutParams(lp);
// Adding the TextView to the RelativeLayout as a child
relativeLayout.addView(tv);
// Setting the RelativeLayout as our content view
setContentView(relativeLayout, rlp);
}
}
try this code,
AbsoluteLayout absoluteLayout = //get absolute layout
Button button = new Button();
AbsoluteLayout.LayoutParms params = absoluteLayout.generateDefaultLayoutParams();
params.x = 100;
params.y = 100;
absoluteLayout.addView(button, params);
I'm trying to set LayoutParams.BELOW programmatically. Here my code:
RelativeLayout layout = new RelativeLayout(this.getActivity());
layout.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
layout.setBackgroundColor(Color.parseColor("#CCCDCDCD"));
ImageView imageView = new ImageView(this.getActivity());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
imageView.setLayoutParams(params);
imageView.setBackgroundResource(R.drawable.create_template);
AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
if (frameAnimation != null) {
frameAnimation.start();
}
TextView textView = new TextView(this.getActivity());
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, imageView.getId());
textView.setLayoutParams(params);
textView.setText("Generating information...");
layout.addView(imageView);
layout.addView(textView);
return layout;
But it's not being correctly positioned. Any idea on why it is not well positioned?
The ImageView doesn't have an ID. You need to give it one. Either define some constant integer value, or use View.generateViewId(), and call setId() before using it in the layout params:
ImageView imageView = new ImageView(this.getActivity());
imageView.setId(View.generateViewId());
...
params.addRule(RelativeLayout.BELOW, imageView.getId());
I am using this code to get the contents from a source and hence using dynamic initialisation.
The problem is that the views made by the createcomment(), overlap each other.
What can be done so that overlapping does not occur??
createdynamic
void createdymanic()
{
rl= new RelativeLayout(getActivity());
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams ivprofile = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textname = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textdate = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textbody = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams imagelike = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams imagecomment = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams imageshare = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams editcomment = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// set picture of the person
ivprofilepic= new ImageView(getActivity());
ivprofile.addRule(RelativeLayout.ALIGN_PARENT_TOP);
ivprofile.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
ivprofile.setMargins(5,5,0,0);
ivprofilepic.setId(1);
ivprofilepic.setImageResource(R.drawable.ic_launcher);
tvname= new TextView(getActivity());
textname.addRule(RelativeLayout.ALIGN_PARENT_TOP);
textname.addRule(RelativeLayout.RIGHT_OF, ivprofilepic.getId());
textname.setMargins(8, 5, 0, 0);
tvname.setId(2);
tvname.setText("Name");
tvname.setTextColor(getResources().getColor(R.color.textcolor));
tvname.setTextSize(18);
tvdate= new TextView(getActivity());
textdate.addRule(RelativeLayout.ALIGN_BOTTOM, ivprofilepic.getId());
textdate.addRule(RelativeLayout.ALIGN_LEFT, tvname.getId());
textdate.addRule(RelativeLayout.BELOW, tvname.getId());
tvdate.setId(3);
tvname.setText("Date");
tvcontent= new TextView(getActivity());
textbody.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
textbody.addRule(RelativeLayout.BELOW, ivprofilepic.getId());
textbody.setMargins(5,0,0,0);
tvcontent.setId(4);
tvcontent.setText("Content");
iblike= new Button(getActivity());
imagelike.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
imagelike.addRule(RelativeLayout.BELOW, tvcontent.getId());
imagelike.setMargins(5, 7, 0, 0);
iblike.setId(5);
iblike.setText("like");
ibcomment= new Button(getActivity());
imagecomment.addRule(RelativeLayout.ALIGN_TOP, iblike.getId());
imagecomment.addRule(RelativeLayout.CENTER_HORIZONTAL);
ibcomment.setId(6);
ibcomment.setText("comment");
ibshare= new Button(getActivity());
imageshare.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imageshare.addRule(RelativeLayout.ALIGN_TOP, iblike.getId());
imageshare.setMargins(0,0,0,5);
ibshare.setId(7);
ibshare.setText("share");
etcommentbody= new EditText(getActivity());
editcomment.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
editcomment.addRule(RelativeLayout.BELOW, iblike.getId());
editcomment.setMargins(5,8,0,0);
etcommentbody.setId(8);
etcommentbody.setHint("Add a comment...");
rl.addView(ivprofilepic, ivprofile);
rl.addView(tvname, textname);
rl.addView(tvdate ,textdate);
rl.addView(tvcontent, textbody);
rl.addView(iblike, imagelike);
rl.addView(ibcomment, imagecomment);
rl.addView(ibshare, imageshare);
rl.addView(etcommentbody, editcomment);
mainlayout.addView(rl, 0, rlp);
tvname.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//profile pe le jaana h yaha se
//profile class banegi
}
});
iblike.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Caller().execute("3", api, id);
}
});
ibcomment.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String a=etcommentbody.getText().toString();
new Caller().execute("4", api, id,a);
}
});
ibshare.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// h.share(user_id, post_id);
}
});
}
createcomments
void createcomment()
{
RelativeLayout clayout= new RelativeLayout(getActivity());
RelativeLayout.LayoutParams clp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
//clp.addRule(RelativeLayout.BELOW, etcommentbody.getId());
clp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
RelativeLayout.LayoutParams ivcommentprofile = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textcommentname = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textcommentdate = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textcommentbody = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
ivcommentprofilepic= new ImageView(getActivity());
ivcommentprofile.addRule(RelativeLayout.ALIGN_PARENT_TOP);
ivcommentprofile.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
ivcommentprofile.setMargins(5,5,0,0);
ivcommentprofilepic.setId(101);
ivcommentprofilepic.setImageResource(R.drawable.ic_launcher);
tvcommentname= new TextView(getActivity());
textcommentname.addRule(RelativeLayout.ALIGN_PARENT_TOP);
textcommentname.addRule(RelativeLayout.RIGHT_OF, ivcommentprofilepic.getId());
textcommentname.setMargins(5,5,0,0);
tvcommentname.setId(102);
tvcommentname.setText("Name");
tvcommentname.setTextColor(getResources().getColor(R.color.textcolor));
tvcommentname.setTextSize(18);
tvcommentdate= new TextView(getActivity());
textcommentdate.addRule(RelativeLayout.ALIGN_BOTTOM, ivcommentprofilepic.getId());
textcommentdate.addRule(RelativeLayout.RIGHT_OF, ivcommentprofilepic.getId());
textcommentdate.addRule(RelativeLayout.BELOW, tvcommentname.getId());
textcommentdate.setMargins(5, 3, 0, 0);
tvcommentdate.setId(3);
tvcommentdate.setText("Date");
tvcommentcontent= new TextView(getActivity());
textcommentbody.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
textcommentbody.addRule(RelativeLayout.BELOW, ivcommentprofilepic.getId());
textcommentbody.setMargins(5,0,0,0);
tvcommentcontent.setId(4);
tvcommentcontent.setText("Content");
clayout.addView(ivcommentprofilepic, ivcommentprofile);
clayout.addView(tvcommentname, textcommentname);
clayout.addView(tvcommentdate ,textcommentdate);
clayout.addView(tvcommentcontent, textcommentbody);
rl.addView(clayout, clp);
}
I use the following statements
for(ka=0;ka<jsarray.length();ka++){
createdymanic(ka);
// setting on click listener and all
for(int i=0;i<comment_array.length();i++){
createcomment(i);
// detting onclicklistener and all
}
}
It looks like your last text view you're adding (tvcommentcontent) doesn't have a right bound, so your third (tvcommentdate) and your 4th view (tvcommentcontent) you're adding are overlapping. Try adding
textcommentbody.addRule(RelativeLayout.LEFT_OF, tvcommentdate.getId());
right above your
textcommentbody.setMargins(5,0,0,0);
line. This should make sure the last view stayes on the left of the third view. (I made a sweet diagram with MS paint to show this better but apparently new users can't add images :) )
I hope this helps!
Sorry for creating such a lame question.
The problem was that the default value of commentlayout's orientation is horizontal by default.
The following code solved the problem :
commentlayout= new LinearLayout(getActivity());
commentlayout.setOrientation(LinearLayout.VERTICAL);
When I create Activity and set new RelativeLayout right in onCreate(), all works fine.
But I think it's more convenient to create a class MView that extends RelativeLayout and do all the settings there. And if I do this, Activity stays empty. What should I change in MView (or shall I need to use RelativeLayout without extension)?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout mLayout = new RelativeLayout(this);//Change this to MView mLayout=new MView(this);
//I've been trying to move text from here...
TextView tv = new TextView(this);
tv.setText("TestText");
tv.setTextAppearance(this, android.R.attr.textAppearanceSmall);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
tv.setLayoutParams(lp);
mLayout.addView(tv);
//... to here to MView class that extends RelativeLayout
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
setContentView(mLayout, rlp);
}
So, code for MView constructor is:
public MView(Context context) {
super(context);
TextView tv = new TextView(context);
tv.setText("TestText");
tv.setTextAppearance(context, android.R.attr.textAppearanceLarge);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
tv.setLayoutParams(lp);
this.addView(tv);
}
And onCreate becomes this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MView mLayout = new MView(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
setContentView(mLayout, rlp);
}
It was VERY simple. When I created MView, my implementation of onLayout was empty. Obviously, I had to write
super.onLayout(arg0, arg1, arg2, arg3, arg4);
And it worked fine. Thanks for helping :)
It is better to set layout parmas to direct view rather than when you add another layout.
In your case, set wrong add rules for text now.
Corrected code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout mLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
mLayout.setLayoutParams(rlp);
TextView tv = new TextView(this);
tv.setText("TestText");
tv.setTextAppearance(this, android.R.attr.textAppearanceSmall);
RelativeLayout.LayoutParams rlt = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlt.addRule(RelativeLayout.CENTER_IN_PARENT);
tv.setLayoutParams(rlt);
mLayout.addView(tv);
setContentView(mLayout);
}
I am trying to create a method where each time I click a button, I want the screen to display a Relative Layout box below one that is currently there or create one at the top if there isn't one there and I want it to keep creating them below from each button click. Currently it just displays the Relative Layout box at the top of the screen as it would expect to do.
Here is the code that I have so far and can someone please help me see what I am doing wrong and what I can do to fix this issue:
onCreate code:
Resources r = this.getResources();
dpMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, r.getDisplayMetrics()); //Changes pixels to dp of margins
dpContainerHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, r.getDisplayMetrics()); //Changes pixels to dp of container height
dpContainerPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, r.getDisplayMetrics()); //Changes pixels to dp of container padding
layout = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams textParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textParams2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textParams3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textParams4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams progressParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams imageButtonParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams newLayoutButtonParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams containerParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
RelativeLayout.LayoutParams containerParams2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
layout.setPadding(dpMargin, dpMargin, dpMargin, dpMargin);
//Declaring new views
tv1 = new TextView(this);
tv2 = new TextView(this);
containerLayout1 = new RelativeLayout(this);
containerLayout2 = new RelativeLayout(this);
tv3 = new TextView(this);
tv4 = new TextView(this);
pb1 = new ProgressBar(this);
ib1 = new ImageButton(this);
newLayoutButton = new Button(this);
//Declaring views ID's
tv1.setId(1);
tv2.setId(2);
containerLayout1.setId(3);
containerLayout2.setId(4);
tv3.setId(5);
tv4.setId(6);
pb1.setId(7);
ib1.setId(8);
newLayoutButton.setId(9);
//Text view 1
textParams1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
textParams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
tv1.setText("TextView1");
//Text view 2
textParams2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
textParams2.addRule(RelativeLayout.BELOW, tv1.getId());
textParams2.setMargins(0, 0, 0, dpMargin);
tv2.setText("TextView2");
//Container 1
containerParams1.addRule(RelativeLayout.ALIGN_LEFT, tv2.getId());
containerParams1.addRule(RelativeLayout.ALIGN_RIGHT, tv1.getId());
containerParams1.addRule(RelativeLayout.BELOW, tv2.getId());
containerParams1.setMargins(0, 0, 0, dpMargin);
containerLayout1.setPadding(dpContainerPadding, 0, dpContainerPadding, dpContainerPadding);
containerLayout1.setBackgroundResource(R.color.display_panels);
//Container 2
containerParams2.addRule(RelativeLayout.ALIGN_LEFT, containerLayout1.getId());
containerParams2.addRule(RelativeLayout.ALIGN_RIGHT, containerLayout1.getId());
containerParams2.addRule(RelativeLayout.BELOW, containerLayout1.getId());
containerParams2.setMargins(0, 0, 0, dpMargin);
containerLayout2.setBackgroundResource(R.color.display_panels);
//Text view 3
textParams3.addRule(RelativeLayout.ABOVE, tv4.getId());
textParams3.addRule(RelativeLayout.ALIGN_LEFT, pb1.getId());
tv3.setText("TextView3");
tv3.setTextAppearance(this, android.R.style.TextAppearance_Small); //Need to change text colour to white
tv3.setTextColor(getResources().getColor(R.color.text_colour));
//Text view 4
textParams4.addRule(RelativeLayout.ABOVE, pb1.getId());
textParams4.addRule(RelativeLayout.CENTER_HORIZONTAL);
tv4.setText("TextView4");
tv4.setTextAppearance(this, android.R.style.TextAppearance_Small); //Need to change text colour to white
tv4.setTextColor(getResources().getColor(R.color.text_colour));
//Progress bar 1
progressParams1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
progressParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
progressParams1.addRule(RelativeLayout.LEFT_OF, ib1.getId());
pb1.setProgress(40);
pb1.setMax(100);
//Image button 1
imageButtonParams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imageButtonParams1.addRule(RelativeLayout.BELOW, tv4.getId());
ib1.setBackgroundResource(R.color.display_panels);
ib1.setImageResource(R.drawable.ic_green_ok);
newLayoutButtonParams.addRule(RelativeLayout.ABOVE, tv2.getId());
newLayoutButton.setText("New Layout");
newLayoutButton.setOnClickListener(this);
layout.addView(tv1, textParams1);
layout.addView(tv2, textParams2);
layout.addView(newLayoutButton, newLayoutButtonParams);
containerLayout1.addView(tv3, textParams3);
containerLayout1.addView(tv4, textParams4);
containerLayout1.addView(pb1, progressParams1);
containerLayout1.addView(ib1, imageButtonParams1);
layout.addView(containerLayout1, containerParams1);
layout.addView(containerLayout2, containerParams2);
setContentView(layout, layoutParams);
createNewLayout method code:
public RelativeLayout createNewLayout(RelativeLayout newlayout){
newLayoutContainer = new RelativeLayout(this);
final RelativeLayout.LayoutParams newLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
newLayoutContainer.setLayoutParams(newLayoutParams);
newLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
newLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
newLayoutParams.addRule(RelativeLayout.BELOW);
newLayoutParams.setMargins(0, 0, 0, dpMargin);
newLayoutContainer.setBackgroundResource(R.color.display_panels);
return newLayoutContainer;
}
onClick code:
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==newLayoutButton){
layout.addView(createNewLayout(newLayoutContainer), 0);
}
}
EDIT
new method code:
public void createNewLayout(){
int currentId = 1;
for(int i = 1; i <= numberOfLayouts; i++){
newLayoutContainer = new RelativeLayout(this);
newLayoutContainer.setId(currentId);
if(currentId == 1){
newLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
newLayoutParams.addRule(RelativeLayout.ALIGN_LEFT, containerLayout2.getId());
newLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT, containerLayout2.getId());
newLayoutParams.addRule(RelativeLayout.BELOW, containerLayout2.getId());
newLayoutParams.setMargins(0, 0, 0, dpMargin);
newLayoutContainer.setBackgroundResource(R.color.display_panels);
}
else{
newLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
newLayoutParams.addRule(RelativeLayout.ALIGN_LEFT, newLayoutContainer.getId());
newLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT, newLayoutContainer.getId());
newLayoutParams.addRule(RelativeLayout.BELOW, currentId);
newLayoutParams.setMargins(0, 0, 0, dpMargin);
newLayoutContainer.setBackgroundResource(R.color.display_panels);
}
newLayoutContainer.setLayoutParams(newLayoutParams);
layout.addView(newLayoutContainer);
currentId++;
}
}
You could use a global variable to store currentId assigned to layouts, and update it every time u add a layout.
int currentId = 10;
button click function
public void onClick(View v) {
if(v==newLayoutButton){
layout.addView(createNewLayout(newLayoutContainer,currentId), 0);
currentId++;
}
}
createNewLayout function will have one more attribute as currentId.
public RelativeLayout createNewLayout(RelativeLayout newlayout, int currentId){
newLayoutContainer = new RelativeLayout(this);
final RelativeLayout.LayoutParams newLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
newLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
newLayoutParams.addRule(RelativeLayout.BELOW, currentId);
newLayoutParams.setMargins(0, 0, 0, dpMargin);
newLayoutContainer.setLayoutParams(newLayoutParams);
newLayoutContainer.setBackgroundResource(R.color.display_panels);
return newLayoutContainer;
}
Try this working code:
int currentId = 5;
#Override
public void onClick(View v) {
if(v.getId()==9){
containerLayout2.addView(createNewLayout(currentId), 0);
currentId +=10;
}
}
createNewLayoutFunction
public RelativeLayout createNewLayout(int currentId){
RelativeLayout newLayoutContainer = new RelativeLayout(this);
final RelativeLayout.LayoutParams newLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
newLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
newLayoutParams.addRule(RelativeLayout.BELOW, currentId);
newLayoutParams.setMargins(0, 0, 0, dpMargin);
newLayoutContainer.setLayoutParams(newLayoutParams);
RelativeLayout.LayoutParams image = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView x = new ImageView(this);
x.setImageResource(android.R.drawable.ic_media_play);
newLayoutContainer.addView(x, image);
newLayoutContainer.setId(currentId+10);
newLayoutContainer.setBackgroundResource(R.color.display_panels);
return newLayoutContainer;
}
also change this line in containerParams2
RelativeLayout.LayoutParams containerParams2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight+330);
this should work fine. working in my code.