My table is populated by dynamic data, so I am adding TableRows and its Views in the code. The TableLayout and the first row (my header) are built in the xml. Gravity set to center works on these items. But setting the Gravity to center on my dynamically created TableRows and Views is not working.
<TableLayout
android:id="#+id/poweredEquip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center"
android:padding="10dip">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/tab_bg_unselected"
android:gravity="center">
<TextView
android:text="Serial"
android:textSize="15sp"
android:textColor="#android:color/black"
android:layout_width="130px"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:gravity="center"
android:layout_column="1"/>
<TextView
android:text="Model"
android:textSize="15sp"
android:textColor="#android:color/black"
android:layout_width="110px"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:gravity="center"
android:layout_column="2"/>
<TextView
android:text="Status"
android:textSize="15sp"
android:textColor="#android:color/black"
android:layout_width="150px"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:gravity="center"
android:layout_column="3"/>
<TextView
android:text="Tool Turned"
android:textSize="15sp"
android:textColor="#android:color/black"
android:layout_width="100px"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:gravity="center"
android:layout_column="4"/>
<TextView
android:text="Hours"
android:textSize="15sp"
android:textColor="#android:color/black"
android:layout_width="110px"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:gravity="center"
android:layout_column="5"/>
<TextView
android:text="Trailer Model"
android:textSize="15sp"
android:textColor="#android:color/black"
android:layout_width="110px"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:gravity="center"
android:layout_column="6"/>
</TableRow>
<TableLayout>
Activity.cs
TableLayout powered = (TableLayout) FindViewById(Resource.Id.poweredEquip);
TableRow.LayoutParams p = new TableRow.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.WrapContent) {Gravity = GravityFlags.Center};
TableRow.LayoutParams p1 = new TableRow.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.WrapContent) {Column = 1, Gravity = GravityFlags.Center};
TableRow row = new TableRow(this) { LayoutParameters = p };
text = new TextView(this) {Text = dr["Serial"].ToString(), LayoutParameters = p1};
text.SetPadding(8, 8, 8, 8);
text.SetMaxWidth(150);
text.SetMinWidth(150);
row.AddView(text);
powered.AddView(row);
Can you post your XML layout for the textview's (just 1 or 2) that work?
Stab in the dark, but you might want to try setting the LAYOUT_GRAVITY for the text's layout parameters instead of the GRAVITY since you are forcing the width to be 150. Specifying the Layout Gravity as CENTER should force the entire TEXTVIEW to move to the middle of the parent container (Table Row).
Another thought: When you set the layout params for the TextView, it's using TableRow.LayoutParams. I think it might actually be setting the parameters in reference to the table row, not setting the parameters of the TextView itself. Try calling this on each TextView you create before adding it to the table row:
text.setGravity(Gravity.CENTER);
If you want same property for your tablerows then you can do it easily.
Just use get layout param from your xml layout and then set those layoutparmas to your dynamic table row.
xml_table_row = (TableRow) findviewbyid (R.id.xml_table_row_id)
TableLayout.layoutparam param = xml_table_row.getLayoutParams();
powered.AddView(row, param);
Hope it will help you. If not please response so that i can try someother way.
Related
I am writing an Android app that requires me to programmatically add a TextView to a TableRow which is, in turn, added to a TableLayout. For the design to look proper, the TextView width must be the same as the TableRow width. The problem I am having is that even though I have attempted to set the width of the TextView to match using MATCH_PARENT, it is not taking effect.
Here is a sample of the Java that I am trying to use to do this:
final TableRow myTableRow = new TableRow(getApplicationContext());
final TextView myTextView = new TextView(getApplicationContext());
myTextView.setWidth(TableRow.LayoutParams.MATCH_PARENT);
myTextView.setPadding(5, 0, 5, 0);
myTextView.setTextColor(Color.BLACK);
myTextView.setTextSize(30);
myTextView.setBackgroundColor(Color.RED);
myTextView.setText("1");
myTableRow.addView(myTextView);
myTableLayout.addView(myTableRow); // Assume 'myTableLayout' has already been declared.
Here is the XML for myTableLayout:
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myTableLayout">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tableRowOne">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/textViewOne"
android:layout_weight="1"
android:textSize="40sp"
android:paddingStart="5dp"
android:paddingEnd="5dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tableRowTwo">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textViewTwo"
android:textColor="#000000"
android:textSize="30sp"
android:paddingEnd="5dp"
android:paddingStart="5dp"
android:layout_weight="1" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tableRowThree">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textViewThree"
android:textColor="#000000"
android:textSize="20sp"
android:paddingEnd="5dp"
android:paddingStart="5dp"
android:layout_weight="1" />
</TableRow>
</TableLayout>
Does anyone have any idea what I could be doing wrong?
Here is a picture of the way the layout appears. The code posted here is not identical to the actual app code, but it is enough to show the problem. Specifically, the code that was removed concerns the replies. In the example code posted as part of the question, only one TextView is shown in the Java code, but in the actually project, there are two.
Layout Example Image
There are several ways you can handle this.
TableLayout and TableRow both extends linearlayout, so maybe you need to set Orientation.
you could set the layout params(margin params) like this
layout_width = 0dp ;
layout_weight = 1 ;
The best way to give height and width property to a view is to use Layout Parameters. most of times (almost all the times for me) setting width and height directly wont work.
TableRow.LayoutParams mParam=new TableRow.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// set layout parameter to view
textView.setLayoutParams(mParam);
A page on my app has a table layout page, and one row of it seems to not be working. My goal is to evenly split the page between the left cell and right cell. The left cell contains a string, the right cell an image and string. I have tried to do so using weighSum. The left cell got weight of 4/8, and right cell was put into a linear layout, within which the image got weight of 1/8 and the string got weight 3/8. However, in the layout the left cell is taking up the great majority of the row, about 75% and I'm not sure why.
My rows below this one attempt almost the same thing, but don't have the same problem.
My xml(cut for relevancy):
<TableRow
android:layout_width="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Class"
android:id="#+id/classTextView"
android:layout_marginTop="14dp"
android:layout_weight="1"
android:gravity="center"/>
<LinearLayout>
<View
android:layout_width="4dp"
android:gravity="left"
android:layout_height="match_parent"
android:background="#663300"/>
<ImageView
tools:ignore="ContentDescription"
android:id="#+id/classicon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/barbarianicon50"
android:layout_gravity="end"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Class Value"
android:id="#+id/classValueView"
android:layout_marginTop="14dp"
android:layout_weight="3"
android:gravity="left"/>
</LinearLayout>
</TableRow>
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="#663300"/>
<TableRow android:layout_width="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Strength"
android:id="#+id/strengthTextView"
android:layout_marginTop="20dp"
android:layout_weight="4"
android:gravity="center" />
<LinearLayout>
<View
android:layout_width="4dp"
android:gravity="left"
android:layout_weight="0"
android:layout_height="match_parent"
android:background="#663300"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Strength Value"
android:id="#+id/strengthValView"
android:layout_marginTop="20dp"
android:layout_weight="4"
android:gravity="center" />
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Intelligence"
android:id="#+id/intelligenceTextView"
android:layout_marginTop="20dp"
android:layout_weight="4"
android:gravity="center" />
<LinearLayout>
<View
android:layout_width="4dp"
android:gravity="left"
android:layout_weight="0"
android:layout_height="match_parent"
android:background="#663300"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Intelligence Value"
android:id="#+id/intelligenceValView"
android:layout_marginTop="20dp"
android:layout_weight="4"
android:gravity="center" />
</LinearLayout>
</TableRow>
The first table row is the problematic one - it should be evenly split between the left and right cells, but is mostly towards the right.
The second table row is an example of it working correctly - this row has 2 pairs of cells, but each of the cells, and the pairs of cells are correctly splitting the width between themselves evenly.
I can't figure out why the first doesn't work. I appreciate any help!
I have finally succeeded in fixing the layout!
The method used was to <include /> another layout as a row element inside this one. All these row elements had the same data, so I then programmatically filled the row elements with their necessary data whenever the view changed. You could also do this as a ListView, with each row in the ListView being another pair of elements, but I'm not sure how well that works for the 4 element wide rows.
The XML of each row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/statNameString"
android:id="#+id/statName"
android:layout_marginTop="30dp"
android:gravity="center" />
<View
android:layout_width="4dp"
android:gravity="left"
android:layout_height="match_parent"
android:background="#663300"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/statValString"
android:id="#+id/statVal"
android:layout_marginTop="30dp"
android:gravity="center" />
</LinearLayout>
The top row(which represented the Class of the Character in my app) needed to include an ImageView, so it had a slightly changed layout. The ImageView was placed after the bar View, and before the last TextView.
The XML including each row into the final layout, for the Land layout:
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
layout="#layout/horizontal_class_row_layout"
android:id="#+id/strValues"
android:layout_column="0" />
<include
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
layout="#layout/horizontal_class_row_layout"
android:id="#+id/intValues"
android:layout_column="0" />
</TableRow>
If you only wanted 1 pair of elements in each row, you can simply remove one of the include tags. As mentioned before, you could also use the ListView to do these, and fill the data, and I will probably modify it to do that eventually.
Finally, you programmatically fill the elements with their data in the Java, so they don't just say "Class" and "Class Value" or whatever default data:
private void fillViewValues() {
//Gather views
LinearLayout llClass = (LinearLayout)findViewById(R.id.classValues);
LinearLayout llStr = (LinearLayout)findViewById(R.id.strValues);
LinearLayout llDex = (LinearLayout)findViewById(R.id.dexValues);
LinearLayout llCon = (LinearLayout)findViewById(R.id.conValues);
LinearLayout llInt = (LinearLayout)findViewById(R.id.intValues);
LinearLayout llWis = (LinearLayout)findViewById(R.id.wisValues);
LinearLayout llCha = (LinearLayout)findViewById(R.id.chaValues);
//Get the layout's locations
TextView classNameView = (TextView) llClass.findViewById(R.id.statName);
TextView classValView = (TextView) llClass.findViewById(R.id.statVal);
ImageView classImage = (ImageView) llClass.findViewById(R.id.classicon);
TextView strNameView = (TextView) llStr.findViewById(R.id.statName);
TextView strValView = (TextView) llStr.findViewById(R.id.statVal);
TextView dexNameView = (TextView) llDex.findViewById(R.id.statName);
TextView dexValView = (TextView) llDex.findViewById(R.id.statVal);
TextView conNameView = (TextView) llCon.findViewById(R.id.statName);
TextView conValView = (TextView) llCon.findViewById(R.id.statVal);
TextView intNameView = (TextView) llInt.findViewById(R.id.statName);
TextView intValView = (TextView) llInt.findViewById(R.id.statVal);
TextView wisNameView = (TextView) llWis.findViewById(R.id.statName);
TextView wisValView = (TextView) llWis.findViewById(R.id.statVal);
TextView chaNameView = (TextView) llCha.findViewById(R.id.statName);
TextView chaValView = (TextView) llCha.findViewById(R.id.statVal);
//Fill those values
//Names of sections
classNameView.setText(getResources().getString(R.string.classString));
strNameView.setText(getResources().getString(R.string.strString));
dexNameView.setText(getResources().getString(R.string.dexString));
conNameView.setText(getResources().getString(R.string.conString));
intNameView.setText(getResources().getString(R.string.intString));
wisNameView.setText(getResources().getString(R.string.wisString));
chaNameView.setText(getResources().getString(R.string.chaString));
//Values
classValView.setText(classString);
setImage(classImage, classString);
strValView.setText(String.format("%d", finalStats[0]));
dexValView.setText(String.format("%d", finalStats[1]));
conValView.setText(String.format("%d", finalStats[2]));
intValView.setText(String.format("%d", finalStats[3]));
wisValView.setText(String.format("%d", finalStats[4]));
chaValView.setText(String.format("%d", finalStats[5]));
}
This follows the advice at at this StackOverflow post.
One thing I would like to note, is that when you change the layout, you need to reset the content view to your current layout. In my case, the function looked like:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
setContentView(R.layout.activity_see_character);
fillViewValues();
}else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
setContentView(R.layout.activity_see_character);
fillViewValues();
}
}
where fillViewValues() is the previous function. I believe that this sets the layout to either the portrait or landscape version of the layout, which can then have its elements accessed. If you don't do this when you try to access the elements it can't find them and gives a NullPointerException.
I'm working in an Android application and I want to add a TableRow programmatically in my TableLayout.
I have this TableLayout:
<TableLayout
android:id="#+id/details_table"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView
android:text="4686"
android:layout_width="wrap_content"
android:layout_column="0"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
<TextView
android:text="sdhiuf osdfh isdhf ihdf"
android:layout_width="wrap_content"
android:layout_column="1"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
<TextView
android:text="2"
android:layout_width="wrap_content"
android:layout_column="2"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
<TextView
android:text="UN"
android:layout_width="wrap_content"
android:layout_column="3"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
</TableRow>
and I want to add this exactly TableRow programmatically.
I'm trying something like this:
TableLayout detailsTable = (TableLayout) l.findViewById(R.id.details_table);
for(Nfce_Product nfceProduct : nfceProducts){
TableRow tableRow = new TableRow(getActivity());
TextView tvProductCode = new TextView(getActivity());
tvProductCode.setLayoutParams(new TableRow.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
tvProductCode.setText(nfceProduct.getProduct_code());
tvProductCode.setTextSize(TypedValue.COMPLEX_UNIT_PX, productDetailsTextSize);
tvProductCode.setTextColor(getResources().getColor(R.color.black));
TextView tvProductDescription = new TextView(getActivity());
tvProductDescription.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
tvProductDescription.setText(nfceProduct.getProduct_description());
tvProductDescription.setTextSize(TypedValue.COMPLEX_UNIT_PX, productDetailsTextSize);
tvProductDescription.setTextColor(getResources().getColor(R.color.black));
TextView tvProductAmount = new TextView(getActivity());
tvProductAmount.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
tvProductAmount.setText(String.valueOf(nfceProduct.getAmount()));
tvProductAmount.setTextSize(TypedValue.COMPLEX_UNIT_PX, productDetailsTextSize);
tvProductAmount.setTextColor(getResources().getColor(R.color.black));
TextView tvProductMetric = new TextView(getActivity());
tvProductMetric.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
tvProductMetric.setText(nfceProduct.getProduct_metric());
tvProductMetric.setTextSize(TypedValue.COMPLEX_UNIT_PX, productDetailsTextSize);
tvProductMetric.setTextColor(getResources().getColor(R.color.black));
tableRow.addView(tvProductCode);
tableRow.addView(tvProductDescription);
tableRow.addView(tvProductAmount);
tableRow.addView(tvProductMetric);
detailsTable.addView(tableRow);
}
Here is my own answer to my own dynamically created TableRow question. I think my answer is detailed enough, you should have no problems taking it as your own! My issue involved not only dynamically creating TableRows, but also being able to touch each row and have something happen. I've gone further than that nowadays to make each CELL separately clickable.
Edit: You need to have a separate TableRow XML file that you can access, separate from the actual TableLayout you're trying to dynamically populate.
Edit2: I should probably try to make an actual solution for you, so that you see what I'm talking about:
First, create (or keep) your XML file containing your TableLayout. Second, Create a separate TableRow XML file.
(This is your potential tablerow.xml file)
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:focusable="true" >
<TextView
android:id="#+id/tableCell1"
android:layout_width="wrap_content"
android:layout_column="0"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
<TextView
android:id="#+id/tableCell2"
android:layout_width="wrap_content"
android:layout_column="1"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
<TextView
android:id="#+id/tableCell3"
android:layout_width="wrap_content"
android:layout_column="2"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
<TextView
android:id="#+id/tableCell4"
android:layout_width="wrap_content"
android:layout_column="3"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
</TableRow>
Now... back to your actual code!
for (Nfce_Product nfceProduct : nfceProducts) {
final TableLayout detailsTable = (TableLayout) findViewById(R.id.details_table);
final TableRow tableRow = (TableRow) getLayoutInflater().inflate(R.layout.tablerow, null);
TextView tv;
//Filling in cells
tv = (TextView) tableRow.findViewById(R.id.tableCell1);
tv.setText(nfceProduct.getProduct_code());
tv = (TextView) tableRow.findViewById(R.id.tableCell2);
tv.setText(nfceProduct.getProduct_description());
tv = (TextView) tableRow.findViewById(R.id.tableCell3);
tv.setText(nfceProduct.getAmount());
tv = (TextView) tableRow.findViewById(R.id.tableCell4);
tv.setText(nfceProduct.getProduct_metric());
//Add row to the table
detailsTable.addView(tableRow);
} //End for
If you still need/want to change the text size and color, you can do that after the setText() line and before you do findViewById again. If you want to have column headers that basically say Code, Description, Amount, and Metric, make a TableRow inside the TableLayout like you have currently. The TableRows created programmatically will fall in line after that "header" row.
i am trying to align multiple (variable) textviews in one line. It should look like this:
- Textview1 | Textview2 | TextView3
Since the textviews vary regarding length I am having a problem aligning them properly(the only thing I know is that Textview1 will be the shortest one).
Here is my code (it is placed inside a Linear Layout):
<RelativeLayout
android:id="#+id/persondetails_names_horizontal_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right" >
<TextView
android:id="#+id/persondetails_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingTop="3dp"
android:text=" Title "
android:layout_alignParentRight="true" />
<TextView
android:id="#+id/persondetails_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Name "
android:textSize="#dimen/font_big"
android:textStyle="bold"
android:layout_toEndOf="#id/persondetails_title"
android:layout_toRightOf="#id/persondetails_title" />
<TextView
android:id="#+id/persondetails_surname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/persondetails_name"
android:layout_toRightOf="#id/persondetails_name"
android:text=" Surname " />
</RelativeLayout>
Now, as long as these 3 Textviews fill out one line on the screen everything is OK, but as soon as one of them gets bigger I get some problems. For example, when I put the surname field very long, it pushes the other 2 views out of the screen (they are not visible) and takes all the space (but just one line!).
What I want is that these views are aligned to the right side, each of them to the right of the previous one, and when it is needed to linebreak into a new line (no mather which textview it is) and in the new line the following textviews should align to the right of it.
So what do I need to change in my code, so that these Textviews are aligned next to each other, and moved properly when one of them is getting bigger ?
Thank you
This is how I implemented Elltz suggestions:
<LinearLayout
android:id="#+id/persondetails_namen_horizontal_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="#+id/persondetails_wert_titel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" />
<TextView
android:id="#+id/persondetails_wert_vorname"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_below="#id/persondetails_text_vorname"
android:gravity="right"
android:layout_gravity="right"
android:text=" "
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:overScrollMode="always"
android:scrollHorizontally="true"
android:singleLine="true"/>
<TextView
android:id="#+id/persondetails_wert_nachname"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_below="#id/persondetails_text_vorname"
android:layout_gravity="right"
android:text=" "
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:overScrollMode="always"
android:scrollHorizontally="true"
android:singleLine="true"/>
</LinearLayout>
But still not working. Actually I removed the "singleLine=true" because in case when one of the 3 textviews gets too long I need it break (don't have a problem with breaking).
Regarding breaking, I want every textview to take as muche space as it needs, the only thing is that all the other views shoud be moved properly.
The property "single line" cannot be right because it is possible that one of these views gets too long for one line, but I have tested it with single line and without:
a) with "singleline=true"
b) without "singleline=true"
In bothe cases I get a wrong output, but case 2 is closer to what I want. But if you look at it you will see that textview2 is being pushed and it breakes in its bounds but not on the entire lenght. Textview 3 takes the most space. But what i want is that every view is fully streched and if needed breaked into new line (textview1 is this "Mag.")
This is how I would like it to look like:
Change the relative layout to a linear layout and then set the orientation to horizontal something like this.
<LinearLayout:id="#+id/persondetails_names_horizontal_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<TextView
android:id="#+id/persondetails_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:text=" Title "
/>
<TextView
android:id="#+id/persondetails_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Name "
android:textSize="#dimen/font_big"
android:textStyle="bold"
/>
<TextView
android:id="#+id/persondetails_surname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Surname " />
</LinearLayout>
You should try and do a LinearLayout and weight each element.
<LinearLayout
android:id="#+id/persondetails_names_horizontal_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right" >
<TextView
android:id="#+id/persondetails_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingTop="3dp"
android:text=" Title "
android:layout_weight= "1"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/persondetails_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Name "
android:textSize="#dimen/font_big"
android:textStyle="bold"
android:layout_weight= "1"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/persondetails_surname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight= "1"
android:layout_centerVertical="true"
android:text=" Surname " />
</LinearLayout>
To answer why your code line is giving you that problems is a RelativeLayout positions its Views relative to each other, a View will need another View to be layed out if not the parent itself. So TextView 1 is positioned to the left of TextView 2 when TextView two gets bigger since its to the left it will be to the left but not on screen layout or space get it?
To answer you
TextView 1 will be the shortest Using the LinearLayout Approach with weightsum and weight tweak it this way first set the weightsum to two on the LinearLayout and the two i am going to be long TextViews give them a width of 0dp and weight 1 each the shortest TextView will have wrap_content as width and that's all life's is good. now the two long TextViews will calculate their sizes and the rest will be left for the shorter one. I do not not specifically how you want your TextView to long in terms of straight horizontal line but after you have done that you can add these elements to your TextView in xml or you could do it in java to ensure your straight lined TextViews
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:overScrollMode="always"
android:scrollHorizontally="true"
android:singleLine="true"
EDIT
this is what i mean
int width = getWindow().getDecorView().getMeasuredWidth();
//i am getting the overal width of the screen
Toast.makeText(getApplicationContext(), String.valueOf(width), Toast.LENGTH_SHORT).show();//toast it
for(int i =0; i < ((LinearLayout)findViewById(R.id.l)).getChildCount(); i++){
//looping through the parent container
TextView child = (TextView) ((LinearLayout)findViewById(R.id.l)).getChildAt(i);
child.setSelected(true);
child.setEllipsize(TruncateAt.MARQUEE);
child.setHorizontallyScrolling(true);
child.setSingleLine(true);
if(i+1 == 3){ //the last textview
child.setLayoutParams(new LinearLayout.LayoutParams(
((LinearLayout)findViewById(R.id.l)).getChildAt(1).getRight(),
LinearLayout.LayoutParams.WRAP_CONTENT));
}else{ // the first two take 1/3 of the width of the screen
child.setLayoutParams(new LinearLayout.LayoutParams(width/3, LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
}
Hope it helps
I need to create a dinamic table which will be fit with rows of data. The rows I want will have an specific form, like this:
So, to get it, I used relativeLayouts. The problem is that I first tried it with an .xml to see what parameters will have the differents views that it forms, and I get it, but now I cant replicate it in the code.
Here is my code:
TableLayout linkRoutesTable = (TableLayout) findViewById(R.id.layoutLinkRoutes);
//ROW:
TableRow linkRouteRow = new TableRow(this);
linkRouteRow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linkRouteRow.setId(i)
//RELATIVELAYOUT:
float px0 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, r.getDisplayMetrics());
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(0, (int)px0)));
//CONTENT:
// BUTTON: ...
// NUMBER:...
// ADDRESS:...
// NAME: ...
relativeLayout.addView(number);
relativeLayout.addView(name);
relativeLayout.addView(address);
relativeLayout.addView(button);
linkRouteRow.addView(relativeLayout);
linkRouteTable.addView(linkRouteRow);
linkRoutesTable.refreshDrawableState();
It doesn't show anything...
The .xml show ok, here it is:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" android:orientation="vertical">
<TableRow>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_weight="1"
android:id="#+id/layoutLinkRoutes" >
<Button android:text="Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/button1" android:layout_alignParentTop="true" android:layout_alignParentRight="true"></Button>
<TextView android:textStyle="bold" android:textSize="45dp" android:text="1" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_marginLeft="22dp"></TextView>
<TextView android:text="TextView" android:id="#+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="#+id/button1" android:layout_toRightOf="#+id/textView1" android:layout_marginLeft="16dp"></TextView>
<TextView android:text="TextView" android:id="#+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentTop="true" android:layout_alignLeft="#+id/textView3"></TextView>
</RelativeLayout>
</TableRow>
What Am I doing wrong in the code? I need some help, I tried everything.
If I dont use the relativeLayouts and the rows, it shows something but bad... so the problem is the rows and the relativelayout
Really thanks...
You don't have to define the layout in code to fill a table dynamically. You can infalte a layout which you define in xml like a normal layout.
So you define your table row layout one time in a xml file and infalte it multiple times into your table layout.
Check this answer.