I want to create 4 RelativeLayout dynamically so that each subsequent layout is placed below the previous one. I'm trying to do this whit this piece of code:
RelativeLayout layoutParent = (RelativeLayout)findViewById(R.id.layoutParent);
int layouts = 4;
int dp15 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(dp15, dp15, dp15, dp15);
for (int l = 0; l <= layouts; l++)
{
RelativeLayout queueLayout = new RelativeLayout(getApplicationContext());
TextView one = new TextView(getApplicationContext());
one.setText(String.valueOf(l));
queueLayout.setId(2000 + l);
if (l != 0) params.addRule(RelativeLayout.BELOW, queueLayout.getId() - 1);
queueLayout.addView(one, params);
layoutParent.addView(queueLayout);
}
But I can't get the desired position of each layout. Can someone tell me how can I do what I wanna do?
Thank you in advance!
You set the BELLOW rule but never use it when you add the child RelativeLayout to the parent layout(as MisterSquonk said). Also use another set of LayoutParams for the child RelativeLayout:
for (int l = 0; l <= layouts; l++) {
RelativeLayout queueLayout = new RelativeLayout(getApplicationContext());
TextView one = new TextView(getApplicationContext());
one.setText(String.valueOf(l));
queueLayout.setId(2000 + l);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (l != 0) lp.addRule(RelativeLayout.BELOW, queueLayout.getId() - 1);
queueLayout.addView(one, params);
layoutParent.addView(queueLayout, lp);
}
Related
I have to create Dynamically list of Imageview in a row, There is an other Imageview (dot Drawable) under Each Imageview.
see below pic which is my desired view.
Here is my source.
for (int i = 0; i < 9; i++) {
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView catImg = new ImageView(getApplicationContext());
catImg.setImageResource(bottDrawableArr[i]);
catImg.setId(View.generateViewId());
int dp = (int) utiles.convertDpToPixel(27, getApplicationContext());
params1.height = dp;
params1.width = dp;
ImageView dotImg = new ImageView(getApplicationContext());
dotImg.setImageResource(R.mipmap.dot);
dotImg.setId(View.generateViewId());
params2.addRule(RelativeLayout.BELOW, dotImg.getId());
dockBg.addView(catImg, params1);
dockBg.addView(dotImg, params2);
}
But I am Unable to get my desire view,
while my views show like this
I tried to put dot under each image but unable to achieve that.
The best way is working with some LinearLayout, something like this:
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < 9; i++) {
LinearLayout layoutChild = new LinearLayout(this);
layoutChild.setOrientation(LinearLayout.VERTICAL);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView catImg = new ImageView(getApplicationContext());
catImg.setImageResource(bottDrawableArr[i]);
catImg.setId(View.generateViewId());
int dp = (int) utiles.convertDpToPixel(27, getApplicationContext());
params1.height = dp;
params1.width = dp;
ImageView dotImg = new ImageView(getApplicationContext());
dotImg.setImageResource(R.mipmap.dot);
dotImg.setId(View.generateViewId());
params2.addRule(RelativeLayout.BELOW, dotImg.getId());
layoutChild.addView(catImg, params1);
layoutChild.addView(dotImg, params2);
layout.addView(layoutChild);
}
dockBg.addView(layout);
Note: it's better to delete the unnecessary properties like setting some rules...
params2.addRule(RelativeLayout.BELOW, dotImg.getId());
This line of code looks like you are trying to add a rule to put the dotImg below the dotImg. Replacing that with catImg.getId() should fix your issue.
It might be a good idea to consider a RecyclerView though, Or if it is a static array (always the same items) then you could make a new layout file and just inflate it when you need it.
I add two Checkboxes dynamically to a Linearlayout. Then those Linearlayouts are added one after another in a Relativelayout. The weights of the checkboxes are set so that each take 50% of the Linearlayout width. Now, if their heights do not match, the bottom of the checkbox with bigger height disappears. How to solve this? Here's a screenshot:
And the code:
LinearLayout ll;
LinearLayout.LayoutParams lp;
CheckBox ch;
int id = 1200, i, j;
for (i = 0, j = 0; i < selections.size() - 1; i += 2, j += 2) {
ll = new LinearLayout(NotificationSettings.this);
lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ch = new CheckBox(NotificationSettings.this);
lp.weight = 1.0f;
ch.setLayoutParams(lp);
ch.setText(selections.get(i));
ch.setChecked(isSelected);
ch.setTextColor(color);
ch.setId(j);
ll.addView(ch);
ch = new CheckBox(NotificationSettings.this);
ch.setLayoutParams(lp);
ch.setText(selections.get(i + 1));
ch.setChecked(isSelected);
ch.setTextColor(color);
ch.setId(j + 1);
ll.addView(ch);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
if (id == 1200)
p.addRule(RelativeLayout.BELOW, addBelow);
else
p.addRule(RelativeLayout.BELOW, id);
ll.setLayoutParams(p);
ll.setId(++id);
rl.addView(ll);
}
Edit:
When both checkboxes have multiple lines:
Can you make sure that the Linear Layout's height below it is not too large that it covers the above Linear Layout?
Or try changing your Relative Layout Params' height to WRAP_CONTENT
Change
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
To
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
I have a list of items and a relative layout. I want to add as many relative layouts as the no of items in list and textview in each on them.
My problem is, I am not able to position the textview in the centre of the layout.
Below in my code
for(int i = 0; i < Names.size(); i++)
{
textView = new TextView(this);
textView.setText(Names.get(i));
rt=new RelativeLayout(this);
rt.setBackgroundResource(R.drawable.mednamedash);
int curTextViewId = prevTextViewId + 1;
int curRLId = prevRLId + 1;
textView.setId(curTextViewId);
rt.setId(curRLId);
final RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, prevTextViewId);
rt.setLayoutParams(params);
//textView.setLayoutParams(params);
rt.addView(textView);
textView.setGravity(Gravity.CENTER);
prevTextViewId = curTextViewId;
prevRLId=curRLId;
noon.addView(rt, params);
}
Here rt is the dynamiclly created RelativeLayout and textView is the dynamically created TextView... noon is the RelativeLayout I am adding everything into.
Try this:
final RelativeLayout.LayoutParams tvParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
tvParams.addRule(RelativeLayout.CENTER_IN_PARENT);
rt.addView(textView, tvParams);
Try with textView.setGravity(Gravity.CENTER); before its addition to the rt (RelativeLayout)
I'm trying to dynamically build relative layouts consisting of an image and a textview at the moment. I have tried building a loop to place the next relative layout below the former one, but I can't really make it work. The end result should be something like this, but I guess that if i figure out how to align below the former one, I can also figure out how to place it right_of the former relative layout.
Any suggestions? This is my code so far:
RelativeLayout container1 = (RelativeLayout) rootView
.findViewById(R.id.main_layout);
for (int i = 0; i < pictures.size(); i++) {
RelativeLayout tile = new RelativeLayout(getActivity());
tile.setId(i);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
params.height = height / 3;
params.width = width / 2;
tile.setLayoutParams(params);
ImageButton ibGood = new ImageButton(getActivity());
ibGood.setId(1);
ibGood.setImageResource(pictures.get(0));
ibGood.setAdjustViewBounds(true);
ibGood.setMaxHeight(height / 3 / 5 * 4);
ibGood.setMaxWidth(width);
ibGood.setScaleType(ScaleType.CENTER_CROP);
ibGood.setPadding(5, 5, 5, 5);
tile.addView(ibGood);
RelativeLayout.LayoutParams tvPriceParam = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
tvPriceParam.addRule(RelativeLayout.BELOW, ibGood.getId());
tvPriceParam.addRule(RelativeLayout.ALIGN_LEFT, ibGood.getId());
TextView tvPrice = new TextView(getActivity());
tvPrice.setId(2);
tvPrice.setHeight(tile.getHeight() / 5);
tvPrice.setWidth(tile.getWidth() / 5 * 2);
tvPrice.setPadding(5, 0, 0, 5);
tvPrice.setText(Integer.toString(price.get(0)));
tile.addView(tvPrice, tvPriceParam);
if (counter == 0) {
container1.addView(tile);
} else if (counter % 2 == 0) {
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.BELOW, -1);
tile.setLayoutParams(lay);
container1.addView(tile);
}
counter += 1;
}
you can make it with the custom gridview !! why you are doing this with the relative layout ?
here is the link which you can use to achieve what do you want..
http://www.learn-android-easily.com/2013/09/android-custom-gridview-example.html
http://www.caveofprogramming.com/uncategorized/custom-gridview-with-imageview-and-textview-in-android/
please visit 3 links are there you will get that you want .. with relative layout ..
Thanks,
Madhav
RelativeLayout layout = (RelativeLayout)findViewById(R.id.rl);
for(int i = 1; i <= count; i++)
{
final TextView textViewUtente = new TextView(mContext);
final TextView textViewMessaggio = new TextView(mContext);
final TextView textViewData = new TextView(mContext);
textViewUtente.setText(sepUser[i]);
textViewMessaggio.setText(sepMessage[i]);
textViewData.setText(sepDate[i]);
int curTextViewIdUtente = 1000+i;
int curTextViewIdMessaggio = 2000+i;
int curTextViewIdData = 3000+i;
textViewUtente.setId(curTextViewIdUtente);
textViewMessaggio.setId(curTextViewIdMessaggio);
textViewData.setId(curTextViewIdData);
final RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.topMargin= 100+i*150;
textViewUtente.setLayoutParams(params);
layout.addView(textViewUtente, params);
params.topMargin= 250+i*150;
textViewMessaggio.setLayoutParams(params);
layout.addView(textViewMessaggio, params);
params.topMargin= 100+i*150;
params.rightMargin=200;
textViewData.setLayoutParams(params);
layout.addView(textViewData, params);
}
I need to create 3 textview (that create the post on the blog from the user) every for loop like this:
Post 1:
TextViewUSer TextViewDate
TextViewMessage
Post 2:
TextViewUSer TextViewDate
TextViewMessage
I'm able to make one blog's post(a group of text view) above the other.
I just can position them, like this, from eachother. they stuck one above the other.
I also trued with addRules the .ABOVE and others but it's even worst cause they stuck all on the top left corner
You can add Rules to your textviews like this
tv1 = new TextView(this);
tv1.setId((int)System.currentTimeMillis());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.BELOW, recent.getId());
tv1.setText("Time: "+System.currentTimeMillis());
rr.addView(tv1, lp);
Egs.
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
lp.addRule(RelativeLayout.BELOW, someOtherView.getId())
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT)