Stretching columns on TableLayout has no effect - android

I am working on an android project and I am making use of the table layout but I am experiencing an issue.
As mentioned in the title, I am using a TableLayout and trying to stretch all of the columns to fit the screen but for some reason it has no affect, and all the columns are squished over to the left.
Below is the XML layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="horizontal">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout android:id="#+id/resultTable"
android:stretchColumns="*"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TableLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
Below is how I am populating the TableLayout
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
try
{
Log.d("Returned in Result", String.valueOf(result.length()));
resultTable.removeAllViews();
resultTable.setStretchAllColumns(true);
for (int i = 0; i < result.length(); i++)
{
TableRow tr = new TableRow(getActivity());
JSONArray array = result.getJSONArray(i);
Log.d("Returned in Result", "Loaded Columns: " + String.valueOf(array.length()));
for (int j = 0; j < array.length(); j++)
{
//LinearLayout linearLayout = new LinearLayout(getActivity());
//LayoutParams parms = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//linearLayout.setLayoutParams(parms);
TextView textView = new TextView(getActivity());
textView.setText(array.getString(j));
textView.setSingleLine();
//linearLayout.addView(textView);
tr.addView(textView);
}
resultTable.addView(tr);
}
}
catch (Exception ex)
{
Log.e("Load Result", ex.toString());
CrashReporter.ReportCrash(ex, Severity.Critical);
}
}
});
Thanks for any help you can provide.
UPDATE
I've found it the HorizontalScrollView that is causing the columns to not stretch. If I remeove the horizontal scroll view it works fine but with the horizontal scroll view the columns don't stretch. How can I get this working as I need to have the horizontal scroll view.

I might be wrong but I had something similar , I didn't had any table layout but I wanted to have a scroll view inside a relative layout so I had to do something like ,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/theme"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="34dp" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
This mighy not be of any use but this is what made my "columns are squished over to the left." problem solved.
EDIT: Try having your Linear Layout inside Scroll view.

Related

Horizontal Scroll View(Image View)

I have a Horizonal scroll view with Images. Then the output is this.
I loaded 2 same pictures but the Gap is too much.
I just want it to make like 5Margins on left but didn't get the result.
Here's my Code.
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginBottom="1dp"
android:background="#FFF"
android:scrollbars="none"
>
<LinearLayout
android:id="#+id/imgLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
>
</LinearLayout>
</HorizontalScrollView>
And here is my code of Adding imageviews to linear layout.
public void setImageViews() {
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.imgLayout);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
parms.setMargins(5,5,5,5);
for(int i=0;i<6;i++){
ImageView imageView =new ImageView(this);
imageView.setLayoutParams(parms);
imageView.setImageResource(R.drawable.bsu);
linearLayout.addView(imageView);
}
}
The results i got.
I want it to be 5margins apart.

Top view disappeared in ScrollView

I am adding list of table layouts to a LinearLayout dynamically. For that I used ScrollView to see the list of tables which are added to linear layout.
below is my xml code.
<LinearLayout 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:orientation="vertical"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:background="#color/white"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RET MASTER SITE REPORTS"
android:paddingBottom="5dp"
android:textColor="#android:color/black"
android:textStyle="bold" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical"></LinearLayout>
</ScrollView>
Source code:
layout = (LinearLayout) findViewById(R.id.linearLayout);
for (int i = 0; i < 5; i++) {
if (isColorChange) {
displayDeviceDetails(getColor(R.color.blue), getColor(R.color.lightBlue));
} else {
displayDeviceDetails(getColor(R.color.orange), getColor(R.color.lightOrange));
}
}
//Method which i am calling from loop
private void displayDeviceDetails(int titleColor, int cellColor) {
TableRow row = null;
TableLayout tableLayout = new TableLayout(this);
tableLayout.setPadding(0, 30, 0, 0);
for (int i = 0; i < arr.length; i++) {
row = (TableRow) inflater.inflate(R.layout.child_views, null);
row.setBackgroundColor(titleColor);
TextView tvTitle = (TextView) row.findViewById(R.id.tvRowTitle);
TextView tvText = (TextView) row.findViewById(R.id.tvRowText);
if (i == 0) {
tvText.setVisibility(View.GONE);
tvTitle.setGravity(Gravity.CENTER);
tvTitle.setTextColor(Color.WHITE);
tvTitle.setText(arr[i]);
} else {
changeBgColor(cellColor);
setBgColor(tvTitle);
setBgColor(tvText);
tvTitle.setGravity(Gravity.RIGHT);
tvTitle.setText(arr[i]);
tvText.setText(arr1[i]);
row.setBackgroundColor(titleColor);
}
tableLayout.addView(row);
}
layout.addView(tableLayout);
}
In the above code, There is TextView at top of the screen which is used to display some text. I added 3 tables to a linear layout it's showing 1st table from middle followed by next 2 tables with some white space/empty screen at bottom. If I increase number of tables that bottom screen empty space is increasing.
Example: There are 5 tables in a ScrollView, but it will display from 2nd table that might be from table starting or might be from middle of the table, followed by 3rd, 4th n 5th tables with empty space at bottom of the ScrollView.
I can able to see the Text, but I added 5 tables to linear layout inside ScrollView, when I executed that, it will display from 2nd table that might be from table starting or might be from middle, followed by 3rd, 4th n 5th tables with empty space. I have attachment my screen-shots.
Please help me.
Found it ! It's the android:layout_gravity="center_vertical" in the LinearLayout that is the culprit..
Just delete this and everything should be fine.
You need to change the root LinearLayout to RelativeLayout as below:
<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:orientation="vertical"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:background="#color/white"
tools:context=".MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RET MASTER SITE REPORTS"
android:paddingBottom="5dp"
android:textColor="#android:color/black"
android:textStyle="bold" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/textView">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"></LinearLayout>
</ScrollView>
</RelativeLayout>
I hope this helps you

