Android Multi-Line TextView Inside TableView weird Padding - android

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.

Related

How to add Borders to table layout

In my application i am using the following code for display content in table view. following code works fine for me but here it is displaying data with out borders but i want to display data with borders for each row and column also.
Here is the reference code i used:
public void init() {
TableLayout stk = (TableLayout) findViewById(R.id.table_main);
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setText(" Sl.No ");
tv0.setTextColor(Color.WHITE);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Product ");
tv1.setTextColor(Color.WHITE);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(" Unit Price ");
tv2.setTextColor(Color.WHITE);
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(" Stock Remaining ");
tv3.setTextColor(Color.WHITE);
tbrow0.addView(tv3);
stk.addView(tbrow0);
for (int i = 0; i < 25; i++) {
TableRow tbrow = new TableRow(this);
TextView t1v = new TextView(this);
t1v.setText("" + i);
t1v.setTextColor(Color.WHITE);
t1v.setGravity(Gravity.CENTER);
tbrow.addView(t1v);
TextView t2v = new TextView(this);
t2v.setText("Product " + i);
t2v.setTextColor(Color.WHITE);
t2v.setGravity(Gravity.CENTER);
tbrow.addView(t2v);
TextView t3v = new TextView(this);
t3v.setText("Rs." + i);
t3v.setTextColor(Color.WHITE);
t3v.setGravity(Gravity.CENTER);
tbrow.addView(t3v);
TextView t4v = new TextView(this);
t4v.setText("" + i * 15 / 32 * 10);
t4v.setTextColor(Color.WHITE);
t4v.setGravity(Gravity.CENTER);
tbrow.addView(t4v);
stk.addView(tbrow);
}
}
and i called this init() in my oncreate method as below
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
init();
}
finally xml code:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#3d455b"
android:layout_alignParentLeft="true" >
<HorizontalScrollView
android:id="#+id/hscrll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/table_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" >
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
any one help me to add borders to the output.
The easiest way to do is here. You can get a hint from here.
As link says to give background to text view, you can apply it at run time too.

Calculating data of individual cells of EditText type and display result in TextView type in the corresponding cell

I'm developing a basic android app since I'm a beginner. I created a dynamic table with 4 columns as: Serial number, width, length and the area of width and length entered.
But I'm not understanding how to calculate result at the same time and display in the cell of fourth column as the user is entering the data in the respective fields.
Here is the java code that I've come up with so far:
TableLayout stk = (TableLayout) findViewById(R.id.table_main);
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
TableRow.LayoutParams r1 = new TableRow.LayoutParams(180, 50);
tv0.setTextSize(15);
tv0.setText(" Sl.No ");
tv0.setTextColor(Color.BLACK);
tv0.setBackgroundResource(R.drawable.cell_shape);
tv0.setLayoutParams(r1);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setTextSize(15);
tv1.setBackgroundResource(R.drawable.cell_shape);
tv1.setText(" Len ");
//tv1.setPadding(5,5,5,5);
tv1.setTextColor(Color.BLACK);
tv0.setLayoutParams(r1);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setTextSize(15);
tv2.setBackgroundResource(R.drawable.cell_shape);
tv2.setText(" Wid ");
tv2.setTextColor(Color.BLACK);
//tv2.setPadding(10,5,10,5);
tv2.setLayoutParams(r1);
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setTextSize(15);
tv3.setBackgroundResource(R.drawable.cell_shape);
tv3.setText(" Sq Ft ");
//tv3.setPadding(5,5,5,5);
tv3.setTextColor(Color.BLACK);
tv3.setLayoutParams(r1);
tbrow0.addView(tv3);
stk.addView(tbrow0);
for (int i = 1; i <= n; i++) {
TableRow tbrow = new TableRow(this);
TextView t1v = new TextView(this);
t1v.setBackgroundResource(R.drawable.cell_shape);
t1v.setText("" + i);
t1v.setTextSize(15);
t1v.setTextColor(Color.BLACK);
t1v.setGravity(Gravity.CENTER);
tbrow.addView(t1v);
EditText t2v = new EditText(this);
t2v.setBackgroundResource(R.drawable.cell_shape);
//tv2.setHint("");
//t2v.setText("Product " + i);
//t2v.setTextSize(15);
//t2v.setId(i);
t2v.setTextColor(Color.BLACK);
t2v.setGravity(Gravity.CENTER);
t2v.isClickable(true);
tbrow.addView(t2v);
EditText t3v = new EditText(this);
t3v.setBackgroundResource(R.drawable.cell_shape);
//t3v.setText("Rs." + i);
t3v.setTextColor(Color.BLACK);
t3v.setGravity(Gravity.CENTER);
t3v.setId(i+1);
//t3v.setTextSize(15);
tbrow.addView(t3v);
TextView t4v = new TextView(this);
t4v.setBackgroundResource(R.drawable.cell_shape);
t4v.setText(""+calculation);
t4v.setTextColor(Color.BLACK);
t4v.setGravity(Gravity.CENTER);
t4v.setTextSize(15);
tbrow.addView(t4v);
stk.addView(tbrow);
}
here is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff"
android:layout_gravity="center"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<TableLayout
android:layout_margin="9dp"
android:id="#+id/table_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</ScrollView>

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

How to convert rows to columns using tablelayout dynamically in 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>

Create a line with points to draw a "Bus path"

I was trying to create something like this: http://s28.postimg.org/a8zi7d4rx/Stop.jpg
My original idea is to create a CanvasImage and X textviews on the right. Then draw with Canvas some dots.
Now i'm stuck at the beginnig; I can't add 2 textview dynamically, 1 below the first.
Here is my code and the screenshot of the result.
What should i do?
Does someone knows if there is any complete example?
Thanks!
public class MainActivity extends Activity {
final int N = 10; // total number of textviews to add
final TextView[] myTextViews = new TextView[N]; // create an empty array;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout l = (RelativeLayout) findViewById(R.id.lLayout);
//fist txtview
final TextView rowTextView1 = new TextView(this);
rowTextView1.setText("This is the first row");
RelativeLayout.LayoutParams p1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
p1.addRule(RelativeLayout.RIGHT_OF, findViewById(R.id.imageView1).getId());
l.refreshDrawableState();
l.addView(rowTextView1,p1);
l.requestLayout();
myTextViews[0] = rowTextView1;
for (int i = 1; i < N; i++) {
final TextView rowTextView = new TextView(this);
rowTextView.setText("This is row #" + i);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.RIGHT_OF, findViewById(R.id.imageView1).getId());
//p.addRule(RelativeLayout.ALIGN_BOTTOM, myTextViews[i-1].getId());
p.addRule(RelativeLayout.BELOW, rowTextView1.getId());
p.setMargins(10, 10, 10, 10);
l.addView(rowTextView, p);
l.refreshDrawableState();
myTextViews[i] = rowTextView;
}
}
}
<RelativeLayout 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"
tools:context=".MainActivity" >
<RelativeLayout
android:id="#+id/lLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#AEEEEE"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="10dp"
android:layout_height="fill_parent"
android:background="#000000"
android:contentDescription="Base" />
</RelativeLayout>
and the result is here: http://s18.postimg.org/e1z3yvlpl/Result.png

Categories

Resources