i want to add spinner dynamically inside table like this image and how to specify its width and height also :
Any help will be appreciated.
Take a TableLayout form xml and inflate the xml to your code.
Now you can add new TableRows to the tableLayout using
addView(new TableRow())...
And again u can addView(View v) to each table Row.
U can addWeight to each TableRow you add to TableLayout..In your case
TableRow weight will be 2
addView(new TextView())
addView(new Spinner())
you can add as many table rows as you want:
"TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayoutFromXML);"
"TableRow tableRow = new TableRow(this);"
"tableRow.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));"
"tableRow.addView(WhatEver view that you want);"
"tableLayout.addView(tableRow);"
view that you want add to tablerow, you can also set their attributes like "width" or "height".
I know i am bit late, but you can try this.....
final TableLayout dynamicTable = (TableLayout) findViewById(R.id.hotelTableLayout);
/* Create a new row to be added. */
final TableRow row = new TableRow(Hotels.this);
row.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
/* Create a component to be the row-content. */
SpnAdult = new Spinner(Hotels.this);
SpnAdult.setLayoutParams(new TableRow.LayoutParams(0,
TableRow.LayoutParams.WRAP_CONTENT, 1));
SpnAdult.setAdapter(adapter);
/* Add Spinner to row. */
row.addView(SpnAdult);
/* Add row to TableLayout. */
dynamicTable.addView(row, new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
Hope this helps....
Related
TableLayout mTableLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTableLayout = findViewById(R.id.table_layout);
mTableLayout = new TableLayout(this);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.black);
TableRow tableRow = new TableRow(this);
tableRow.addView(imageView);
mTableLayout.addView(tableRow);
I want to add an imageview to a table row, then add this table row to Table Layout.
But when this run, the screen has nothing happen. And I don't know why.
I try to find Logcat and break point but nothing wrong.
Ok the issue here is you are creating row first the the table layout; try adding row to layout first then view to row like below reference code:
TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);
TableRow row = new TableRow(this);
TextView tv = new TextView(this);
tv.setText("This is text");
tl.addView(row);
row.addView(tv);
In the code snippet below there's one commented line. When I uncomment that line, then the contents of LinearLayout are not displayed in a TableRow. Without setting the LayoutParams, the row displays both texts. I don't understand this behavior. I know that I can include complex views via xml files, but I'd rather understand what's wrong with this code:
TableLayout tableLayout = (TableLayout) findViewById(R.id.table);
TableRow tableRow = new TableRow(this );
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// when I comment out this line, the row only shows the second text.
// linearLayout.setLayoutParams(layoutParams);
TextView textLabel = new TextView(this);
textLabel.setText("inside linear layout");
linearLayout.addView(textLabel);
TextView message = new TextView(this);
message.setText( "inside tablerow");
tableRow.addView(linearLayout);
tableRow.addView(message);
tableLayout.addView(tableRow);
Assuming the question is something like "What's the issue of this? How to resolve this?", here's my answer:
When you are setting LayoutParams to a View, those params would be used by the parent of this View to layout that View appropriately. So in your case what you have done is following:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(...);
linearLayout.setLayoutParams(layoutParams);
tableRow.addView(linearLayout);
Now, the tableRow is confused, because it expects TableRow.LayoutParams in order to layout the view appropriately, but it suddenly finds out some other layout params. Whereas, had you not explicitly specified params (i.e. when linearLayout.setLayoutParams() is commented out), the default layout params would be generated.
#Override
protected LinearLayout.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(); // this is TableRow.LayoutParams
}
So, instead of creating LinearLayout.LayoutParams, create TableRow.LayoutParams:
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(...);
linearLayout.setLayoutParams(layoutParams);
tableRow.addView(linearLayout);
I have created a fragment which should display table of buttons. I used table layout to create a table. But the button table doesn't display in the fragment. What is the problem related to this code and how can I overcome this???
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
ArrayList buttons = new ArrayList();
ScrollView sv = new ScrollView(this.getActivity());
//Set a TableLayout to add buttons
TableLayout tl= new TableLayout(getActivity());
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
tl.setLayoutParams(params);
tl.setOrientation(TableLayout.HORIZONTAL);
for(int i=0;i<5;i++){
TableRow row=new TableRow(this.getActivity());
LayoutParams paramrow = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
row.setLayoutParams(paramrow);
for(int j=0;j<2;j++){
Button button = new Button(getActivity());
button.setText("testing");
button.setPadding(5, 5, 5, 5);
LayoutParams parab = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(parab);
row.addView(button);
buttons.add(button);
}
tl.addView(row);
}
sv.addView(tl);
}
You're using the wrong type of LayoutParams in at least two places. Views need to use the type of LayoutParams that correspond to their container. Therefore Views contained directly in a TableLayout - i.e., the TableRows - should use TableLayout.LayoutParams. The Views contained in TableRows - the Buttons, in this case - should use TableRow.LayoutParams. And finally, the TableLayout is in a ScrollView, so it should use ScrollView.LayoutParams, which is actually FrameLayout.LayoutParams, since ScrollView extends FrameLayout.
It also appears that you're not adding the ScrollView to the Fragment's View anywhere. I would mention, too, that it might be preferable to define the ScrollView and the TableLayout in layout xml, inflate this layout in onCreateView(), and dynamically create only what needs to be - i.e., the TableRows and Buttons.
I have a TextView inside a TableRow. The text contained in this TextView is tipically larger than the screen. As such, I would like for the the text to wrap into multiple lines.
TableLayout table = (TableLayout)findViewById(R.id.myTable);
TableRow row = new Tablerow(context);
TextView view = new TextView(context);
view.setText(stringWithLotsOfCharacters);
view.setSingleLine(false);
row.add(view);
table.addview(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
I tried using "setSingleLine" but it does not have the effect I was looking for.
Any suggestions? Thanks.
Add android:shrinkColumns="1" for the column you want to wrap to the TableLayout in your layout or android:shrinkColumns="*" if you want to wrap all columns.
You can also do it programmatically with setColumnShrinkable:
public void setColumnShrinkable (int columnIndex, boolean isShrinkable)
view.setHeight(LayoutParams.WRAP_CONTENT);
I'm trying to create a TableLayout programatically. It just won't work. The same layout in an xml file works though. This is what I have:
public class MyTable extends TableLayout
{
public MyTable(Context context) {
super(context);
setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TableRow row = new TableRow(context);
row.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Button b = new Button(getContext());
b.setText("hello");
b.setLayoutParams(new LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row.addView(b);
addView(row)
}
}
...
// In main activity:
MyTable table = new MyTable(this);
mainLayout.addView(table);
When I run this, I don't get a crash, but nothing appears. If I get rid of the TableRow instance, at least the button does appear as a direct child of the TableLayout. What am I doing wrong?
Just to make the answer more clear:
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
TableLayout tableLayout = new TableLayout(context);
tableLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));// assuming the parent view is a LinearLayout
TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(tableParams);// TableLayout is the parent view
TextView textView = new TextView(context);
textView.setLayoutParams(rowParams);// TableRow is the parent view
tableRow.addView(textView);
Explanation
When you call setLayoutParams, you are supposed to pass the LayoutParams of the parent view
It turns out I needed to specify TableRowLayout, TableLayout etc for the layout params, otherwise the table just won't show!
For me, to get mine I had to call addContentView().
A good solution is to inflate layout files for each instance of row you want to create. See this post : How to duplicate Views to populate lists and tables?
Your problem is at this line:
b.setLayoutParams(new LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
You need to change LayoutParams to TableRow.LayoutParams:
b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));