Add a button within a ScrollView

I need some help I have a scrollview that shows some question with radio buttons and I want to add a button when the user reaches the last question and I want this button to be available only after the user has selected an answer to all the questions.
This is my code so far but it is not displaying anything. I can display the questions without the button but I want to display the button too.
String countryName[] = { "India", "Pakistan", "China", "Nepal",
"Bangladesh" };
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
for (int k = 1; k <= 10; k++)
{
//create text button
TextView title = new TextView(this);
title.setText("Question Number:" + k);
title.setTextColor(Color.BLUE);
mLinearLayout.addView(title);
// create radio button
final RadioButton[] rb = new RadioButton[5];
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.VERTICAL);
for (int i = 0; i < 5; i++)
{
rb[i] = new RadioButton(this);
rg.addView(rb[i]);
rb[i].setText(countryName[i]);
}
mLinearLayout.addView(rg);}
ScrollView scroll = new ScrollView(this);
Button btn = new Button(this);
ViewGroup.LayoutParams blp = new ViewGroup.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
btn.setLayoutParams(blp);
btn.setText("Click Me");
mLinearLayout.addView(scroll);
mLinearLayout.addView(btn);
setContentView(mLinearLayout);
}
The XML file:
<!--?xml version="1.0" encoding="utf-8"?-->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/textView3"
android:text="Continue" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</RelativeLayout>
mLinearLayout.addView(btn);
scroll.addView(mLinearLayout);
setContentView(scroll);
You should place your LinearLayout inside the ScrollView, because ScrollView is a container and its children are scrollable. Also, set the ScrollView as the activity's content view and define its layout params.
ScrollView scrollView = new ScrollView(this);
scrollView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
scroll.addView(mLinearLayout);
setContentView(scroll);
Hi I have found the right way to do it! The easiest way is to fix the XML file and add a LinearLayout within the LinearLayout and put the questions within the inner LinearLayout and the button in the outside LinearLayout.
Here is the trick:
<!--?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="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2.87"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2.87"
android:text="Button" />
</LinearLayout>
</ScrollView>

display 2D array in matrix format

I want my 2D array to be displayed as matrix format. For that which layout is suitable? Can I have any example for that?
The simple answer is to use a TableLayout with TextViews inside.
But if you want to display a large array, you should make the matrix scrollable in both directions. Below is the code that populates a matrix that is displayed as a series of EditTexts inside rows, inside a TableLayout. The TableLayout is made scrollable in both directions using two ScrollView. Adapt the code to your needs. table is the TableLayout with the id tableLayout1.
private void fillTable(final int n, final double[][] matrix, TableLayout table) {
table.removeAllViews();
for (int i = 0; i < n; i++) {
TableRow row = new TableRow(MainActivity.this);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < n; j++) {
EditText edit = new EditText(OutputActivity.this);
edit.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_NUMBER_FLAG_SIGNED);
edit.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
edit.setText(Double.toString(matrix[i][j]));
edit.setKeyListener(null);
row.addView(edit);
}
table.addView(row);
}
}
The layout XML:
<?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:layout_gravity="center_vertical"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</TableLayout>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
</LinearLayout>

Adding items to LinearLayout (and resizing)

I have the following layout
<?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="horizontal"
android:background="#f00"
android:id="#+id/main_layout">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#0f0"
android:id="#+id/bed_list"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00f"
android:text="Howdy cowboy?"
android:textSize="50dip"
android:textColor="#fff"
android:id="#+id/textview"/>
</LinearLayout>
I want to add some textviews to R.id.bed_list (as if they were vertical tabs)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
LinearLayout bedList = (LinearLayout) findViewById(R.id.bed_list);
for (int i = 0; i < 12; i++) {
System.out.println("In the loop");
TextView textView = new TextView(this);
textView.setText("HJM-" + i);
textView.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
textView.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
bedList.addView(textView,i,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
So far, the code is run, the System.out.println messages in the loop appear at the console, but I only see the textview (R.id.textview). I wanted the list to appear at the left of the screen with the options calculated, but it does not work.
Further on, if I define a first textview in the label (so that there is some items at the very beginning and a space is reserved), I get to see that textview but the others (added from the program) do not appear either.
Your textview (R.id.textview) has a width of match_parent, effectively pushing your other views (the linearlayout) off screen. Instead you can give it a width of wrap_content and possibly a layout_weight of 1.

Categories

Resources