Change background color when Listview has no items

Asked

Viewed 70 times

0

Is there any way to customize a listView besides the textView default? The screen is empty obviously, but would like to change the background color only when searching not to display the results.

 mEstadoVazioTextView = (TextView) findViewById(R.id.visualizacaoVazia);
        listview.setEmptyView(mEstadoVazioTextView);

 mEstadoVazioTextView.setText("Nenhum livro encontrado!\n\nFavor verificar ortografia ou" +
                    " informar algum dado sobre o livro novamente.");

2 answers

1

Hi, look I’m new to development, but I hope I can help.

From what I understand you have a Listview that is being populated with a list of Books, what you want is if the search does not return result that is displayed a message to the user.

I’ve done it in a few different ways.

1) Instead of putting the message in Listview, you could take the list size and if this list has size 0 (with no result listed) you can put this message in Layout (not in Listview).

int tamanho = Lista.size();
if(tamanho == 0){
    Texto.SetText("Livro não encontrado")
}

2) Now if you really want to customize the List, I advise you to use a Recyclerview because you will have more freedom in customization. Research on this that will certainly help.

3) You can still put a Toast informing the user that there is no result in the search.

I hope I’ve helped.

  • hi, thanks for the help, but the message is already being presented if it does not return any results, I would really like to customize the layout when it returns in this state, I’m using the message in listView, for the method setEmptyView() already takes care of that part of the result of the search for emptiness.

  • Blz! So search on Recyclerview you will get what you’re looking for. There are many videos explaining step by step implementation. Explaining here would be very great the post. Good Luck!

1


Before making the list available to Adapter check that it is empty and change the background of Listview accordingly.

Anything like that:

if(lista.size() == 0){
    listview.setBackgroundColor(Color.RED)
else{
    listview.setBackgroundColor(Color.WHITE)
}

Edit after the comment

I am using Loader, the loading of the list is outside onCreate, as reference the variable in this case?

Declare the variable to store Listview as an Activity attribute.

private ListView listview;

In the method onLoadFinished() check if the cursor has results and change the background of Listview accordingly:

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {

    if(cursor.getCount() == 0){
        listview.setBackgroundColor(Color.RED)
    else{
        listview.setBackgroundColor(Color.WHITE)
    }

    adapter.changeCursor(cursor);
}
  • I am using Loader, the loading of the list is outside onCreate, as reference the variable in this case?

  • I edited the answer. It answers your question?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.