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);
Related
I am trying to add cardview to GridLayout dynamically. But it is not adding the cardview.
The Hierarchy is ScrollView --> HorizontalScrollView --> LinearLayout --> GridLayout and I want to add to this grid layout. my code is:
public class MainActivity extends AppCompatActivity {
GridLayout gridLayout;
TextView tc;
CardView newc;
#SuppressLint("SetTextI18n")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridLayout = findViewById(R.id.gridlayout);
newc = new CardView(getApplicationContext());
newc.setLayoutParams(new CardView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
newc.setRadius(8);
newc.setCardElevation(10);
tc = new TextView(getApplicationContext());
tc.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
tc.setText("This is dynamic");
newc.addView(tc);
GridLayout.Spec row = GridLayout.spec(0);
GridLayout.Spec col = GridLayout.spec(0);
GridLayout.LayoutParams gridP = new GridLayout.LayoutParams(row, col);
gridLayout.addView(newc, gridP);
}
}
So how to add the views dynamically to GridLayout.
Thank you.
There are some bugs in your code ... use this
newc = new CardView(getApplicationContext());
newc.setLayoutParams(new CardView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
newc.setRadius(8);
newc.setCardElevation(10);
tc = new TextView(getApplicationContext());
tc.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
tc.setText("This is dynamic");
newc.addView(tc);
gridLayout.setColumnCount(2);
gridLayout.setOrientation(GridLayout.HORIZONTAL);
gridLayout.addView(newc);
If you want to add view to spcific row and specific column use this
GridLayout.Spec row = GridLayout.spec(0); // Mention row index here
GridLayout.Spec col = GridLayout.spec(0);// Mention column index here
GridLayout.LayoutParams gridP = new GridLayout.LayoutParams(row, col);
In your case, you are always using 0,0 for row and column so what basically happening is views are being added to grid layout but they are overlapping and hence you are unable to see those
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....
Originally, I had LinearLayout>ScrollView>TableLayout and the code below:
LinearLayout layout = (LinearLayout) View.inflate(this, R.layout.photos_layout, null);
setContentView(layout);
// TODO Auto-generated method stub
//get the tableLayout which we created in main.xml
tl = (TableLayout)findViewById(R.id.tableLayout1);
for(int i = 0; i < names.length; i++) {
//Create a new row to be added.
TableRow tr = new TableRow(this);
//Create text views to be added to the row.
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
//Put the data into the text view by passing it to a user defined function createView()
createView(tr, tv1, Integer.toString(i+1));
createView(tr, tv2, names[i]);
//Add the new row to our tableLayout tl
tl.addView(tr);
}
The problem is that a scrollView can only have one child, so if I wanted to add more views in addition to the tableLayout I can't. In order to overcome this problem I made the hierarchy like LinearLayout>ScrollView>linearLayout>tableLayout. However, now the code above crashes the app.
What do I need to change in order to populate my table, as well as add views to my newly created linearlayout?
I'd suggest removing the parent LinearLayout. Scrollview in itself can be the parent, and then have one LinearLayout as its only direct child.
In the code you posted that you mentioned was crashing, you're not adding any view to your "newly-created" LinearLayout, instead you're adding a new row to your TableLayout, which if I guess correctly, is inside your scrollview. This is perfectly ok and should not be the cause of the crash.
I want to show two scrollView in one LinearLayout or RelativeLayout.
Which property should is set to show first Scrollview on the top of screen and second scrollview just below the first Scrollview ?
I have tried but in linearlayout it shows me only first scrollview and in relativelayout it shows me only second scrollView.
Yes, I want to do all this dynamically without using xml file.
Here is my code
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.widget.TableLayout.LayoutParams;
public class TableFinal extends Activity {
LinearLayout linearMain, linearScrollView, linearTextview;
RelativeLayout relativeMain;
ScrollView scrollview;
HorizontalScrollView Hscrollview;
TableLayout tablelayout;
TableRow tablerow;
TextView textview;
LinearLayout.LayoutParams linearparmas;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
linearparmas=new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
relativeMain = new RelativeLayout(this);
// First Table
linearScrollView = new LinearLayout(this);
scrollview = new ScrollView(this);
Hscrollview = new HorizontalScrollView(this);
tablelayout = new TableLayout(this);
// First Table's First Row
tablerow = new TableRow(this);
linearTextview = new LinearLayout(this);
textview = new TextView(this);
textview.setText("11");
linearTextview.addView(textview);
tablerow.addView(linearTextview);
linearTextview = new LinearLayout(this);
textview = new TextView(this);
textview.setText("12");
linearTextview.addView(textview);
tablerow.addView(linearTextview);
tablelayout.addView(tablerow);
Hscrollview.addView(tablelayout);
scrollview.addView(Hscrollview);
linearScrollView.addView(scrollview);
relativeMain.addView(linearScrollView);
// first table complete
// second tabler start
linearScrollView = new LinearLayout(this);
scrollview = new ScrollView(this);
Hscrollview = new HorizontalScrollView(this);
tablelayout = new TableLayout(this);
// second Table's First Row
tablerow = new TableRow(this);
linearTextview = new LinearLayout(this);
textview = new TextView(this);
textview.setText("21");
linearTextview.addView(textview);
tablerow.addView(linearTextview);
linearTextview = new LinearLayout(this);
textview = new TextView(this);
textview.setText("22");
linearTextview.addView(textview);
tablerow.addView(linearTextview);
tablelayout.addView(tablerow);
Hscrollview.addView(tablelayout);
scrollview.addView(Hscrollview);
linearScrollView.addView(scrollview);
relativeMain.addView(linearScrollView);
// second tabler complete
setContentView(relativeMain);
}
}
Thank you.
You can use LinearLayout or RelativeLayout.
For all layouts you need to get displayHeight.
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int displayHeight = display.getHeight();
Then for all layouts set layoutParams to fill parent.
Now for scrollViews differences begin.
In LinearLayout for both scrollViews set the same layoutParams (LinearLayout.layoutParams)
scrollView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, displayHeight/2));
And add both to linearLayout
In RelativeLayout set layoutParams like in my example (RelativeLayout.layoutParams)
RelativeLayout.LayoutParams lp1 = new LayoutParams(LayoutParams.FILL_PARENT, displayHeight);
lp1.addRule(ALIGN_PARENT_TOP);
scrollView1.setLayoutParams(lp1);
RelativeLayout.LayoutParams lp2 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
lp2.addRule(BELOW, scrollView1.getId());
scrollView2.setLayoutParams(lp2);
And add both to layout.
Should work in all display resolution if I did not messed up.
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));