How to keep both components from XML and those added programatically - java

I am new to Android.
I am drawing Textfield using drag-drop from Eclipse.
Also adding a Table programatically.
But I can see only table which is added programatically.
How to keep both components from XML and those added programatically.
Sorry if it is a really basic and stupid question.
Here is my code
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*"
android:stretchColumns="*">
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<ZoomButton
android:id="#+id/zoomButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/btn_plus" />
<ZoomControls
android:id="#+id/zoomControls1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TwoLineListItem
android:id="#+id/twoLineListItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<AbsoluteLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</AbsoluteLayout>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />
<AnalogClock
android:id="#+id/analogClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CalendarView
android:id="#+id/calendarView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableLayout>
and
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHandler db = new DatabaseHandler(this);
//To insert
// db.addContact(new Contact("Ninad", "9893353432"));
TableLayout table = new TableLayout(this);
table.setStretchAllColumns(true);
table.setShrinkAllColumns(true);
TableRow rowTitle = new TableRow(this);
rowTitle.setGravity(Gravity.CENTER_HORIZONTAL);
TableRow rowDayLabels = new TableRow(this);
// title column/row
TextView title = new TextView(this);
title.setText("Contacts");
title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
title.setGravity(Gravity.CENTER);
title.setTypeface(Typeface.SERIF, Typeface.BOLD);
TableRow.LayoutParams params = new TableRow.LayoutParams();
params.span = 3;
rowTitle.addView(title, params);
// Header 1
TextView header1 = new TextView(this);
header1.setText("ID");
header1.setTypeface(Typeface.SERIF, Typeface.BOLD);
rowDayLabels.addView(header1);
// Header 2
TextView header2 = new TextView(this);
header2.setText("NAME");
header2.setTypeface(Typeface.SERIF, Typeface.BOLD);
rowDayLabels.addView(header2);
// Header 3
TextView header3 = new TextView(this);
header3.setText("PHONE");
header3.setTypeface(Typeface.SERIF, Typeface.BOLD);
rowDayLabels.addView(header3);
// Add to Table
table.addView(rowTitle);
table.addView(rowDayLabels);
List<Contact> contacts = db.getAllContacts();
TableRow rowHighs = null;
for (Contact cn : contacts) {
rowHighs = new TableRow(this);
// Data
TextView day1High = new TextView(this);
day1High.setText(String.valueOf(cn.getID()));
rowHighs.addView(day1High);
TextView day2High = new TextView(this);
day2High.setText(cn.getName());
rowHighs.addView(day2High);
TextView day3High = new TextView(this);
day3High.setText(cn.getPhoneNumber());
rowHighs.addView(day3High);
table.addView(rowHighs);
}
setContentView(table);
}
My Table in the OnCreate method is populated properly, but components from XML are lost.
Update :
What I found is, I am setting it two times and later one is overwritting it
setContentView(R.layout.activity_main);
setContentView(table);
But how can I merge it ?

Type in your activity below oncreate
TableLayout tbl_lay=(TableLayout)findViewById(R.id.tablelayout1);
.....
.....
.....
Your Code.....
TableLayout tb=new TableLayout(this);
.....
.....
atlast add these line
tbl_lay.addview(tb);
it will help you

Your layout name is "tableLayout1" in xml, you should implement a TableLayout object from it by using findViewById, and add your programmatically created view to it. Be careful about positioning, you may put it on your drag-drop views!
TableLayout myLayout = (TableLayout) findViewById(R.id.tableLayout1);
//your elements created
myLayout.addView(yourCreatedViewElement);

Related

How to create a scroll list of custom relative layouts not stacking upon each other in Android Studio?

