How to stretch programmatically created columns with HorzontalScroll View - android

Hello I been looking around for an answer but for some reason most of the answers that i find do not help me. I have created a tablelayout on my fragment and I am adding a simple tablerow with a text view programmatically. Currently there are 5 or more columns which seem to be packed into the left corner Is there any way to stretch accross the view? thank you for you help!
Update: removing the Horizontal view fixes the problem, however is there a way to make the table stretch with horizontal scrolling? or would I have to manually add a width for each cell?
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#3d455b">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true" >
<HorizontalScrollView
android:id="#+id/hscrll1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/entries"
android:stretchColumns="*">
<TableRow android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/row_cell">
</TableRow>
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
</RelativeLayout>
JAVA:
valuesTable.removeAllViews();
//add header
TableRow tbrow0 = new TableRow(ctx);
tbrow0.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
TextView tv0 = new TextView(ctx);
tv0.setText("Id");
tv0.setTextSize(20);
tv0.setTextColor(Color.WHITE);
tv0.setPadding(10, 10, 10, 10);
tbrow0.addView(tv0);
TextView tv0 = new TextView(ctx);
tv0.setText("name");
tv0.setTextSize(20);
tv0.setTextColor(Color.WHITE);
tv0.setPadding(10, 10, 10, 10);
tbrow0.addView(tv0);
TextView tv0 = new TextView(ctx);
tv0.setText("color");
tv0.setTextSize(20);
tv0.setTextColor(Color.WHITE);
tv0.setPadding(10, 10, 10, 10);
tbrow0.addView(tv0);
valuesTable.addView(tbrow0);
//Add row information
TableRow tbrow = new TableRow(ctx);
tbrow.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
for(String value : row.getValues()){
TextView t1v = new TextView(ctx);
t1v.setText(value);
t1v.setTextSize(17);
t1v.setTextColor(getResources().getColor(R.color.darkgray));
t1v.setPadding(10, 10, 10, 10);
t1v.setGravity(Gravity.CENTER);
tbrow.addView(t1v);
}
valuesTable.addView(tbrow);

Related

Filling a container with text area

My main activity window is looking really ugly. Since i tried and worked over a tutorial now my final part is to set the size to be full of the screen (portrait/landscape)
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="521dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/spinner">
<HorizontalScrollView
android:id="#+id/hscrll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:orientation="vertical">
<TableLayout
android:id="#+id/table_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:stretchColumns="0">
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
Adding rows in this table layout is going dynamically and the code is shown bellow :
// border
GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.WHITE); // Changes this drawbale to use a single
color instead of a gradient
gd.setCornerRadius(5);
gd.setStroke(1, 0xFF000000);
// draw table header
final TableLayout stk = (TableLayout) findViewById(R.id.table_main);
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tbrow0.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
tv0.setText(" Type ");
tv0.setPadding(5, 15, 0, 15);
tv0.setTextColor(Color.BLACK);
tv0.setBackground(gd);
tv0.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.textsize));
tv0.setGravity(Gravity.CENTER);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Year ");
tv1.setPadding(5, 15, 0, 15);
tv1.setTextColor(Color.BLACK);
tv1.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.textsize));
tv1.setGravity(Gravity.CENTER);
tv1.setBackground(gd);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setPadding(5, 15, 0, 15);
tv2.setText(" Month ");
tv2.setTextColor(Color.BLACK);
tv2.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.textsize));
tv2.setGravity(Gravity.CENTER);
tv2.setBackground(gd);
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setPadding(5, 15, 0, 15);
tv3.setText(" Week ");
tv3.setTextColor(Color.BLACK);
tv3.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.textsize));
tv3.setGravity(Gravity.CENTER);
tv3.setBackground(gd);
tbrow0.addView(tv3);
TextView tv4 = new TextView(this);
tv4.setPadding(5, 15, 0, 15);
tv4.setText(" Amount ");
tv4.setTextColor(Color.BLACK);
tv4.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.textsize));
tv4.setGravity(Gravity.CENTER);
tv4.setBackground(gd);
tbrow0.addView(tv4);
stk.addView(tbrow0);
Even its so much code the control still looks like wrong placed like shown on picture bellow :
Can anyone take a look on my main activity and tell me what am i doing wrong here.
The final output should be the table well placed from edge to edge with same row width and height size
Just by changing activity to this solved the issue
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="613dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/spinner"
tools:layout_editor_absoluteX="0dp">
<TableLayout
android:id="#+id/table_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:stretchColumns="*"></TableLayout>
</ScrollView>

How to Left Align TableRow objects?

