Dynamically create Imageviews - android

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.

Related

Display a Linear Layout programmatically in a loop

Hello everyone please i need help. I'm a biginner in Android programming so since i was trying to solve this problem it took me all my days that why i want you to help me please. I'm trying to display a Linear layout according to the loop condition but the issue is that when i'm doing that it's just displaying the last one and i'm not able to see others when it's displaying. Here are my codes
ScrollView sv = new ScrollView(this);
sv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
sv.setPadding(5, 5, 5, 5);
sv.setBackgroundColor(getResources().getColor(R.color.colorWhite));
LinearLayout ll = null;
for (int i=0; i < 2; i++){
try {
//Here iam defining the LinearLayout
ll = new LinearLayout(this);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
ll.setLayoutParams(params);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setBackground(getDrawable(R.drawable.circle_view));
//Here iam defining the RelativeLayout
RelativeLayout Rl = new RelativeLayout(this);
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Rl.setLayoutParams(param);
//Defining a layout params for widgets
RelativeLayout.LayoutParams image = new RelativeLayout.LayoutParams(100, 90);
image.addRule(RelativeLayout.ALIGN_LEFT);
//Creating widgets
ImageView im = new ImageView(this);
im.setImageResource(R.mipmap.airtel);
im.setId(R.id.image1);
im.setLayoutParams(image);
//----------------
RelativeLayout.LayoutParams txtone = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
txtone.addRule(RelativeLayout.RIGHT_OF, R.id.image1);
txtone.setMargins(0, 5, 0, 45);
TextView txtVone = new TextView(this);
txtVone.setText("ArkaL"+i);
//txtVone.setTextSize(R.dimen.MainTextSize);
txtVone.setTextSize(15);
txtVone.setTypeface(txtVone.getTypeface(), Typeface.BOLD);
txtVone.setLayoutParams(txtone);
//------------------------------------------------
RelativeLayout.LayoutParams txttwo = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
txttwo.addRule(RelativeLayout.RIGHT_OF, R.id.image1);
txttwo.setMargins(0, 45, 0, 0);
TextView txtVtwo = new TextView(this);
txtVtwo.setText("République Démocratique du Congo");
txtVtwo.setTypeface(txtVtwo.getTypeface(), Typeface.SERIF.getStyle());
txtVtwo.setTextSize(10);
txtVtwo.setLayoutParams(txttwo);
Rl.addView(im);
Rl.addView(txtVone);
Rl.addView(txtVtwo);
RelativeLayout.LayoutParams recycler = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RecyclerView rv = new RecyclerView(this);
rv.setLayoutParams(recycler);
ll.addView(Rl);
ll.addView(rv);
}catch (Exception e){
}
}
//Here for assigning Linear to ScrollView
sv.addView(ll);
this.setContentView(sv);
Please help me.
your problem is that you created one LinearLayout and changed it in a loop and then added it to a ScrollView so at the end you will have just one LinearLayout. as you may know an ScrollView should have just one View as a child view so you should add a LinearLayout for a ScrollView and add as many LinearLayout as you want to the primary LinearLayout. your code can be like this:
LinearLayout llMain = new LinearLayout(this);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llMain.setLayoutParams(params2);
llMain.setOrientation(LinearLayout.VERTICAL);
sv.addView(llMain);
LinearLayout ll = null;
for (int i = 0; i < 2; i++) {
try {
//Here iam defining the LinearLayout
ll = new LinearLayout(this);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(params);
ll.setOrientation(LinearLayout.VERTICAL);
...
llMain.addView(ll);
} catch (Exception e) {
...
note that the Height of primary linearlayour should wrap its content;

Dynamically create textview in center

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)

Relative Layout dynamic textView

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)

How to overlay text on image programmatically

I have a image placed in the relative layout. How can i place a small image over the background image and two text view side by side next to the image. Any help will be really appreciated. The only thing is I want to do this programmatically. I have tried doing it the following way but for some reason the textview appears on the top left of the screen. I want it appear on the imagebutton.
LinearLayout LinearLayout = new LinearLayout(this);
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams;
Button[] btn = new Button[1];
btn[0] = new Button(this);
btn[0].setId(1);
btn[0].setGravity(Gravity.CENTER);
btn[0].setText("text");
btn[0].setBackgroundResource(R.drawable.button_grey);
btn[0].setTextColor(Color.parseColor("#FFFFFF"));
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT | RelativeLayout.ALIGN_PARENT_TOP);
relativeLayoutParams.setMargins(20, 20, 15, 15);
relativeLayout.addView(btn[0], relativeLayoutParams);
ImageButton[] Imgbtn = new ImageButton[10];
Imgbtn[0] = new ImageButton(this);
Imgbtn[0].setId(2);
Imgbtn[0].setBackgroundResource(R.drawable.box_round_corners);
Imgbtn[0].setMaxHeight(200);
Imgbtn[0].setClickable(true);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.BELOW,btn[0].getId());
relativeLayoutParams.height = 100;
relativeLayoutParams.setMargins(15, 0, 15, 10);
Imgbtn[0].setLayoutParams(relativeLayoutParams);
relativeLayout.addView(Imgbtn[0], relativeLayoutParams);
TextView[] txtview = new TextView[10];
txtview[0] = new TextView(this);
txtview[0].setId(3);
txtview[0].setTextColor(Color.parseColor("#000000"));
txtview[0].setText("text");
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// relativeLayoutParams.addRule(RelativeLayout.ALIGN_LEFT | RelativeLayout.ALIGN_TOP);
relativeLayoutParams.addRule(RelativeLayout.BELOW,Imgbtn[0].getId());
relativeLayoutParams.height = 20;
relativeLayoutParams.setMargins(5, 5, 5, 5);
txtview[0].setLayoutParams(relativeLayoutParams);
relativeLayout.addView(txtview[0], relativeLayoutParams);
android.widget.ScrollView ScrollV = new ScrollView(this);
ScrollV.addView( relativeLayout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
addContentView(ScrollV, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
try to set your views LayoutParams with setLayoutParams() method;
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(width,height);
lp.topMargin=//what is your desired y coordinate
lp.leftMargin=//what is your desired x coordinate
lp.gravity=Gravity.NO_GRAVITY;
view.setLayoutParams(lp);
FrameLayout is better choise for overlaying purposes in my opinion.

set position of layout dynamically

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

Categories

Resources