I want to create a scroll view list which would show "boxes" or more accurately custom made layouts (I'm using a custom class that extends a RelativeLayout - basically shows different pieces of information, some are static like places' working hours and some change dynamically and will be pulled from a server).
Though I encountered a problem - I (for the sake of seeing if my solution works) created 5 boxes and added them to the scroll view list but it seems like they are stacked upon each other. What's the proper way to make them appear one under another without manually tweaking their position coordinates? I was using addView() for that purpose but it doesn't work as intended for me or I use it poorly. If you know the answer, please briefly describe how this should be done.
Thanks in advance!
EDIT, here goes the code:
public class RestaurantBox extends RelativeLayout {
RestaurantBox (Context context){
super(context);
this.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
TextView restaurantName = new TextView(context);
restaurantName.setText("Test Restaurant");
RelativeLayout.LayoutParams restNameParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
this.addView(restaurantName, restNameParams);
restaurantName.setTypeface(null, Typeface.BOLD);
TextView freeSpots = new TextView(context);
freeSpots.setText("15/20");
RelativeLayout.LayoutParams freeSpotParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
freeSpotParams.topMargin = restNameParams.bottomMargin + 50;
this.addView(freeSpots, freeSpotParams);
TextView book = new TextView(this.getContext());
RelativeLayout.LayoutParams bookParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
book.setText("Book");
bookParams.setMargins(0,freeSpotParams.bottomMargin + 100,0,0);
this.addView(book, bookParams);
}
}
public class BrowseRestaurants extends AppCompatActivity {
int restaurantCount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browse_restaurants);
Intent intent = getIntent();
String login = intent.getStringExtra("LOGIN");
TextView text = (TextView) findViewById(R.id.txtWelcomeUser);
text.setText("Welcome, " + login);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relLayoutRestaurants);
RestaurantBox initRBox = new RestaurantBox(this);
initRBox.setTop(0);
initRBox.setBottom(300);
relativeLayout.addView(initRBox);
for(int i=1;i<5;i++){
final View view = relativeLayout.getChildAt(i-1);
RestaurantBox restaurantBox = new RestaurantBox(this);
restaurantBox.setTop(view.getBottom() + 50);
restaurantBox.setBottom(restaurantBox.getTop() + 300);
relativeLayout.addView(restaurantBox);
}
}
}
<!-- activity_browse_restaurants.xml-->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical"
tools:context="com.konrad.rezerwacje1.BrowseRestaurants">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollViewRestaurants"
android:layout_below="#+id/txtWelcomeUser"
android:layout_alignParentStart="true">
<RelativeLayout
android:id="#+id/relLayoutRestaurants"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="65dp"
android:orientation="vertical">
</RelativeLayout>
</ScrollView>
<TextView
android:id="#+id/txtLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Logout"
android:textColor="#android:color/holo_red_dark"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="#+id/txtWelcomeUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textColor="#android:color/holo_blue_dark"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="9dp" />
</RelativeLayout
>
you are adding view in RelativeLayout so view stacking on each other so change it to LinearLayout in activity_browse_restaurants.xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollViewRestaurants"
android:layout_below="#+id/txtWelcomeUser"
android:layout_alignParentStart="true">
<LinearLayout
android:id="#+id/relLayoutRestaurants"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="65dp"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
now make changes according to in BrowseRestaurants.class
replace
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relLayoutRestaurants);
with
LinearLayout relativeLayout = (LinearLayout) findViewById(R.id.relLayoutRestaurants);
else will be fine if you want to change variable name its up to u, let me know if any problem

how to put dynamic TableRow in TableLayout and place Textview dynamically in TableRow using Android?

i am newbie and facing difficulty to achieve my required output.
XML CODE:
//this code is inside of ScrollView
<LinearLayout
android:id="#+id/statusSecond_Layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/statusDisciplineTable_layout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
></TableLayout>
</LinearLayout>
JAVA CODE:
setContentView(R.layout.status_view);
statusTableLayout = (TableLayout)findViewById(R.id.statusDisciplineTable_layout);
for(int i=0;i<2;i++)
{
TableRow statusTableRow = new TableRow(this);
statusTableRow.setId(i);
statusTableRow.setOrientation(TableRow.VERTICAL);
TextView productsTextView = new TextView(this);
productsTextView.setText("product name:"+i);
statusTableRow.addView(productsTextView);
//statusTableRow.setBackgroundColor(Color.BLACK);
for(int j=0;j<2;j++)
{
RelativeLayout statusRelativelayout = new RelativeLayout(this);
TableRow.LayoutParams rlp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
rl.setMargins(0, 0, 16, 0);
rl.addRule(RelativeLayout.ALIGN_LEFT);
TextView label = new TextView(this);
label.setId(j);
label.setText("abcd:"+j);
label.setLayoutParams(rl);
statusRelativelayout.addView(label);
statusTableRow.addView(statusRelativelayout,rlp);
}
statusTableLayout.addView(statusTableRow);}
please tell me what should i need to do changes in my current code to product required given image
You can use LinearLayout instead of TableRow like this
TableLayout statusTableLayout = (TableLayout)findViewById(R.id.statusDisciplineTable_layout);
for(int i=0;i<2;i++)
{
LinearLayout statusTableRow = new LinearLayout(this);
statusTableRow.setId(i);
statusTableRow.setOrientation(LinearLayout.VERTICAL);
TextView productsTextView = new TextView(this);
productsTextView.setText("product name:"+i);
statusTableRow.addView(productsTextView);
//statusTableRow.setBackgroundColor(Color.BLACK);
for(int j=0;j<2;j++)
{
RelativeLayout statusRelativelayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
rl.setMargins(0, 0, 16, 0);
rl.addRule(RelativeLayout.ALIGN_LEFT);
TextView label = new TextView(this);
label.setId(j);
label.setText("abcd:"+j);
label.setLayoutParams(rl);
statusRelativelayout.addView(label);
statusTableRow.addView(statusRelativelayout);
}
statusTableLayout.addView(statusTableRow);}
also put everything inside Relativelayout and then put inside ScrollView like this
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/statusSecond_Layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/statusDisciplineTable_layout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
</TableLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>

