ISchwarz23 / SortableTableView

An Android library containing a simple TableView and an advanced SortableTableView providing a lot of customisation possibilities to fit all needs.
Apache License 2.0
1.05k stars 237 forks source link

Dynamically Changing Table Size #183

Closed Nefariis closed 5 years ago

Nefariis commented 5 years ago

Is it possible to change the size of the table programatically?

Here is how I am implementing my table -

   <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center">

            <de.codecrafters.tableview.SortableTableView
                android:id="@+id/tableView"
                android:layout_width="900dp"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                custom:tableView_headerElevation="14">
            </de.codecrafters.tableview.SortableTableView>
    </HorizontalScrollView>

Which is working fine, but the table can have anywhere from 2-8 columns - so 900dp looks great for 5-8 columns but looks funny for 2-4 columns.

I've tried changing the table size like a normal element but it doesnt seem to be working.

    table.layoutParams = FrameLayout.LayoutParams(100, WRAP_CONTENT)

I can tell it wants to work though, when I wrap it with

    // Log the width and the height before the change
    Log.i(TAG, table.layoutParams.width.toString() + " " + table.layoutParams.height.toString())

    // Change the width and keep the height the same 
    table.layoutParams = FrameLayout.LayoutParams(100, WRAP_CONTENT)

    // Log what it is after the change 
    Log.i(TAG, table.layoutParams.width.toString() + " " + table.layoutParams.height.toString())

I get this in my Logcat

    2019-06-06 09:10:26.104 19812-19812/com.nefariis.xxxxxx I/info_logger: 2475 -2
    2019-06-06 09:10:26.104 19812-19812/com.nefariis.xxxxxx I/info_logger: 100 -2

So you can see the width is going from 2475 to 100 - but its not changing on the screen.

I've tried invalidating, post invalidating, and refreshing/redrawing the activity to no avail.

I was hoping you have an idea of how I might accomplish this?

Thank you and thank you again for the wonderful library.

ISchwarz23 commented 5 years ago

To be honest, I have no idea. But there are two things I would try to do:

1) As the TableView is a LinearLayout, I would try to apply the LinearLayout.LayoutParams instead of the FrameLayout.LayoutParams.

2) In addition I would try to request layout on the scroll views after changing the tables width.

Nefariis commented 5 years ago

So when I switch it to LinearLayout.LayoutParams instead of FrameLayout.LayoutParams I receive this crashing error -

    E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams`

Here is how I have tried to do the update/refresh -

    // This is for the Table
    table.invalidate()
    table.postInvalidate()
    table.refreshDrawableState()
    table.requestLayout()
    table.forceLayout()
    table.visibility = GONE
    table.visibility = VISIBLE

    // This is for the HorizontalScrollView 
    container.invalidate()
    container.postInvalidate()
    container.refreshDrawableState()
    container.requestLayout()
    container.forceLayout()
    container.visibility = GONE
    container.visibility = VISIBLE

Update

I figured out a solution in case anyone needs to accomplish this -

On the SortableTable I changed the android:layout_width to "fill_parent"

    <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center">

            <de.codecrafters.tableview.SortableTableView
                android:id="@+id/tableView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                custom:tableView_headerElevation="14">
            </de.codecrafters.tableview.SortableTableView>
    </HorizontalScrollView>

And then I implemented a method to change the column width depending on which columns were displayed (taken from the main page)

    TableColumnDpWidthModel columnModel = new TableColumnDpWidthModel(context, 4, 200);
    columnModel.setColumnWidth(1, 300);
    columnModel.setColumnWidth(2, 250);
    tableView.setColumnModel(columnModel);

Using this method, invalidate or requestLayout are not necessary and the SortableTable increases/decreses size according to the columns that are displayed.

Thank you again for the help and the quick response

ISchwarz23 commented 5 years ago

Thanks for sharing! :)

omkarghurye1998 commented 4 years ago
[<HorizontalScrollView

        android:id="@+id/lresult"
        android:layout_below="@+id/l1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center">

        <de.codecrafters.tableview.SortableTableView
            android:id="@+id/result"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="6sp"
            >
        </de.codecrafters.tableview.SortableTableView>
    </HorizontalScrollView>](url)

I am unable to change the textsize of the table view I want to make the textsize small. How can i do it? @ISchwarz23 @OoElectron please help!!

ISchwarz23 commented 4 years ago

Are you using the SimplaTableDataAdapter.

omkarghurye1998 commented 4 years ago

Yes sir @ISchwarz23

ISchwarz23 commented 4 years ago

Then you can use the SimpleTableDataAdapter#setTextSize( int ) method.

omkarghurye1998 commented 4 years ago

Screenshot_2020-04-19-23-48-12-987_edmt dev androidgridlayout Thank u sir :) It's working