0

in my app I have an activity that shows a table with data.

The problem is that I can not adjust the width of the table to the full width of the screen. In smartphone the width of the table takes more than the width of the device so I use a scroll, while in a tablet the table takes less than the width of it, so I want to fit the total width.

Use the following code in the xml:

<?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"
    android:orientation="vertical"
    android:background="@color/fondo_pantalla" >

<TextView
    android:id="@+id/Title"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:layout_alignParentTop="true"
    android:text="@string/city_Vld"
    android:textColor="@color/blanco"
    android:gravity="center"
    android:background="@color/color_Vld" />

<TextView
    android:id="@+id/Title_O"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/Title"
    android:textColor="@color/color_Vld"
    android:gravity="center"
    android:background="@color/blanco" />

<LinearLayout
    android:id="@+id/grafico"
    android:layout_width="fill_parent"
    android:layout_heightt="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="5dp"
    android:background="@color/black"
    android:orientation="vertical"
    android:layout_below="@+id/Title_O" />
</RelativeLayout>

It is in the LinearLayout with id = grafico where I draw table in the appropriate class as a follows:

@SuppressWarnings("deprecation")
private void pintaTexto(){

ScrollView scvista = new ScrollView(this);
scvista.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
HorizontalScrollView hscvista = new HorizontalScrollView(this);
hscvista.setLayoutParams(new HorizontalScrollView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TableLayout tabla = new TableLayout(this);
tabla.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
double numRows = 20;
int numColumns = 4;
// Header of the table
    TableRow cabecera = new TableRow(this);
    cabecera.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    cabecera.setBackgroundColor(getResources().getColor(R.color.azulOscuro_elvg));
    tabla.addView(cabecera);    
// Header data
    for (int j = 0; j < numColumns; j++) {
        TextView textColumna = new TextView(this);
        textColumna.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));                
        textColumna.setText(json.json4.GetInfo(0, j));
        textColumna.setTextColor(getResources().getColor(R.color.blanco));
        textColumna.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        textColumna.setGravity(Gravity.CENTER_HORIZONTAL);
        textColumna.setPadding(5, 5, 5, 5);
        cabecera.addView(textColumna);
    }       
// Data row
        for (int f = 0; f < numRows; f++) {
           TableRow row = new TableRow(this);
           for (int c = 0; c < numColumns; c++) {
                   TextView textDato = new TextView(this);
                   textDato.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, 60));
                   textDato.setText(json.json4.GetValue(f, c));
                   textDato.setTextColor(getResources().getColor(R.color.azulOscuro_elvg));
                   textDato.setBackgroundColor(getResources().getColor(R.color.blanco));
                   textDato.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);                           
                   textDato.setGravity(Gravity.CENTER_VERTICAL);                           
                   textDato.setPadding(15, 0, 15, 0);
                   row.addView(textDato);
           }
           tabla.addView(fila);
        }

hscvista.addView(tabla);
scvista.addView(hscvista);
LinearLayout layout = (LinearLayout) findViewById(R.id.grafico);
layout.addView(scvista); 
}

I have checked width attribute of all elements and I can not fit FILL_PARENT as i have indicated. In addition, this LinearLayout with id = grafico also it is used to draw bar charts and if that fits the entire screen, so I think it's can not anything in xml. ly me.

I hope you can help me, this is my third post with the same message and anybody reply me anything.

Thanks.

KryNaC
  • 379
  • 4
  • 21

1 Answers1

1

give scroll view width and height fill parent

ScrollView scvista = new ScrollView(this); scvista.setLayoutParams(new LayoutParams(LayoutParams.Fillparent, LayoutParams.Fillparent)); HorizontalScrollView hscvista = new HorizontalScrollView(this); hscvista.setLayoutParams(new HorizontalScrollView.LayoutParams(LayoutParams.Fillparent, LayoutParams.Fillparent)); TableLayout tabla = new TableLayout(this); tabla.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

try this dear :)

Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
  • If I just use the table or vertical scroll with table if it works and occupies the full width, but not working using horizontall scroll ... – KryNaC Oct 18 '13 at 16:02