TableRow not being displayed in TableLayout

Can anyone see what is wrong with the below:
if (mViewPager.getCurrentItem() == 3){
TableLayout tbl = (TableLayout)findViewById(R.id.tblHistory);
if (tbl.getChildCount() != mExceptions.size()){
for (int i = tbl.getChildCount(); i < mExceptions.size(); i++){
GPSException e = mExceptions.get(i);
//TableRow
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tr.setBackgroundColor(Color.BLACK);
//DateTime
TextView tx = new TextView(this);
tx.setLayoutParams(new android.view.ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tx.setText(e.DateTime);
tx.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
tx.setTextColor(Color.BLACK);
tx.setBackgroundColor(Color.WHITE);
tr.addView(tx);
//Speed
tx = new TextView(this);
tx.setLayoutParams(new android.view.ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tx.setText(Float.toString(e.Speed));
tx.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
tx.setTextColor(Color.BLACK);
tx.setBackgroundColor(Color.WHITE);
tr.addView(tx);
//Bearing
tx = new TextView(this);
tx.setLayoutParams(new android.view.ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tx.setText(Double.toString(e.Degrees));
tx.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
tx.setTextColor(Color.BLACK);
tx.setBackgroundColor(Color.WHITE);
tr.addView(tx);
//Exceptions
tx = new TextView(this);
tx.setLayoutParams(new android.view.ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tx.setText(e.ExceptionString());
tx.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
tx.setTextColor(Color.BLACK);
tx.setBackgroundColor(Color.WHITE);
tr.addView(tx);
tbl.addView(tr, 0);
}
tbl.invalidate();
tbl.refreshDrawableState();
}
}
My class implements LocationListener and the above code is fired in the onLocationChanged event. The aim is to add a TableRow to a TableLayout for each queued "Exception". Each row is inserted at position 0.
I've tried everything apart from the correct approach it would appear!
My layout is as follows (resources not used for the time being, but will be once I've sorted out this issue - for all you eagle eyed viewers out there!):
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<TableRow android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Dt/Tm"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Spd"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Brg"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ex"></TextView>
</TableRow>
</TableLayout>
There are no exceptions thrown, and if I toast a short message in the for loop, it does appear.
If you need more info, not a problem.
Thanks.
tbl.addView(tr, 0);
I think you need to give that layout params, not 0. Perhaps try this?
tbl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
After all of this, removing the android.view.ViewGroup.LayoutParams qualifier (meaning the layout params for each TextView are now TableRow.LayoutParams) has fixed the solution. Why? I do not know......
Maybe the layout parameters of the TextView have to pertain to the TableRow???! Scraping here. Anyway, problem solved :-(

First TableRow is not shown

I'm trying to create a TableLayout, but some error occurs:
The data in the first red colored row are supposed to be in the second, white row.
Here is the layout xml file:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="horizontal|vertical"
android:layout_weight="1"
android:background="#838080">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:id="#+id/myOtherTableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></TableLayout>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000">
<TableLayout
android:id="#+id/myTableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal|vertical"
android:background="#FFFFFF"
>
</TableLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
And here is the code:
Color c = new Color();
setContentView(R.layout.data_table_creater_activity);
android.widget.TableRow.LayoutParams params = new TableRow.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(10, 0, 2, 0);
rowFiller();
TableLayout TbL = (TableLayout) findViewById(R.id.myOtherTableLayout);
TableRow headerRowHead = new TableRow(this);
headerRowHead.setId(51);
headerRowHead.setBackgroundColor(c.rgb(241,26,41));
TextView header1 = new TextView(this);
header1.setText(Html.fromHtml("<u>Purchase_Order_Number</u>"));
header1.setLayoutParams(params);
headerRowHead.addView(header1);
TextView header2 = new TextView(this);
header2.setText(Html.fromHtml("<u>Vendor</u>"));
header2.setLayoutParams(params);
headerRowHead.addView(header2);
TextView header3 = new TextView(this);
header3.setText(Html.fromHtml("<u>Currency</u>"));
header3.setLayoutParams(params);
headerRowHead.addView(header3);
TextView header4 = new TextView(this);
header4.setText(Html.fromHtml("<u>Total_Price</u>"));
header4.setLayoutParams(params);
headerRowHead.addView(header4);
TbL.addView(headerRowHead, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
));
TableRow headerRowData = new TableRow(this);
headerRowData.setId(50);
headerRowData.setBackgroundColor(c.rgb(255,255,255));
TextView headerData1 = new TextView(this);
header1.setText("0350005000");
header1.setLayoutParams(params);
headerRowData.addView(headerData1);
TextView headerData2 = new TextView(this);
header2.setText("Vendor_A");
header2.setLayoutParams(params);
headerRowData.addView(headerData2);
TextView headerData3 = new TextView(this);
header3.setText("EUR");
header3.setLayoutParams(params);
headerRowData.addView(headerData3);
TextView headerData4 = new TextView(this);
header4.setText("44.60");
header4.setLayoutParams(params);
headerRowData.addView(headerData4);
TbL.addView(headerRowData, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
));
Why are the data of the first row not displayed? And why are the data of the second row displayed in the first row?
Working with header1.setText(Html.fromHtml("<u>*Text*</u>"));, to underline text works fine, as you can see in the TableLayout below.
TextView headerData1 = new TextView(this);
header1.setText("0350005000");
header1.setLayoutParams(params);
headerRowData.addView(headerData1);
TextView headerData2 = new TextView(this);
header2.setText("Vendor_A");
header2.setLayoutParams(params);
headerRowData.addView(headerData2);
TextView headerData3 = new TextView(this);
header3.setText("EUR");
header3.setLayoutParams(params);
headerRowData.addView(headerData3);
TextView headerData4 = new TextView(this);
header4.setText("44.60");
header4.setLayoutParams(params);
headerRowData.addView(headerData4);
You have added header3 instead of headerData3 and so on for headerData1 ,headerData2 ,headerData3 .and headerData4 please check that , Is that the issue

Why do my programatically added TableRows not float in line with my table-header?

MY GOAL:
I am trying to populate a table(with a headline that I created in my xml) with a bunch of programmatically added views. The plan is to add some TextViews to a TableRow and then add that TableRow to the final table that created. I tried to follow this tutorial.
MY PROBLEM:
The programmatically added views do not go inline with the columns that the xml-created TextViews dictate. Please see the following screenshot:
MY CODE:
1.) XML
<TableLayout
android:id="#+id/tableViewTable"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="#dimen/margin_EvalProblemTableRight"
android:layout_marginBottom="#dimen/margin_EvalProblemTableBottom"
android:text="#string/str_EvalProblemNumber"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="#dimen/margin_EvalProblemTableRight"
android:layout_marginBottom="#dimen/margin_EvalProblemTableBottom"
android:text="#string/str_EvalProblemString"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="#dimen/margin_EvalProblemTableRight"
android:layout_marginBottom="#dimen/margin_EvalProblemTableBottom"
android:text="#string/str_EvalProblemCorrectSolution"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="#dimen/margin_EvalProblemTableRight"
android:layout_marginBottom="#dimen/margin_EvalProblemTableBottom"
android:text="#string/str_EvalProblemUserSolution"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="#dimen/margin_EvalProblemTableRight"
android:layout_marginBottom="#dimen/margin_EvalProblemTableBottom"
android:text="#string/str_EvalProblemDuration"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
</TableLayout>
2.) JAVA:
public class EvalActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_eval);
TableLayout tableViewTable = (TableLayout) findViewById(R.id.tableViewTable);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Button b = new Button(this);
b.setText("Button");
b.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TextView t1 = new TextView(this);
t1.setText("I am textView1");
t1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TextView t2 = new TextView(this);
t2.setText("I am textView2");
t2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TextView t3 = new TextView(this);
t3.setText("I am textView3");
t3.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TextView t4 = new TextView(this);
t4.setText("I am textView3");
t4.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tr.addView(b);
tr.addView(t1);
tr.addView(t2);
tr.addView(t3);
tr.addView(t4);
tableViewTable.addView(tr,(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)));
}
How can I join the "hardcoded" xml-TableRow with the programatically added Views and Rows?
Try manually adding an align rule to the layout parameters like this:
LayoutParams myParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
myParams.addRule(RelativeLayout.ALIGN_RIGHT, R.id.tableColumnX);
TextView t3 = new TextView(this);
t3.setText("I am textView3");
t3.setLayoutParams(myParams);

Categories