Linear layout add view not getting full width - android

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

Related

Set Imageview Height And Width Programmatically

for (int i = 0; i < rows; i++) {
TableRow row =new TableRow(this);
View v = new ImageView(getBaseContext());
TextView tv_main =new TextView(this);
ImageView iv = new imageView(this);
tv_main.setText("Test");
iv.setImageDrawable(v.getResources().getDrawable(R.drawable.home_icon));
row.addView(iv);
row.addView(tv_main);
table_layout.addView(row);
}
How to set height and width programmatically? I had tried
iv.getLayoutParams().height=80;
iv.getLayoutParams().width=100;
but image is not display.
You need to create a new LayoutParams object and set the height and width for it and then pass it to the iv.setLayoutParams() method.
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(100, 80);
iv.setLayoutParams(layoutParams);

Android separator for textview like listview

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!

Set alignment of ImageView programmatically

I created a table with five table rows. The first row is the header of each table.
In the following four rows each second column shell represent an image and a textview.
My problem is that my image is displayed in the center of the row.
If I add some layoutparams to the image for their width, it disappears.
I want that the alignment of my picture is left, so its right next to my first column ends.
Creating the rows:
for (int i = 0; i < 4; i++) {
TableRow tableRow = new TableRow(context);
for (int column = 1; column <= 8; column++) {
TextView textView = null;
if (column == 2) {
ImageView imgView = new ImageView(context);
imgView.setLayoutParams(new LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
tableRow.addView(imgView);
textView = new TextView(context);
textView.setGravity(LEFT);
} else {
textView = new TextView(context);
}
textView.setGravity(CENTER);
textView.setTextColor(WHITE);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
Update the table with data:
for (int column = 0; column <= 7; column++) {
View child = tableRow.getChildAt(column);
if (child instanceof ImageView) {
ImageView flag = (ImageView) child;
flag.setImageResource(getFlagByClubName(group.getTeams().get(i).getClub()));
}
if (child instanceof TextView) {
TextView textView = (TextView) tableRow.getChildAt(column);
setContentInColumn(group.getTeams().get(i), column, textView);
}
}
You could try wrapping the imageview and textview in a relative layout. Please note I haven't tested the code below, but it's adapted from some other code of mine that works fine.
RelativeLayout wrapper = new RelativeLayout(context);
// Create imageView params
RelativeLayout.LayoutParams imageParams;
imageParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
// Create imageView
ImageView imageView = new ImageView(context);
imageView.setLayoutParams(imageParams);
imageView.setId(1);
// Create textView params
RelativeLayout.LayoutParams textParams;
textParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
textParams.addRule(RelativeLayout.LEFT_OF, imageView.getId());
// Create textView
TextView textView = new TextView(context);
textView.setLayoutParams(textParams);
// Add to the wrapper
wrapper.addView(imageView);
wrapper.addView(textView);
And then just add wrapper to your table:
tableRow.addView(wrapper);

Add multiple text views programmatically in Android

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

How to place text of an button in center

In my array of buttons i want to place the text of each button to be placed in center.I have use Gravity but it doesnt work. The code i have use is.Please anyone help me to solve this out.
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout rowLayout = null;
LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1);
//Create Button
for (int i = 0; i<6; i++)
{
rowLayout = new LinearLayout(this);
rowLayout.setWeightSum(7);
layoutVertical.addView(rowLayout, param);
for(int j=0; j<7; j++)
{
m_pBtnDay[i][j] = new Button(this);
m_pBtnDay[i][j].setTextSize(12);
m_pBtnDay[i][j].setGravity(Gravity.CENTER);
rowLayout.addView(m_pBtnDay[i][j], param);
m_pBtnDay[i][j].setOnLongClickListener(this);
m_pBtnDay[i][j].setOnClickListener(this);
m_pBtnDay[i][j].setOnTouchListener(this);
//save button position
m_pBtnDay[i][j].setTag(new CalendarForm(i , j));
}
}
try this: m_pBtnDay[i][j].getPaint().setTextAlign(Paint.Align.CENTER);

Categories

Resources