for (int i = 0; i < tel.size(); i++) {
LayoutInflater inflater = getLayoutInflater();
View vi = inflater.inflate(R.layout.telefones, null);
TextView tv = (TextView) vi.findViewById(R.id.telefone);
tv.setText(tel.get(i).getNumero());
l1.addView(vi);
ImageView divider = new ImageView(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 5);
lp.setMargins(10, 10, 10, 10);
divider.setLayoutParams(lp);
divider.setBackgroundColor(Color.RED);
l1.addView(divider);
}
That code is giving me the following results:
But the below image is what I want to achieve:
How can I delete the separator for the last item?
for (int i = 0; i < tel.size(); i++) {
LayoutInflater inflater = getLayoutInflater();
View vi = inflater.inflate(R.layout.telefones, null);
TextView tv = (TextView) vi.findViewById(R.id.telefone);
tv.setText(tel.get(i).getNumero());
l1.addView(vi);
ImageView divider = new ImageView(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 5);
lp.setMargins(10, 10, 10, 10);
divider.setLayoutParams(lp);
if((i+1)<tel.size){
divider.setBackgroundColor(Color.RED);
}else{
divider.setBackgroundColor(Color.TRANSPARENT);
}
l1.addView(divider);
}
Try this one!
Related
I have LinearLayout in that I have RadioGroup in RadioGroup I am adding layout dynamically using addview() method of LinearLayout. I am able to add view but my view not getting full width.
here is my code
radiogroup_ans.setOrientation(LinearLayout.VERTICAL);
radiogroup_ans.removeAllViews();
radiogroup_ans.clearCheck();
for (int i = 0; i < subjectDetailMain.getSubjectdetail().get(0).getPackageDetailArrayList().size(); i++)
{
LinearLayout lnr=new LinearLayout(this);
RadioButton rb_answer = new RadioButton(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
rb_answer.setLayoutParams(p);
rb_answer.setId(i);
rb_answer.setTag(i);
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.packagelist_layout,(ViewGroup) null);
p = new LinearLayout.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT,1.0f);
v.setLayoutParams(p);
lnr.addView(rb_answer);
lnr.addView(v);
radiogroup_ans.addView(lnr);
}
here is output hows its looks
image
here is image how i want it
image2
LinearLayout lnr=new LinearLayout(this);
LayoutParams LParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
lnr.setLayoutParams(LParams);
Set Layout params to lnr also
Use this code. I hope it will help
radiogroup_ans.setOrientation(LinearLayout.VERTICAL);
radiogroup_ans.removeAllViews();
radiogroup_ans.clearCheck();
for (int i = 0; i < subjectDetailMain.getSubjectdetail().get(0).getPackageDetailArrayList().size(); i++)
{
LinearLayout lnr=new LinearLayout(this);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT); //add this line
lnr.setLayoutParams(parms); //add this line
RadioButton rb_answer = new RadioButton(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
rb_answer.setLayoutParams(p);
rb_answer.setId(i);
rb_answer.setTag(i);
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.packagelist_layout,(ViewGroup) null);
p = new LinearLayout.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT,1.0f);
v.setLayoutParams(p);
lnr.addView(rb_answer);
lnr.addView(v);
radiogroup_ans.addView(lnr);
}
I have to create like flip cart main page, so I need use more than one horizontal view how to create that programmatically in android. In that horizontal view inside Vertical orientation using images, price, product name will display in android.
Thank in Advance
I just created Like this as mention below code:
public void shopimages()
{
/* GradientDrawable drawable = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,new int[] {R.color.shadow,Color.WHITE,R.color.shadow});
drawable.setShape(GradientDrawable.RECTANGLE);*/
// drawable.setStroke(3, Color.BLACK);
//drawable.setCornerRadius(8);
// drawable.setColor(0xFFFFFFFF); //white background
// border.setStroke(1, 0xFF000000);
// categories = getResources().getStringArray(R.array.categories);
/* child layout of horizontal*/
LinearLayout[] lLayout = new LinearLayout[8];
for(int j=0;j<lLayout.length;j++ ) {
lLayout[j] = new LinearLayout(getActivity());
LinearLayout li=new LinearLayout(getActivity());
li.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
li.setOrientation(LinearLayout.VERTICAL);
/* Heading text for each horizontal images*/
name=new TextView(getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0,150,0,0);
name.setLayoutParams(params);
name.setBackgroundColor(Color.parseColor("#FFFFFF"));
name.setText("kani"+j);
name.setPadding(10,10,10,10);
name.setTextColor(Color.parseColor("#ff1319ff"));
li.addView(name);
li.setTop(50);
root.addView(li);
/*end*/
/* child layout created dynamically*/
// lLayout[j].setBackgroundDrawable(drawable);
sv = new HorizontalScrollView(getActivity());
sv.setHorizontalScrollBarEnabled(false);
lLayout[j].setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
lLayout[j].setOrientation(LinearLayout.HORIZONTAL);
sv.addView(lLayout[j]);
/*end*/
t = new TextView[10];
names=new TextView[10];
price=new TextView[10];
existingprice=new TextView[10];
img = new ImageView[10];
/*create the layout for textveiw and image view*/
LinearLayout.LayoutParams image=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
image.topMargin=50;
image.leftMargin=15;
image.rightMargin=15;
image.bottomMargin=10;
image.width=200;
image.height=150;
LinearLayout.LayoutParams dim = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
dim.topMargin=20;
dim.leftMargin=15;
dim.rightMargin=15;
dim.bottomMargin=10;
dim.width=200;
dim.height=80;
LinearLayout.LayoutParams dim2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
dim2.leftMargin=40;
dim2.width=200;
LinearLayout.LayoutParams dim3 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
/*end*/
/*using for loop to run the rexxtview*/
for (int i = 0; i < 10; i++) {
LinearLayout imageVertical =new LinearLayout(getActivity());
imageVertical.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
imageVertical.setOrientation(LinearLayout.VERTICAL);
img[i] = new ImageView(getActivity());
img[i].setPadding(8, 8, 8, 8);
img[i].setScaleType(ImageView.ScaleType.CENTER_CROP);
img[i].setImageResource(mThumbIds[i]);
t[i] = new TextView(getActivity());
img[i].setLayoutParams(image);
t[i].setLayoutParams(dim);
t[i].setGravity(Gravity.CENTER);
t[i].setTextSize(14);
t[i].setTextColor(Color.parseColor("#ff1319ff"));
t[i].setText("YOHOHO: " + i);
// name
names[i] = new TextView(getActivity());
names[i].setLayoutParams(dim2);
names[i].setGravity(Gravity.CENTER);
names[i].setTextSize(14);
names[i].setTextColor(Color.parseColor("#ff1319ff"));
/*names[i].setEllipsize (TextUtils.TruncateAt.END);*/
names[i].setText("lightweight" + i);
//horizontal linear layout
LinearLayout pricelayout=new LinearLayout(getActivity());
pricelayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
pricelayout.setOrientation(LinearLayout.HORIZONTAL);
//price
price[i] = new TextView(getActivity());
// price[i].setLayoutParams(dim3);
price[i].setGravity(Gravity.CENTER);
price[i].setTextSize(9);
price[i].setTextColor(Color.parseColor("#ff1319ff"));
price[i].setText("Rs.2000");
// existing price
existingprice[i] = new TextView(getActivity());
/*existingprice[i].setLayoutParams(dim3);*/
existingprice[i].setGravity(Gravity.CENTER);
existingprice[i].setTextSize(9);
existingprice[i].setTextColor(Color.parseColor("#ff1319ff"));
existingprice[i].setPaintFlags(existingprice[i].getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
existingprice[i].setText("Rs.5340");
pricelayout.addView(price[i]);
pricelayout.addView(existingprice[i]);
imageVertical.addView(img[i]);
imageVertical.addView(t[i]);
imageVertical.addView(names[i]);
imageVertical.addView(pricelayout);
// imageVertical.addView(existingprice[i]);
lLayout[j].addView(imageVertical);
}
/*end*/
/* parent layout we can add the child view */
root.addView(sv);
}
At first i have a method to create view(yes i know its wrong, but it works fine for me):
public ImageView createPhotoField(final Context context,
LinearLayout reviewsLayout, Integer id, String comment, OnClickListener a, OnClickListener b, int tag) {
/* reviewLayout */
LinearLayout reviewLayout = new LinearLayout(context);
reviewLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, densityToPixels(132,
context)));
reviewLayout.setOrientation(LinearLayout.VERTICAL);
reviewLayout.setGravity(Gravity.CENTER_VERTICAL);
/* reviewEntryTopLayout */
LinearLayout reviewEntryTopLayout = new LinearLayout(context);
reviewEntryTopLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, densityToPixels(65,
context)));
reviewEntryTopLayout.setOrientation(LinearLayout.HORIZONTAL);
reviewEntryTopLayout.setGravity(Gravity.CENTER_VERTICAL);
/* reviewsLayout */
reviewsLayout.addView(reviewLayout);
/* entryNumber */
ImageView imageview = new ImageView(context);
LinearLayout.LayoutParams entryNumberParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
imageview.setTag(tag);
entryNumberParams.setMargins(40, 0, 0, 0);
imageview.setLayoutParams(entryNumberParams);
imageview.setBackgroundResource(R.drawable.ic_bad);
imageview.setImageResource(R.drawable.ic_bad);
//entryNumber.set
imageview.setVisibility(View.GONE);
reviewLayout.addView(reviewEntryTopLayout);
//reviewEntryTopLayout.addView(entryNumber);
/* table layout */
TableLayout tl = new TableLayout(context);
tl.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT, 1.0f));
tl.setPadding(densityToPixels(20, context), 0, 0, 0);
reviewEntryTopLayout.addView(tl);
/* tableRow1 */
TableRow tableRow1 = new TableRow(context);
tableRow1.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tl.addView(tableRow1);
/* textView1 */
TextView comment_photo = new TextView(context);
comment_photo.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
comment_photo.setText(comment);
tableRow1.addView(imageview);
tableRow1.addView(comment_photo);
/* lightSeperatorLayout */
LinearLayout lightSeperatorLayout = new LinearLayout(context);
lightSeperatorLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, densityToPixels(1,
context)));
lightSeperatorLayout.setOrientation(LinearLayout.VERTICAL);
lightSeperatorLayout.setBackgroundColor(Color.parseColor("#d5d5d5"));
/* reviewEntryBottomLayout */
LinearLayout reviewEntryBottomLayout = new LinearLayout(context);
reviewEntryBottomLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, densityToPixels(65,
context)));
reviewEntryBottomLayout.setOrientation(LinearLayout.VERTICAL);
reviewEntryBottomLayout.setGravity(Gravity.CENTER_VERTICAL);
reviewLayout.addView(reviewEntryBottomLayout);
/* tableRow3 */
TableRow tableRow3 = new TableRow(context);
tableRow3.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tableRow3.setPadding(densityToPixels(10, context),
densityToPixels(10, context), densityToPixels(10, context),
densityToPixels(10, context));
reviewEntryBottomLayout.addView(tableRow3);
/* editButton */
Button editButton = new Button(context);
TableRow.LayoutParams editButtonParams = new TableRow.LayoutParams(
Tabl
eRow.LayoutParams.WRAP_CONTENT, 150);
editButtonParams.weight = 0.5f;
editButtonParams.setMargins(15, 0, 30, 0);
editButton.setLayoutParams(editButtonParams);
editButton.setText("Take a photo");
tableRow3.addView(editButton);
/* reviewButton */
Button reviewButton = new Button(context);
TableRow.LayoutParams reviewButtonParams = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT, 150);
reviewButtonParams.weight = 0.5f;
reviewButtonParams.setMargins(30, 0, 15, 0);
reviewButton.setLayoutParams(reviewButtonParams);
reviewButton.setText("Gallery");
tableRow3.addView(reviewButto
n);
reviewLayout.addView(lightSeperatorLayout);
editButton.setOnClickListener(a);
reviewButton.setOnClickListener(b);
return imageview;
}
long story short: this method creates: an imageview, for a photo, a text field, and two buttons, to choose u take a photo, or u take it from gallery.
After calling this method below few times the view is created
image = element.createPhotoField(this, reviewsLayout,
Integer.parseInt(element_id), input_name, a, b,tag);
the problem is: i can't find a way to place a photo in a right place. I have heard about tags, but i dont know how to implement those. Could somebody more experienced give me a hand ?
Maybe i should use adapter for this ??
Use something like this in your adapter is required to achieve what you need :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=convertView;
ViewHolder holder;
if(convertView==null){
view = inflater.inflate(R.layout.youListItem, null);
holder = new ViewHolder();
holder.image=(ImageView)view.findViewById(R.id.youImageView);
view.setTag(holder);
}
else
holder=(ViewHolder)view.getTag();
holder.image.setTag(data[position]);
//to display image
holder.image.setImageDrawable(YourImageDrawable);
return view;
}
public static class ViewHolder{
//view elements of single list item
public ImageView image;
}
Dynamic linearlayout weight is not setting properly, refer the following coding
LinearLayout.LayoutParams mainparams = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT,
1.0f);
mainparams.setMargins(0, 10, 0, 0);
LinearLayout li = new LinearLayout(Appointmentdetails.this);
li.setOrientation(LinearLayout.HORIZONTAL);
li.setLayoutParams(mainparams);
LinearLayout text = new LinearLayout(Appointmentdetails.this);
text.setWeightSum(1);
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
2.0f);
text.setLayoutParams(lparams);
tv = new TextView(this);
tv.setTextSize(18);
tv.setTypeface(MyriadPro);
tv.setLayoutParams(lparams);
tv.setText(Alldata.question.get(i).getgQuestion());
tv.setSingleLine();
tv.setPadding(25, 0, 0, 0);
tv.setMarqueeRepeatLimit(10);
tv.setEllipsize(TruncateAt.MARQUEE);
tv.setSelected(true);
text.addView(tv);
li.addView(text);
LinearLayout Spi = new LinearLayout(Appointmentdetails.this);
Spi.setWeightSum(1);
LinearLayout.LayoutParams lparams1 = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
1f);
Spi.setLayoutParams(lparams1);
RadioGroup rg = new RadioGroup(Appointmentdetails.this);
// rg.setLayoutParams(lparams1);
gAnswerList.add("Select");
for (int j = 0; j < helptext.split(Expressions).length; j++) {
RadioButton rb = new RadioButton(Appointmentdetails.this);
rb.setText(helptext.split(Expressions)[j].toString());
rb.setId(j);
rb.setLayoutParams(lparams1);
rg.addView(rb);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup rg, int checkedId) {
for (int i = 0; i < rg.getChildCount(); i++) {
RadioButton rb = (RadioButton) rg.getChildAt(i);
if (rb.getId() == checkedId) {
CharSequence text = rb.getText();
mSelectedRadioValue = text + ",";
gAnswerList.set(i2, text.toString());
// do something with text
return;
}
}
}
});
Spi.addView(rg);
li.addView(Spi);
mQuestionLayout.addView(li);
}
}
This is how i have added a radio button inside the dynamic linear layout, i dont know whats wrong in my coding its not properly aligned, kindly suggest me to get the solution. Thanks in advance.
I would attempt setting the layout params to center. Your pretty much just giving them free flow and setting wherever they like with WRAP_CONTENTS. Yes, they will not fill the entire screen which is not what you want, but try passing something like Try doing something like this
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
2.0f, lparams.Gravity.CENTER);
Try this for RadioButton and text alignment
LinearLayout.LayoutParams mainparams = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT
);
LinearLayout li = new LinearLayout(Appointmentdetails.this);
li.setOrientation(LinearLayout.HORIZONTAL);
li.setLayoutParams(mainparams);
li.setWeightSum(2);
LinearLayout.LayoutParams mainparams1 = new LinearLayout.LayoutParams(
0, LayoutParams.WRAP_CONTENT
);
mainparams1.weight = 1;
TextView tv = new TextView(this);
tv.setTextSize(18);
tv.setLayoutParams(mainparams1);
tv.setText("text ");
RadioGroup rg = new RadioGroup(Appointmentdetails.this);
rg.setLayoutParams(mainparams1);
rg.setOrientation(RadioGroup.VERTICAL);
for (int j = 0; j < helptext.split(Expressions).length; j++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT
);
RadioButton rb = new RadioButton(Appointmentdetails.this);
rb.setText(helptext.split(Expressions)[j].toString());
rb.setId(j);
rb.setLayoutParams(params );
rg.addView(rb);
}
li.addView(tv);
li.addView(rg);
Here I have to add text view programmatically based on array list size. Text views should be appear in row like continues pattern...
eg. tv1, tv2, tv3 and so on till the size of array list.
But here I am getting text views which are appearing on each other. I can't read the text on them. Here is my code:
ArrayList<String> languageNames = new ArrayList<String>();
RelativeLayout rl = (RelativeLayout)findViewById(R.id.rl);
if(languageNames.size()>0)
{
int size = languageNames.size();
TextView[] tv = new TextView[size];
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, tvLocation.getId());
for(int i=0; i<size; i++)
{
tv[i] = new TextView(getBaseContext());
tv[i].setText(languageNames.get(i).toString());
tv[i].setLayoutParams(p);
tv[i].setPadding(50, 50, 0, 0);
tv[i].setTextColor(Color.parseColor("#000000"));
rl.addView(tv[i]);
}
}
else
{
}
what needs to be done so that I can get text views in appropriate manner?
Add buttons inside a LinearLayout and add this LinearLayout in the RelativeLayout.
RelativeLayout r1 = (RelativeLayout) findViewById(R.id.r1);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, tvLocation.getId());
LinearLayout LL = new LinearLayout(getBaseContext());
LL.setOrientation(LinearLayout.VERTICAL);
for (int i=0;i< size;i++) {
tv[i] = new TextView(getBaseContext());
tv[i].setText(languageNames.get(i).toString());
tv[i].setPadding(50, 50, 0, 0);
tv[i].setTextColor(Color.parseColor("#000000"));
LL.addView(tv);
}
r1.addview(LL, p);
Try this code:
LinearLayout rl = (LinearLayout)findViewById(R.id.mainLayout);
TextView[] tv = new TextView[10];
for(int i=0; i<10; i++)
{
tv[i] = new TextView(getBaseContext());
tv[i].setText("TextView "+ i);
tv[i].setPadding(50, 50, 0, 0);
tv[i].setTextColor(Color.parseColor("#000000"));
rl.addView(tv[i]);
}
Hope this will help you