How to convert rows to columns using tablelayout dynamically in Android - android

I have a tablelayout that gets data dynamically. and I would like to convert the rows to columns. I changed orientation but didnt work.
TableLayout tableLayout = (TableLayout) second_row_b.findViewById(R.id.table_current_picks);
List<PickGame> selectedPicks = buildSelectedPicks(response);
int selectedPicksSize = selectedPicks.size() > 5 ? 5 : selectedPicks.size();
for (int i = 0; i < selectedPicksSize; i++) {
TableRow rowView = (TableRow) inflater.inflate(R.layout.include_current_picks_row, null);
TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, 0, 0.1f);
tableLayout.addView(rowView, rowParams);
PickGame pickGame = selectedPicks.get(i);
TextView totalPointsText = (TextView) rowView.findViewById(R.id.text_total_points);
TextView gamePointsText = (TextView) rowView.findViewById(R.id.text_game_points);
NetworkImageView imageView = (NetworkImageView) rowView.findViewById(R.id.image_team_pick);
and this is my XML
<TableLayout
android:id="#+id/table_current_picks"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:orientation="vertical"
android:layout_weight="1">
</TableLayout>

Related

Spread imageViews evenly over tablerow

Like the title says, I have problems in spreading ImageViews evenly over tableRows. My xml is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ProgressBar/>
<TextView/>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/progressBar">
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
</ScrollView>
After the Activity is started a dynamical Table is created with the following code:
for (int i = 0; i < 4; i++) {
tableRow = new TableRow(this);
tableRow.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tableRow.setId(i + 1);
tableRow.setBackgroundResource(R.mipmap.shelfboard);
tableRow.setPadding(20, 0, 20, 0);
tableRow.setOnClickListener(thisvariable);
tableLayout.addView(tableRow);
}
Now I want to add different numbers of ImageViews of the same width for each tableRow to each tableRow.
I have 4 TableRows
add 2 ImageViews to tableRow 1
add 5 ImageViews to tableRow 2
add 3 ImageViews to tableRow 3
add 7 ImageViews to tableRow 4
My Code is:
for (int i = 0; i < adpList.size(); i++) {
int row = adpList.get(i).getShelfBoard();
if (row==1) {
int productsPerBoard = 2;
int width = (tableLayout.getWidth()) / productsPerBoard;
TableRow.LayoutParams tlp = new TableRow.LayoutParams(width, TableRow.LayoutParams.MATCH_PARENT);
productPicture = new ImageView(this);
r = (TableRow) findViewById(row);
productPicture.setLayoutParams(tlp);
}
if (row==2) {
int productsPerBoard = 5;
int width = (tableLayout.getWidth()) / productsPerBoard;
TableRow.LayoutParams tlp = new TableRow.LayoutParams(width, TableRow.LayoutParams.MATCH_PARENT);
productPicture = new ImageView(this);
productPicture.setBackgroundResource(R.drawable.border);
r = (TableRow) findViewById(row);
productPicture.setLayoutParams(tlp);
}
if (row==3) {
int productsPerBoard = 3;
int width = (tableLayout.getWidth()) / productsPerBoard;
TableRow.LayoutParams tlp = new TableRow.LayoutParams(width, TableRow.LayoutParams.MATCH_PARENT);
productPicture = new ImageView(this);
r = (TableRow) findViewById(row);
productPicture.setLayoutParams(tlp);
}
if (row==4) {
int productsPerBoard = 7;
int width = tableLayout.getWidth() / productsPerBoard;
TableRow.LayoutParams tlp = new TableRow.LayoutParams(width, TableRow.LayoutParams.MATCH_PARENT);
productPicture = new ImageView(this);
r = (TableRow) findViewById(row);
productPicture.setLayoutParams(tlp);
}
productPicture.setBackgroundResource(R.mipmap.ic_launcher);
r.addView(productPicture);
Unfortunately every row has only two pictures. It always takes the width of the first board. My Output:
How can I add different numbers of ImageViews to each row?
I thought too complicated. Changing the LayoutParams solved my problem:
r = (TableRow) findViewById(shelfboard);
productPicture = new ImageView(this);
TableRow.LayoutParams tlp = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT, 1f);
tlp.setMargins(0, 20, 0, 50);
productPicture.setLayoutParams(tlp);

ScrollView is cutting off beginning of text