They currently appear on the center.
Android Java Code
public void populateTable() {
for(DoctorBean post: beanPostArrayList){
String result = "";
TableRow row = new TableRow(getActivity());
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
ImageView docImageView = new ImageView(getActivity());
docImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
docImageView.setPadding(5, 5, 5, 5);
docImageView.setImageResource(R.drawable.ic_doctor);
TextView textView = new TextView(getActivity());
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
textView.setPadding(5, 5, 5, 5);
result += "\n" + post.getHeroName();
textView.setGravity(Gravity.LEFT);
docImageView.setForegroundGravity(Gravity.LEFT);
row.setGravity(Gravity.LEFT);
textView.setText(result);
row.addView(docImageView);
row.addView(textView);
tableLayout.addView(row);
}
}
Layout Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.gms.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="229dp"
class="com.google.android.gms.maps.SupportMapFragment" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="10dp"
android:shrinkColumns="*"
android:stretchColumns="*">
</TableLayout>
</ScrollView></LinearLayout>
So there I want to align the ImageView and the TextView to the left.
An alternative approach should solve your issue
You don't need to add explicit scroll for a TableView
TableView can be directly added to your main LinearLayout. As you have already specified the height of MapView
Suggested Modified 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">
<com.google.android.gms.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="229dp"
class="com.google.android.gms.maps.SupportMapFragment" />
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
</LinearLayout>
Corresponding code change
LinearLayout linearLayout=(LinearLayout)view.findViewById(R.id.mainLL);
//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.tableLayout);
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("Content left");
nameHead.setLayoutParams(textViewParam);
nameHead.setGravity(Gravity.LEFT);//or Gravity.CENTER according to your requirement
trHead.addView(nameHead);
TextView detailHead = new TextView(context);
detailHead.setText("Content right");
detailHead.setGravity(Gravity.RIGHT);//or Gravity.CENTER according to your requirement
detailHead.setLayoutParams(textViewParam);
trHead.addView(detailHead);
tableLayout.addView(trHead);
//add table layout to linear layout
linearLayout.add(tableLayout);
NOTE:
Many values have been referred from resource files, You can either
neglect/replicate those. I have added two textviews. You may need to
change according to your requirement

Scroll through tabellayout doesn't work in Android

I'm trying to make my tablelayout which is nested in a relativeview scrollable.
Tried a couple of tutorials but nothing worked. Here is the xml and my java code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/white" android:padding="10px"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/textView1"
android:paddingBottom="10dp" android:text="Mijn Rooster"
android:textSize="20sp" android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/tablelayout"></TextView>
<ScrollView android:id="#+id/ScrollView01"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TableLayout android:id="#+id/tablelayout"
android:layout_width=
"fill_parent" android:layout_height="wrap_content"
android:stretchColumns="0" android:layout_below="#+id/textView1"
android:layout_above="#+id/btnsearch">
</TableLayout>
</ScrollView>
<Button android:id="#+id/btnsearch" android:layout_width="wrap_content"
android:text="Zoek op Datum" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"></Button>
</RelativeLayout>
And here is the JAVA code:
TableLayout tl = (TableLayout) findViewById(R.id.tablelayout);
if (date_selected == null) {
for (int i = 0; i < roostermap.size(); i++) {
TableRow trdaydatetime = new TableRow(this);
TableRow trinfo = new TableRow(this);
trdaydatetime.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TableRow.LayoutParams rowSpanLayout = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
rowSpanLayout.span = 3;
TextView txtDay = new TextView(this);
TextView txtTime = new TextView(this);
TextView txtResult = new TextView(this);
// Set Text
txtDay.setText(roostermap.get(i).getDay(
roostermap.get(i).getRoster_date().getDay(),
roostermap.get(i).getRoster_date())
+ " " + df.format(roostermap.get(i).getRoster_date()));
txtTime.setText(dft.format(roostermap.get(i).getRoster_start())
+ " - " + dft.format(roostermap.get(i).getRoster_end()));
txtResult.setText(roostermap.get(i).getWorkplace_name());
// Day&Date Layout
txtDay.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
txtDay.setPadding(5, 5, 0, 0);
txtDay.setTextColor(Color.BLACK);
// Text Time layout
txtTime.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
txtTime.setTextColor(Color.BLACK);
txtTime.setPadding(0, 5, 10, 0);
txtTime.setGravity(Gravity.RIGHT);
// Text Result layout
txtResult.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
txtResult.setPadding(5, 0, 0, 5);
trdaydatetime.addView(txtDay);
trdaydatetime.setBackgroundResource(R.drawable.trup);
trdaydatetime.addView(txtTime);
trinfo.addView(txtResult, rowSpanLayout);
trinfo.setBackgroundResource(R.drawable.trdown);
tl.addView(trdaydatetime, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(trinfo, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
In order for the 'scrolling' to work you need the content of the ScrollView to be larger than the ScrollView itself. Since you have set the android:layout_height value to wrap_content, the height of the ScrollView matches the height of its content - which means no scrolling.
Fix the android:layout_height value of the ScrollView by either setting it to fill_parent (possibly with android:layout_weight) or a pixel value.
Eliminate the ScrollView. You cannot combine two Views which are both scrollable, especially if the scroll in the same direction.
A TableView itself handles scrolling if the content is larger then the height of the TableView.

Setting the layout_weight of the TextView under the TableRow

This question is actually related to this post Set the layout weight of a TextView programmatically
Based on the answers I just need to set the TextView layout params as follow and set the stretchColumn Property, but by adding the following code to mine, it makes the textView disappear from the Table Layout.
TextView tv = new TextView(v.getContext());
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
So, here is my code where I want to dynamically add a row with 3 columns with 35%, 5% and 60% weight. Please let me know what is wrong with my code.
private void addTableRow(int resIdTitle, String strValue){
TableRow tr = new TableRow(this);
// Add First Column
TextView tvTitle = new TextView(this);
tvTitle.setText(resIdTitle);
tvTitle.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0.35f));
tr.addView(tvTitle);
// Add 2nd Column
TextView tvColon = new TextView(this);
tvColon.setText(" : ");
tvColon.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0.05f));
tr.addView(tvColon);
// Add the 3rd Column
TextView tvValue = new TextView(this);
tvValue.setText(strValue);
tvValue.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0.60f));
tr.addView(tvValue);
// Finally add it to the table
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
Log.d(TAG, "Row Added");
}
And here is my xml file,
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<TableLayout
android:id="#+id/tl_cam_get_param"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:title="#string/strGetParamTitle"
android:scrollbars="vertical"
android:shrinkColumns="0">
</TableLayout>
</ScrollView>
I have also tried setting the layout_width to 0, but still it didn't work.
Thanks,
artsylar
Thank you everyone!
Finally, I was able to solve my problem by referencing to the following posts.
android: two issues using Tablerow+TextView in Tablelayout
How to make a TableLayout from XML using programmable way ?
Refer below for my working code.
This is where the dynamic adding of rows is done.
TableLayout tl = (TableLayout)findViewById(R.id.table_layout);
private void addTableRow(int resIdTitle, String strValue){
LayoutInflater inflater = getLayoutInflater();
TableRow tr = (TableRow)inflater.inflate(R.layout.table_row, tl, false);
// Add First Column
TextView tvTitle = (TextView)tr.findViewById(R.id.tvTitle);
tvTitle.setText(resIdTitle);
// Add the 3rd Column
TextView tvValue = (TextView)tr.findViewById(R.id.tvValue);
tvValue.setText(strValue);
tl.addView(tr);
}
This is the table_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<TableLayout
android:id="#+id/table_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:title="#string/strGetParamTitle"
android:scrollbars="vertical"
android:shrinkColumns="0">
</TableLayout>
</ScrollView>
And finally the table_row.xml. I've set all the layout_params here.
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/tvTitle"
android:paddingRight="3dip"
android:layout_width="0px"
android:layout_weight="0.35"/>
<TextView android:text=":"
android:layout_width="0px"
android:layout_weight="0.05"/>
<TextView
android:id="#+id/tvValue"
android:paddingLeft="3dip"
android:layout_width="0px"
android:layout_weight="0.6"
/>
</TableRow>

