Paul
Paul

Reputation: 1734

Displaying empty rows in Android listview

So I'm wondering how to go about filling a ListView with empty rows. The ListView is populated via SQLite db so say for instance there is only 3 items in the list I want to fill the rest of the screen with empty rows. Here is a screenshot of what I mean. Yes I know it's from iPhone but it demonstrates what I mean:

image 1

Upvotes: 4

Views: 3235

Answers (4)

jj.
jj.

Reputation: 2390

The cheap way is to add extra "rows" in your layout xml. Any extra rows will be cut off by the screen. This can get messy: a higher-res screen might require you add many extra TextViews. Since they get cut off, I suppose you could add as many as you'd like. It would look something like this:

<LinearLayout>

<ListView></ListView>
    
<TextView/>
<TextView/>
<TextView/>
...

</LinearLayout>

Another option is to add extra rows to your ListView, as mentioned previously. If you are bound to an array, add extra blank rows to the array and handle it appropriately. Coincidentally, if you are using a Cursor you can use a MaxtrixCursor & MergeCursor as described in this answer

I ended up using a combination of these methods. I try my best to calculate the number of rows I want to add to my ListView, but I error on the side of caution and have a couple TextViews underneath my ListView that make it all come together.

Upvotes: 3

Maxime Claude
Maxime Claude

Reputation: 995

@kabir's solution works. Now if you are like me (wanted to have two alternate colors in background, this is his dispached method rewritten (or let's say edited)

 @Override
protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);

    int caseLastChildBottom = -1;
    int caselastChildHeight = -1;
    int caseNrOfLines = -1;

    //makes the colors follow the order (alternateColor1 - alternateColor2 - alternateColor1 - etc.)
    int plusIndex = 1;
    if (this.getChildCount() % 2 == 0)
        plusIndex = 0;

    // ListView's height
    final int currentHeight = getMeasuredHeight();

    // this will let you know the status for the ListView, fitting/not fitting content
    final int scrolledHeight = computeVerticalScrollRange();

    //empty listview (no item)
    if (scrolledHeight == 0) {
        //no childs exist so we take the top
        caseLastChildBottom = 0;
        //last child doesn't exist, so we set a default height (took the value in dp in the item row's height)
        caselastChildHeight = convertDpToPx(DEFAULT_CHILD_ROW_S_HEIGHT);
        // determine the number of lines required to fill the ListView
        caseNrOfLines = currentHeight / caselastChildHeight;
    }

    //there is a remaining gap to fill
    else {
        final View lastChild = getChildAt(getChildCount() - 1);
        if (lastChild == null) return;
        // values used to know where to start drawing lines
        caseLastChildBottom = lastChild.getBottom();
        // last child's height(use this to determine an appropriate value for the row height)
        caselastChildHeight = lastChild.getMeasuredHeight();
        // determine the number of lines required to fill the ListView
        caseNrOfLines = (currentHeight - caseLastChildBottom) / caselastChildHeight;
    }
    // values used to know where to start drawing lines
    final int lastChildBottom = caseLastChildBottom;
    // last child's height(use this to determine an appropriate value for the row height)
    final int lastChildHeight = caselastChildHeight;
    // determine the number of lines required to fill the ListView
    final int nrOfLines = caseNrOfLines;

    int i = 0;
    for (i = 0; i < nrOfLines; i++) {
        Rect r = new Rect(0, lastChildBottom + i * lastChildHeight, getMeasuredWidth(), lastChildBottom + (i + 1) * lastChildHeight);
        canvas.drawRect(r, (i + plusIndex) % 2 == 0 ? alternateColorView1 : alternateColorView2);
    }

    //is there a gap at the bottom of the list
    if(currentHeight - (nrOfLines *lastChildHeight) > 0){
        Rect r = new Rect(0, lastChildBottom + i * lastChildHeight,getMeasuredWidth(), currentHeight);
        canvas.drawRect(r, (i + plusIndex) % 2 == 0 ? alternateColorView1 : alternateColorView2);
    }
    return;
}

I forgot these two Paint colors (just as @kabir declared the colors):

    alternateColorView1.setColor(....);
    alternateColorView1.setStyle(Paint.Style.FILL);

    alternateColorView2.setColor(....);
    alternateColorView2.setStyle(Paint.Style.FILL);

Upvotes: 0

Paul
Paul

Reputation: 1734

Using a textview below the listview seems to give the illusion of what I was going for.

Upvotes: 0

When you create the Array the is going to be bound to the ListView you just need to add a few rows at the end of the Array with empty strings.

Upvotes: 2

Related Questions