I followed this piece of code written by Aby in another question: https://stackoverflow.com/a/22682248/5915747
The beginning of my text is being cut off on the left side of my screen. I'm missing quite a bit of info and not sure how to fix it, I have tried everything that worked for others but it has not worked for me.
popup.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollviewtest"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:context="com.info.PopupActivity"
android:background="#f0f0f0"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:id="#+id/rel_layout"
android:orientation="vertical">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/table_layout"
android:background="#80000000"
android:layout_centerHorizontal="true">
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
init method:
public void init() {
View popupview = getLayoutInflater().inflate(R.layout.popup, null);
popupWindow = new PopupWindow(
popupview, LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
popupWindow.showAtLocation(popupview, Gravity.CENTER, 0, 0);
popup_shown[0] = true;
TableLayout tableLayout = (TableLayout) popupview.findViewById(R.id.table_layout);
TableRow row0 = new TableRow(this);
TextView text0 = new TextView(this);
text0.setText("Test Header1");
row0.addView(text0);
TextView text1 = new TextView(this);
text1.setText("Test Header2");
row0.addView(text1);
TextView text2 = new TextView(this);
text2.setText("Teset Header3");
row0.addView(text2);
TextView text3 = new TextView(this);
text3.setText("Test Header4");
row0.addView(text3);
TextView text4 = new TextView(this);
text4.setText("Test Header5");
row0.addView(text4);
tableLayout.addView(row0);
for (int i = 0; i < 8; i++) {
TableRow tableRow = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setGravity(Gravity.CENTER);
tv0.setText("long test field 0");
tableRow.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setGravity(Gravity.CENTER);
tv1.setText("long test field 1");
tableRow.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setGravity(Gravity.CENTER);
tv2.setText("long test field 2");
tableRow.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setGravity(Gravity.CENTER);
tv3.setText("long test field 3");
tableRow.addView(tv3);
TextView tv4 = new TextView(this);
tv4.setGravity(Gravity.CENTER);
tv4.setText("long test field 3");
tableRow.addView(tv4);
tableLayout.addView(tableRow);
}
}
The longer the strings are for the text views, the more it crops off, however it's a horizontal scrollview, so it should just start at the beginning of the screen and add length to the end, which is scrollable, right?
This is what it ends up looking like with the current strings in vertical/portrait orientation:
Vertical View
This is what it looks like in horizontal, it seems to fix since my screen is large enough for the data:
Horizontal View
If you have any questions, please ask!
Solved this by adding padding to the scrollview. It seems this is a common android bug.
I don't know will it work or not but try removing
android:layout_gravity="center"
line from Relative Layout

Android Multi-Line TextView Inside TableView weird Padding

Ok I have a TableView, in which I'm inserting rows programatically. My Table looks following:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:stretchColumns="0,1,2,3,4">
</TableLayout>
</LinearLayout>
</ScrollView>
And I'm adding rows in such way:
public void addRow () {
int rowHeight = 200;
final TableRow tbrow = new TableRow(this);
final TextView t1v = getTextView("10 ");
tbrow.addView(t1v);
t1v.requestLayout();
t1v.getLayoutParams().height = rowHeight;
final TextView t2v = getTextView("3");
tbrow.addView(t2v);
t2v.requestLayout();
t2v.getLayoutParams().height = rowHeight;
final TextView t3v = getTextView("5");
tbrow.addView(t3v);
t3v.requestLayout();
t3v.getLayoutParams().height = rowHeight;
final TextView t4v = getTextView("5");
tbrow.addView(t4v);
t4v.requestLayout();
t4v.getLayoutParams().height = rowHeight;
final TextView t5v = getTextView("from 1005\nto 1009");
tbrow.addView(t5v);
t5v.requestLayout();
t5v.getLayoutParams().height = rowHeight;
tl.addView(tbrow);
}
private TextView getTextView(String text)
{
TextView textView = new TextView(this);
textView.setText(text);
textView.setTextColor(Color.BLACK);
textView.setGravity(Gravity.CENTER);
textView.setBackground(gd3);
return textView;
}
And the problem is now my table looks corrupted:
What I've tried is as suggested in this post to set on my tableLayout:
setColumnShrinkable(4,true);
However that doesn't work.
You help will be highly appreciated.

TableLayout fills parent

i want to add a TabeLayout to a LinearLayout programmatically.
This is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content">
</LinearLayout>
and this is how i add the table in my MainActivity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.content);
String[] content = new String[] {"Hello", "World", "Test"};
float d = getResources().getDisplayMetrics().density;
int dp = (int)(3*d);
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 table = new TableLayout(this);
table.setLayoutParams(tableParams);
for(int i = 0; i < content.length; i++) {
TableRow row = new TableRow(this);
row.setLayoutParams(rowParams);
int length = content[i].length();
TextView textView = new TextView(this);
textView.setLayoutParams(rowParams);
textView.setText(content[i]);
textView.setPadding(dp, 0, dp, 0);
row.setBackgroundColor(Color.BLUE);
row.addView(textView);
table.addView(row);
}
table.setBackgroundColor(Color.GRAY);
linearLayout.addView(table);
}
}
as you can see the tableParams are set to wrap_content. However, when i set the background color of the table i see, that the width of the table is as long as the display. Why is that so and how can i change that?
This is the output:
http://postimg.org/image/e29bsg9vv/
TableLayout deals with width a bit differently.
You need to let the TableLayout know that it can shrink the columns.
Try:
table.setShrinkAllColumns(true);
Take a look at the reference docs for TableLayout. In particular, notice it states the following:
The total width of the table is defined by its parent container.
Also, as a tip, you can replace:
table.setLayoutParams(tableParams);
linearLayout.addView(table);
with:
linearLayout.addView(table, tableParams);

If a ScrollView is the parent, it doesn't show a TableLayout into a RelativeLayout(Android)

I have this xml file:
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout android:id="#+id/conteiner_2"
android:layout_width="500px"
android:layout_height="500px"
android:background="#drawable/gradient_2">
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
And I am trying to create a TableLayout which if I delete the ScrollViews it appears, but when I let it like before the TableLayout isn't show.
This is my code:
TableLayout table = new TableLayout(this);
RelativeLayout.LayoutParams paramsTable = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
TableLayout.LayoutParams paramsRow = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,0,1.0f);
TableRow.LayoutParams paramsCol = new TableRow.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT,1.0f);
for(int row = 0 ; row < rows ; row++){
TableRow row = new TableRow(this);
row.setLayoutParams(paramsRow);
for(int col = 0 ; col < column ; col++){
TextView text = new TextView(this);
text.setLayoutParams(paramsCol);
text.setText(row + "-" + col);
row.addView(text);
}
table.addView(row);
}
tabla.setLayoutParams(paramsTable);
conteiner.addView(table);
Sorry for my poor English, I hope I made myself understood.
<<< EDIT >>>
I have to put another layout inside the RelativeLayout, and it works.
I have to put another layout inside the RelativeLayout, and it works.

Categories

Resources