How to programmatically add multiple LinearLayouts into one view and then add to ViewFlipper?

I hope question title gives you a good description of the problem.
I want to create this XML, but programatically (please don't suggest not doing it programmatically ^_^ )
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:text="Messages:"/>
<EditText android:id="#+id/messageHistory"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="false"
android:layout_weight="1"
android:editable="false"
android:gravity="top"
android:scrollbars="vertical"
android:scrollbarSize="10px"
/>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4">
<EditText android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:layout_weight="1"
/>
<Button android:id="#+id/sendMessageButton"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4"
android:text="Send"/>
</LinearLayout>
As you can see, there are two LinearLayout's, one embedded in the other. I need to reproduce this and then add it to a ViewFlipper.
This is what I have so far:
LinearLayout l1 = new LinearLayout(this);
LinearLayout l2 = new LinearLayout(this);
Button btn=new Button(this);
EditText messageHistory = new EditText(this);
EditText newMessage = new EditText(this);
TextView windowTitle = new TextView(this);
btn.setText("Send");
btn.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 4f));
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Clicked send in child index: "+ flipper.getDisplayedChild(), Toast.LENGTH_SHORT).show();
}
});
windowTitle.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
windowTitle.setPadding(0, 5, 0, 10);
windowTitle.setText("Chat with: ");
messageHistory.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
messageHistory.setGravity(Gravity.TOP);
messageHistory.setMovementMethod(new ScrollingMovementMethod());
messageHistory.setClickable(false);
messageHistory.setFocusable(false);
messageHistory.setPadding(0, 0, 0, 5);
newMessage.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f));
newMessage.setGravity(Gravity.TOP);
newMessage.setMovementMethod(new ScrollingMovementMethod());
newMessage.requestFocus();
l1.setOrientation(LinearLayout.VERTICAL);
l1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
l2.setOrientation(LinearLayout.HORIZONTAL);
l2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
l1.addView(windowTitle);
l1.addView(messageHistory);
l2.addView(newMessage);
l2.addView(btn);
l1.addView(l2);
flipper.addView(l1);
Where flipper is defined as:
ViewFlipper flipper = (ViewFlipper) findViewById(R.id.viewflip);
Without a problem, I can see "l1" in the window when it loads up, but l2 is no where to be found. Did I mess up with LayoutParams? Can I use addView to add a LinearLayout?
I think you forgot to set layout weight (only my opinion though, could be wrong).
because you are adding more views with layout-height set to FILL_PARENT

Categories

Resources