i wanted to add textview and edittext params by code.
here is my code
private void draw_table() {
// TODO Auto-generated method stub
TableLayout ll = (TableLayout) findViewById(R.id.input_table_2);
for (int i = 0; i < 2; i++) {
TableRow row = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
// setting textVIEW
textview = new TextView(this);
textview.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
//textview.setPadding(0, 0, 5, 10);
//textview.setTextAppearance(this,
// android.R.style.TextAppearance_Medium);
textview.setText("Hello");
// setting editText
edittext = new EditText(this);
edittext.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
//edittext.setEms(10);
//edittext.setHint("gpa");
//edittext.setInputType(InputType.TYPE_CLASS_NUMBER);
row.addView(textview);
row.addView(edittext);
ll.addView(row, i);
}
}
but nothing appear on the screen. whats wrong in this code ?thanks in advance :)
Use TableRow.LayoutParams to set your widgets LayoutParams. Like this
textview.setLayoutParams(new TableRow.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
also see how add and delete rows to table layout in java programically
Related
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);
How can I do the following:
Create a row in a TableLayout programmatically
Create three cells in the row
Add arbitrary text in each cell
Thanks for your help!
Try this..
TableLayout tableLayout = new TableLayout(this);
tableLayout.setStretchAllColumns(true);
tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT));
tableLayout.setWeightSum(3);
for (int i = 0; i < 3; i++) {
TableRow tableRow = new TableRow(this);
tableRow.setGravity(Gravity.CENTER);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, 1.0f));
for (int j = 0; j < 3; j++) {
TextView button = new TextView(this);
final int buttonNumber = (j + i * 4);
button.setText("" + buttonNumber);
button.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));
tableRow.addView(button);
}
tableLayout.addView(tableRow);
}
main_lay.addView(tableLayout);
TableLayout table = (TableLayout) findViewById(R.id.TableName);
TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView cell1= new TextView(getApplicationContext());
cell1.setText("one");
cell1.setTextColor(Color.BLACK);
cell1.setTextSize(16f);
cell1.setTypeface(tf);
TextView cell2= new TextView(getApplicationContext());
cell2.setText("two");
cell2.setTextColor(Color.BLACK);
cell2.setTextSize(16f);
cell2.setTypeface(tf);
TextView cell3= new TextView(getApplicationContext());
cell3.setText("three");
cell3.setTextColor(Color.BLACK);
cell3.setTextSize(16f);
cell3.setTypeface(tf);
tableRow.addView(cell1);
tableRow.addView(cell2);
tableRow.addView(cell3);
talbe.addView(tableRow , new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
If you want you can use a function like this to update your tableLayout which adds a new row with some text.
private void addRowToTableLayout(TableLayout tableLayout, String text){
TableRow tr = new TableRow(MyActivity.this);
//if you want you can customize the table row
tr.setBackgroundColor(getResources().getColor(R.color.backgroundColor));
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
TextView textView = new TextView(MyActivity.this);
//if you want you can customize the textview also
textView.setText(text);
tr.addView(textView);
tableLayout.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}
Hey in my code I have a table which is being populated dynamically by a method that I call. The table row when added has an invisible view which I would like to display for each particular row when its clicked and all the others to remain hidden here is my code
public void createTable(String itemName,String itemQuantity,String itemPrice,String itemAmount){
final TableRow tr1 = new TableRow(getApplicationContext());
tr1.setId(id);
tr1.setClickable(true);
tr1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(int i = 0;i<lytTableData.getChildCount();i++){
tr1.getChildAt(4).setVisibility(View.INVISIBLE);
}
tr1.getChildAt(4).setVisibility(View.VISIBLE);
}
});
tr1.setLayoutParams(new LayoutParams( LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
EditText textViewItemQuantity;
TextView textViewItemName,textViewItemPrice,textViewItemAmount;
textViewItemName= new TextView(this);textViewItemQuantity= new EditText(this);textViewItemPrice= new TextView(this);textViewItemAmount= new TextView(this);
iv2 = new ImageView(this);
textViewItemName.setWidth(500);textViewItemQuantity.setWidth(100);textViewItemPrice.setWidth(100);textViewItemAmount.setWidth(100);
textViewItemName.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize), LayoutParams.WRAP_CONTENT, 6f));
textViewItemQuantity.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
textViewItemPrice.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
textViewItemAmount.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
textViewItemName.setText(itemName);textViewItemQuantity.setText(itemQuantity);textViewItemPrice.setText(itemPrice);textViewItemAmount.setText(itemAmount);
textViewItemQuantity.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large); textViewItemPrice.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large);
textViewItemName.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large); textViewItemAmount.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large);
textViewItemName.setTextColor(0xFF121212);textViewItemQuantity.setTextColor(0xFF121212);textViewItemPrice.setTextColor(0xFF121212);textViewItemAmount.setTextColor(0xFF121212);
textViewItemName.setPadding(0, 15, 5, 0);textViewItemQuantity.setPadding(0, 15, 5, 0);textViewItemPrice.setPadding(0, 15, 5, 0);textViewItemAmount.setPadding(0, 15, 5, 0);
textViewItemQuantity.setBackgroundColor(android.R.color.transparent);
iv2.setLayoutParams(new TableRow.LayoutParams((int) LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
iv2.setImageResource(R.drawable.delete);
iv2.setVisibility(View.INVISIBLE);
textViewItemQuantity.setOnClickListener(editTextRows);
tr1.addView(textViewItemName);tr1.addView(textViewItemQuantity);tr1.addView(textViewItemPrice);tr1.addView(textViewItemAmount);tr1.addView(iv2);
lytTableData.addView(tr1, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
Double intItemAmount = Double.parseDouble(itemAmount);
intTotalAmount = intTotalAmount + intItemAmount;
intTotalAmount2 = intTotalAmount2 + intItemAmount;
txtTotal.setText(String.valueOf(intTotalAmount));
//txtDiscount.setText("0");
txtAmount.setText(String.valueOf(intTotalAmount));
id = id + 1;
System.out.println(tr1.getId());
}
using this method I'm able to create a new row every time I call it
what could I be missing to make it work just to be clear what is not working is the displaying of the Invisible View it displays but not the way i want it. I want it to display on the row clicked and all the rest to remain invisible and when another row is clicked it is displayed and all the rest remain invisible
You can do this by defining your dynamic layout (which you want to be displayed on click) in your list view's row layout file, keep it invisible / gone by-default and mark it as visible for clicked row on onItemClickListener event of list view.
Hope this help you.
If you are using base adapter then it would be better to solve this issue.
well I was finally able to figure it out thanks to all who tried and here is the answer
public void createTable(String itemName,String itemQuantity,String itemPrice,String itemAmount){
final TableRow tr1 = new TableRow(getApplicationContext());
tr1.setId(id);
tr1.setClickable(true);
tr1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TableRow vi = (TableRow) lytTableData.getChildAt(lytTableData.indexOfChild(tr1));
for(int i = 0;i<lytTableData.getChildCount();i++){
TableRow vi2 = (TableRow) lytTableData.getChildAt(i);
vi2.getChildAt(4).setVisibility(View.INVISIBLE);
}
vi.getChildAt(4).setVisibility(View.VISIBLE);
}
});
tr1.setLayoutParams(new LayoutParams( LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
TextView textViewItemName,textViewItemPrice;
textViewItemName= new TextView(this);textViewItemQuantity= new EditText(this);textViewItemPrice= new TextView(this);textViewItemAmount= new TextView(this);
iv2 = new ImageView(this);
textViewItemName.setWidth(500);textViewItemQuantity.setWidth(100);textViewItemPrice.setWidth(100);textViewItemAmount.setWidth(100);
textViewItemName.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize), LayoutParams.WRAP_CONTENT, 6f));
textViewItemQuantity.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
textViewItemPrice.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
textViewItemAmount.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
textViewItemName.setText(itemName);textViewItemQuantity.setText(itemQuantity);textViewItemPrice.setText(itemPrice);textViewItemAmount.setText(itemAmount);
textViewItemQuantity.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large); textViewItemPrice.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large);
textViewItemQuantity.setInputType(InputType.TYPE_CLASS_NUMBER);
textViewItemName.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large); textViewItemAmount.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large);
textViewItemName.setTextColor(0xFF121212);textViewItemQuantity.setTextColor(0xFF121212);textViewItemPrice.setTextColor(0xFF121212);textViewItemAmount.setTextColor(0xFF121212);
textViewItemName.setPadding(0, 15, 5, 0);textViewItemQuantity.setPadding(0, 15, 5, 0);textViewItemPrice.setPadding(0, 15, 5, 0);textViewItemAmount.setPadding(0, 15, 5, 0);
textViewItemQuantity.setBackgroundColor(0x0000000000);
iv2.setLayoutParams(new TableRow.LayoutParams((int) LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
iv2.setImageResource(R.drawable.delete);
iv2.setVisibility(View.INVISIBLE);
iv2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
lytTableData.removeView(tr1);
}
});
tr1.addView(textViewItemName);tr1.addView(textViewItemQuantity);tr1.addView(textViewItemPrice);tr1.addView(textViewItemAmount);tr1.addView(iv2);
lytTableData.addView(tr1, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
Double intItemAmount = Double.parseDouble(itemAmount);
intTotalAmount = intTotalAmount + intItemAmount;
//intTotalAmount2 = intTotalAmount2 + intItemAmount;
txtTotal.setText(String.valueOf(intTotalAmount));
//txtDiscount.setText("0");
txtAmount.setText(String.valueOf(intTotalAmount));
id = id + 1;
System.out.println(tr1.getId());
}
I have a TableLayout which I add rows programmatically and I'm trying to set "weight" of elements contained in rows programmatically.
I'm trying to do that by setting weightSum on the TableRow and setting "weight" of the columns using the last parameter of "LayoutParams", but nothing appears; instead if I give a fixed width to elements, code works well.
This is my layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/sample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center" >
</TableLayout>
</LinearLayout>
This is the code:
public class HomeFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.home, null);
}
#Override
public void onStart() {
super.onStart();
TableLayout tl = (TableLayout)getActivity().findViewById(R.id.sample);
TableRow tr = new TableRow(getActivity());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tr.setGravity(Gravity.CENTER);
tr.setWeightSum(1f);
TableRow.LayoutParams lp;
TextView tv1 = new TextView(getActivity());
lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.6f);
tv1.setText("AAA");
tv1.setLayoutParams(lp);
TextView tv2 = new TextView(getActivity());
lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.4f);
tv2.setText("BBB");
tv2.setLayoutParams(lp);
tr.addView(tv1);
tr.addView(tv2);
tl.addView(tr);
}
}
Here is the updated code for setting the weightsum and column weight programmatically for a tableLayout.
TableLayout tl = (TableLayout)getActivity().findViewById(R.id.sample);
TableRow tr = new TableRow(getActivity());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tr.setGravity(Gravity.CENTER);
tr.setWeightSum(4); //total row weight
TableRow.LayoutParams lp;
lp.weight = 1; //column weight
TextView tv1 = new TextView(getActivity());
lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.6f);
tv1.setText("AAA");
tv1.setLayoutParams(lp);
TextView tv2 = new TextView(getActivity());
lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.4f);
tv2.setText("BBB");
tv2.setLayoutParams(lp);
tr.addView(tv1);
tr.addView(tv2);
tl.addView(tr);
I want to put my number button like that. This is my code:
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout layout2 = new LinearLayout(this);
layout2.setOrientation(LinearLayout.HORIZONTAL);
TextView titleView = new TextView(this);
titleView.setText("Hello World!");
layout.addView(titleView);
android.widget.LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT, 1);
android.widget.TableLayout.LayoutParams params = new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.FILL_PARENT, 1);
Button btnConnect = new Button(this);
btnConnect.setText("Connect");
btnConnect.setLayoutParams(param);
layout2.addView(btnConnect);
Button btnDisconnect = new Button(this);
btnDisconnect.setText("Disconnect");
layout2.addView(btnDisconnect);
btnDisconnect.setLayoutParams(param);
layout.addView(layout2);
TableLayout tblLayout = new TableLayout(this);
tblLayout.setOrientation(TableLayout.HORIZONTAL);
TableRow tblrow = null;
for (int i = 1; i <= 9; i++) {
if (i % 3 == 1) {
tblrow = new TableRow(this);
tblLayout.addView(tblrow);
}
Button b = new Button(this);
b.setText("" + i);
tblrow.addView(b);
}
TableRow tr = new TableRow(this);
Button btnZero = new Button(this);
btnZero.setText("0");
Button btnHash = new Button(this);
btnHash.setText("#");
Button btnStar = new Button(this);
btnStar.setText("*");
tr.addView(btnZero);
tr.addView(btnHash);
tr.addView(btnStar);
tblLayout.addView(tr);
layout.addView(tblLayout);
setContentView(layout);
this complies:
In order to make my columns like that. I used this code:
android.widget.TableLayout.LayoutParams params = new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.FILL_PARENT, 1);
tblLayout.setLayoutParams(param);
but there's no change. What do i need to do? Is that Layoutparams not enough to do that?
You need to set the widths of layout, layout2, tblLayout, each tblRow, and tr to MATCH_PARENT and each Button must have an equal layout_weight.