I have a table defined in .xml file but rows and headers have to be added dynamically. I have a problem with fill width with table row on horizontal and vertical orientation. I have tried stretchColumns parameter but it did not help. What is the best way to do it?
table = (TableLayout) findViewById(R.id.testList);
TableRow headerRow = new TableRow(this);
JSONObject obj;
obj = new JSONObject(loadJSONFromAsset());
JSONArray records = obj.getJSONArray("tests");
ArrayList<String> headers = new ArrayList<>();
headers.add("Header 1");
headers.add("Header 2");
headers.add("Header 3");
headers.add("Header 4");
headers.add("Header 5");
for(int i = 0; i < headers.size(); i++) {
TextView header = new TextView(this);
header.setText(headers.get(i));
header.setTextColor(Color.WHITE);
header.setBackgroundColor(Color.rgb(24, 116, 205));
header.setPadding(15, 15, 15, 15);
header.setTextSize(20);
header.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
header.setMaxLines(1);
headerRow.addView(header);
}
table.addView(headerRow);
and xml
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<HorizontalScrollView
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TableLayout
android:id="#+id/testList"
android:layout_height="match_parent"
android:layout_width="wrap_content">
</TableLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
</GridLayout>
Try this way. The following will make sure TableRow is aligned to full available width.
After base components, put your TableLayout in XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainLL"
android:orientation="vertical">
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Now in your logic, you can add according to the number of items from response. This should be in a for loop.
for(int i = 0; i < leftComponent.size(); i++)
{
//Table Layout parameters
TableRow.LayoutParams textViewParam = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f);
TableLayout tableLayout = (TableLayout) view.findViewById(R.id.fenceTableLayout);
TableRow trHead = new TableRow(context);
LayoutParams tableRowParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
trHead.setLayoutParams(tableRowParams);
TextView nameHead = new TextView(context);
nameHead.setText(leftComponent[i]);
nameHead.setLayoutParams(textViewParam);
nameHead.setGravity(Gravity.CENTER);
trHead.addView(nameHead);
TextView detailHead = new TextView(context);
detailHead.setText(rightComponent[i]);
detailHead.setGravity(Gravity.CENTER);
detailHead.setLayoutParams(textViewParam);
trHead.addView(detailHead);
tableLayout.addView(trHead);
}
You may need to customize according to your requirement.
Related
i'm using a TableLayout on my applycation. This is my XML code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scrollbars="none">
<include
android:id="#id/action_bar"
layout="#layout/actionbar_toolbar" />
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/action_bar">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/table"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableLayout>
</ScrollView>
</HorizontalScrollView>
And, i fill the table with Java code:
TableRow tbrow = new TableRow(this);
TextView txt_plazo = new TextView(this);
txt_plazo.setText(" Plazo ");
txt_plazo.setTextColor(Color.WHITE);
txt_plazo.setTextSize(16);
txt_plazo.setMinimumHeight(0);
txt_plazo.setBackgroundColor(Color.DKGRAY);
txt_plazo.setGravity(Gravity.CENTER);
tbrow.addView(txt_plazo);
TextView txt_saldoInicial = new TextView(this);
txt_saldoInicial.setText(" Saldo Inicial");
txt_saldoInicial.setTextColor(Color.WHITE);
txt_saldoInicial.setTextSize(16);
txt_saldoInicial.setBackgroundColor(Color.DKGRAY);
txt_saldoInicial.setGravity(Gravity.CENTER);
tbrow.addView(txt_saldoInicial);
TextView txt_parcialidades = new TextView(this);
txt_parcialidades.setText(" Parcialidades ");
txt_parcialidades.setTextColor(Color.WHITE);
txt_parcialidades.setTextSize(16);
txt_parcialidades.setBackgroundColor(Color.DKGRAY);
txt_parcialidades.setGravity(Gravity.CENTER);
tbrow.addView(txt_parcialidades);
TextView txt_interes = new TextView(this);
txt_interes.setText(" Interés ");
txt_interes.setTextSize(16);
txt_interes.setBackgroundColor(Color.DKGRAY);
txt_interes.setGravity(Gravity.CENTER);
txt_interes.setTextColor(Color.WHITE);
tbrow.addView(txt_interes);
TextView txt_total = new TextView(this);
txt_total.setText(" Abono Capital");
txt_total.setTextSize(16);
txt_total.setBackgroundColor(Color.DKGRAY);
txt_total.setGravity(Gravity.CENTER);
txt_total.setTextColor(Color.WHITE);
tbrow.addView(txt_total);
table.addView(tbrow);
fillTable();
Etc...
All works fine but i'm trying to view the table in landscape mode, the problem is that the table don't fill all screen. In portrait mode looks good.
P.D: android:shrinkColumns and android:stretchColumns don't work for me.
Use match_parent for your views.
I'm having troubles trying to create a LinearLayout with an ImageView inside programatically (and add this LinearLayout to another LinearLayout). The XML code that I'm tryng to recreate is this:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout_morty_combinationsV"
android:gravity="center">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:background="#color/black"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/morty"
android:id="#+id/imageView1"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:background="#color/black"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/morty"
android:id="#+id/imageView2"/>
</LinearLayout>
</LinearLayout>
This is my java code
LinearLayout ll_combinations = (LinearLayout) findViewById(R.id.linearLayout_morty_combinationsV);
Iterator<MortyObj> iterator = mortys.iterator();
while (iterator.hasNext()) {
final MortyObj mortyC = iterator.next();
LinearLayout ll_new = new LinearLayout(this);
ll_new.setOrientation(LinearLayout.VERTICAL);
ll_new.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.weight = 1f;
ll_new.setLayoutParams(params);
ll_new.setBackgroundColor(Color.WHITE);
ImageView morty_imageview = new ImageView(this);
try {
InputStream ims = getAssets().open("morty_images/" + mortyC.getId() + ".png");
Drawable d = Drawable.createFromStream(ims, null);
morty_imageview.setImageDrawable(d);
} catch (IOException e) {
e.printStackTrace();
}
FrameLayout.LayoutParams imgParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
morty_imageview.setLayoutParams(imgParams);
ll_new.addView(morty_imageview);
ll_combinations.addView(ll_new);
}
This is how it should look:
XML
And this is how It looks:
JAVA
I've been stuck in this for the last 2 hours.
You can create a layout with something like
View view = (View) findViewById(R.layout.current_layout); //the layout you set in `setContentView()`
LinearLayout picLL = new LinearLayout(CurrentActivity.this);
picLL.layout(0, 0, 100, 0);
picLL.setLayoutParams(new LayoutParams(1000, 60));
picLL.setOrientation(LinearLayout.HORIZONTAL);
((ViewGroup) view).addView(picLL);
What parameters you pass in layout() are obviously going to depend on what you want. Then you can create separate Views to add to the Layout you just created. But I highly advise reading through the docs to understand what all can be done here.
Edit
ImageView myImage = new ImageView(this);
picLL.addView(myImage);
//set attributes for myImage;
Just found the problem. If i load an image from res/drawables I get the view I want, if I load it from assets it doesn't work
I am using a table layout to show information based on a user specifications. The user enters data into a database, then this data is taken and displayed on a table were each row has 3 textView entries. The problem is that if the data entered by the user is to long, it goes off screen and is not visible. Any ideas? The code I used to do this is below:
xml
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*"
android:id="#+id/tl">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp" >
<TextView
android:id="#+id/timeCol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#FF0000"/>
....
</TableRow>
</TableLayout>
Java
for (final String time : timesArray)
{
i++;
TableRow row1= new TableRow(this);
event = db.getEvent(i, gameId);
player = db.getPlayer(i, gameId);
TextView timeRow = new TextView(this);
TextView playerRow = new TextView(this);
TextView eventRow = new TextView(this);
timeRow.setText(time);
timeRow.setTextColor(Color.WHITE);
playerRow.setText(player);
playerRow.setTextColor(Color.WHITE);
eventRow.setText(event);
eventRow.setTextColor(Color.WHITE);
row1.addView(timeRow);
row1.addView(playerRow);
row1.addView(eventRow);
tl.addView(row1);
}
And it looks like this if the data is too long
SOLUTION!
Setting layout_width to 0 and then assigning layout_weight to a float based on the amount of the screen you want a column to take up (For example, 0.5 would let the column take up half the screen) allows all the information to be shown on the screen. The code to do this is below.
XML
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/tl">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp" >
<TextView
android:id="#+id/timeCol"
android:layout_width="0px"
android:layout_weight="0.2"
android:layout_height="wrap_content"
android:text="Time"/>
<TextView
android:id="#+id/playerCol"
android:layout_width="0px"
android:layout_weight="0.4"
android:layout_height="wrap_content"
android:text="Player"/>
<TextView
android:id="#+id/eventCol"
android:layout_width="0px"
android:layout_weight="0.4"
android:layout_height="wrap_content"
android:text="Event"/>
</TableRow>
java
TableLayout tl = (TableLayout) findViewById(R.id.tl);
TableRow row1= new TableRow(this);
TextView timeRow = new TextView(this);
TextView playerRow = new TextView(this);
TextView eventRow = new TextView(this);
TableRow.LayoutParams text1Params = new TableRow.LayoutParams();
text1Params.width = 0;
text1Params.weight = (float) 0.2;
TableRow.LayoutParams text2Params = new TableRow.LayoutParams();
text2Params.weight = (float) 0.4;
text2Params.width = 0;
TableRow.LayoutParams text3Params = new TableRow.LayoutParams();
text3Params.weight = (float) 0.4;
text3Params.width = 0;
timeRow.setLayoutParams(text1Params);
playerRow.setLayoutParams(text2Params);
eventRow.setLayoutParams(text3Params);
row1.addView(timeRow);
row1.addView(playerRow);
row1.addView(eventRow);
tl.addView(row1, params);
I have created an activity in which I want to programmatically generate rows in a table. The table will have a checkbox, imageview, and 2 textviews in every rows. The rows are getting generated, but the alignment of the contents in the rows are not correct.
The activity code:
public class PlanTripActivity extends ActionBarActivity {
int tableRowCount = 0;
TableLayout plan_a_trip_table;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_plan_trip);
plan_a_trip_table = (TableLayout) findViewById(R.id.plan_a_trip_table);
planTripBody("", "Test Name Type",
"P1");
}
//table body
public void planTripBody(String imagename, String nameType,
String placeId) {
tableRowCount = tableRowCount + 1;
// ----------------plan_a_trip_table table
// body------------------------------------------
TableRow plan_trip_tr_data = new TableRow(this);
plan_trip_tr_data.setId(10);
//plan_trip_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
plan_trip_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// select checkbox
final CheckBox select_checkbox = new CheckBox(this);
select_checkbox.setId(20);
select_checkbox.setTextColor(Color.BLACK);
select_checkbox.setGravity(Gravity.CENTER);
select_checkbox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
plan_trip_tr_data.addView(select_checkbox);
// image
final ImageView place_imageview = new ImageView(this);
place_imageview.setId(20);
// test_name.setText(parser.getValue(e, KEY_SETNAME));
//place_imageview.setPadding(5, 5, 5, 5);
place_imageview.setImageResource(R.drawable.planatrip);
plan_trip_tr_data.addView(place_imageview);// add the column to
// the table row
// here
// Type
final TextView name_type = new TextView(this);
name_type.setId(21);// define id that must be unique
// no_of_types.setText(parser.getValue(e, KEY_RIGHTMARKS)); // set
// the text for the header
name_type.setText(Html.fromHtml(nameType));
name_type.setTextColor(Color.BLACK); // set the color
name_type.setPadding(5, 5, 5, 5); // set the padding (if
// required)
name_type.setGravity(Gravity.LEFT);
name_type.setTextSize(18);
plan_trip_tr_data.addView(name_type); // add the column
// to
// the table row
// here
// ID
final TextView place_id = new TextView(this);
place_id.setId(21);// define id that must be unique
// total_amount.setText(parser.getValue(e, KEY_WRONGMARKS)); // set
// the text for the header
place_id.setText(placeId);
place_id.setTextColor(Color.BLACK); // set the color
place_id.setPadding(5, 5, 5, 5); // set the padding (if
// required)
place_id.setGravity(Gravity.CENTER);
place_id.setTextSize(10);
plan_trip_tr_data.addView(place_id); // add the column
// to the
// table row here
plan_a_trip_table.addView(plan_trip_tr_data,
new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// ----------------------On click name_type
name_type.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!select_checkbox.isChecked()) {
select_checkbox.setChecked(true);
} else {
select_checkbox.setChecked(false);
}
}
});
// ----------------------On click customer_name
}
}
The layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<TextView
android:id="#+id/city_name_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="City name"
android:textSize="20sp" />
<ImageView
android:id="#+id/update_indicator_imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/presence_busy"
android:layout_marginLeft="10dp"/>
</TableRow>
<TableLayout
android:id="#+id/plan_a_trip_table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tableRow1"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:stretchColumns="0,1,2,3">
</TableLayout>
<Button
android:id="#+id/plan_a_trip_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/plan_a_trip_table"
android:layout_centerHorizontal="true"
android:text="Plan Trip"
android:layout_marginTop="20dp"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
The output:
Expected output:
What should I do to center the checkbox, imageview and textboxes in the same line such that they do not look haphazard as in the above output?
In your code,you also need to align your TableRow in CENTER,as
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
rowParams.gravity = Gravity.CENTER;
And also set layoutParams for all the child Views.
i.e. Simply,Try to change planTripBody function with below code,it will work.
public void planTripBody(String imagename, String nameType,
String placeId) {
tableRowCount = tableRowCount + 1;
// ----------------plan_a_trip_table table
// body------------------------------------------
TableRow plan_trip_tr_data = new TableRow(this);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
rowParams.gravity = Gravity.CENTER;
plan_trip_tr_data.setLayoutParams(rowParams);
plan_trip_tr_data.setId(10);
//plan_trip_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
plan_trip_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TableRow.LayoutParams innerParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
// select checkbox
final CheckBox select_checkbox = new CheckBox(this);
select_checkbox.setId(20);
select_checkbox.setLayoutParams(innerParams);
select_checkbox.setTextColor(Color.BLACK);
select_checkbox.setGravity(Gravity.CENTER);
select_checkbox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
plan_trip_tr_data.addView(select_checkbox);
// image
final ImageView place_imageview = new ImageView(this);
place_imageview.setId(20);
place_imageview.setLayoutParams(innerParams);
// test_name.setText(parser.getValue(e, KEY_SETNAME));
//place_imageview.setPadding(5, 5, 5, 5);
place_imageview.setImageResource(R.drawable.planatrip);
plan_trip_tr_data.addView(place_imageview);// add the column to
// the table row
// here
// Type
final TextView name_type = new TextView(this);
name_type.setLayoutParams(innerParams);
name_type.setId(21);// define id that must be unique
// no_of_types.setText(parser.getValue(e, KEY_RIGHTMARKS)); // set
// the text for the header
name_type.setText(Html.fromHtml(nameType));
name_type.setTextColor(Color.BLACK); // set the color
name_type.setPadding(5, 5, 5, 5); // set the padding (if
// required)
name_type.setGravity(Gravity.LEFT);
name_type.setTextSize(18);
plan_trip_tr_data.addView(name_type); // add the column
// to
// the table row
// here
// ID
final TextView place_id = new TextView(this);
place_id.setLayoutParams(innerParams);
place_id.setId(21);// define id that must be unique
// total_amount.setText(parser.getValue(e, KEY_WRONGMARKS)); // set
// the text for the header
place_id.setText(placeId);
place_id.setTextColor(Color.BLACK); // set the color
place_id.setPadding(5, 5, 5, 5); // set the padding (if
// required)
place_id.setGravity(Gravity.CENTER);
place_id.setTextSize(10);
plan_trip_tr_data.addView(place_id); // add the column
// to the
// table row here
plan_a_trip_table.addView(plan_trip_tr_data,
new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// ----------------------On click name_type
name_type.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!select_checkbox.isChecked()) {
select_checkbox.setChecked(true);
} else {
select_checkbox.setChecked(false);
}
}
});
// ----------------------On click customer_name
}
EDIT
You need to set android:gravity="center_vertical" property to TableRow to align its child vertically center.
And Use TableRow height as android:layout_height="match_parent"
and set Padding to table row as android:paddingTop="10dp",do not set Margin to Textview as
So Change your layout to below code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:paddingTop="10dp"
android:gravity="center_vertical"
android:layout_centerHorizontal="true" >
<TextView
android:id="#+id/city_name_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="City name"
android:textSize="20sp" />
<ImageView
android:id="#+id/update_indicator_imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/presence_busy"
android:layout_marginLeft="10dp"/>
</TableRow>
<TableLayout
android:id="#+id/plan_a_trip_table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tableRow1"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:stretchColumns="0,1,2,3">
</TableLayout>
<Button
android:id="#+id/plan_a_trip_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/plan_a_trip_table"
android:layout_centerHorizontal="true"
android:text="Plan Trip"
android:layout_marginTop="20dp"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I need my screen to have a couple of text views defined in the activity layout. Below those views I want to set a table whose number of rows and columns will depend upon the user's input (dynamic).
This code is in the oncreate of the activity
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
TableLayout tableLayout = new TableLayout(getApplicationContext());
TableRow tableRow;
TextView textView;
for (int i = 0; i < no_days; i++) {
tableRow = new TableRow(getApplicationContext());
for (int j = 0; j < no_subjects; j++) {
textView = new TextView(getApplicationContext());
textView.setText("Hello");
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
setContentView(tableLayout);
But this code is taking the whole screen and I am unable to have those text views which I have defined in the activity layout.
Here's my activity_home_page.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.attendancerecord.HomePage"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="#string/welcome"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/display_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView5"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
If you only want your TableLayout to below your TextViews then use LinearLayout with orientation "vertical" in activity_home_page instead of RelativeLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.attendancerecord.HomePage"
tools:ignore="MergeRootFrame"
android:orientation="vertical" >
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="welcome"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/display_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="33dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
And add mainLinear.addView(tableLayout); at the end of your loop
LinearLayout mainLinear = (LinearLayout) findViewById(R.id.container);
for (int i = 0; i < 5; i++) {
tableRow = new TableRow(getApplicationContext());
for (int j = 0; j < 4; j++) {
textView = new TextView(getApplicationContext());
textView.setText("Hello");
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
mainLinear.addView(tableLayout);
And remove setContentView(tableLayout);
Hope this works
Add this to your activity file:
<TableLayout
android:id="#+id/table1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/display_user_name"
android:layout_marginTop="15dp" >
</TableLayout>
And in your class :
TableLayout tableLayout = (TableLayout)findViewById(R.id.table1);
TableRow tableRow;
TextView textView;
for (int i = 0; i < no_days; i++) {
tableRow = new TableRow(getApplicationContext());
for (int j = 0; j < no_subjects; j++) {
textView = new TextView(getApplicationContext());
textView.setText("Hello");
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}