How to instantiate a Gridview or a Listview for the same Activity?

Asked

Viewed 506 times

17

In my app I am presenting a list of items. I would like in certain situations to be presented with a ListView and others like GridView. Situations are:

  1. Smartphone(Portrait) - ListView
  2. Smartphone(Landscape) and Tablet - GridView

Question

How could I check each situation and correctly instantiate the object in Activity?

OBS: The xml part is already done, with the folders in the correct way.

3 answers

5

You should work with screen sizes. The most common way I learned was to use a Resource with a Boolean is property (you can put whatever name you want, isGrid, for example), usually use isTablet.

First, you must create a values folder for each qualifier you have of layout. For example, if you have layout-land, layout-large-land, layout-xlarge then you should also have the values-land, values-large-land and values-xlarge folders.

Then you should create within each of the values folders a bool.xml file with the following content:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="isGrid">false</bool>
</resources>

The value true or false changes according to the existence of gridView, if there is a gridView you must put true, otherwise false.

Now in Activity calling gridView or Listview you add the following code:

private boolean isGrid(){
    return getResources().getBoolean(R.bool.isGrid);
}

Now in the onCreate method of the same Activity you must have a code with the following:

if(this.isGrid()){
    //crie uma gridView
}
else{
    //crie uma listView
}
  • Vlw for helping, I got a reply that worked really well. I posted it here so I can help others.

4

If it is necessary to check the orientation, you can use:

getResources().getConfiguration().orientation

which may be one of the following:

ORIENTATION_LANDSCAPE 
ORIENTATION_PORTRAIT

and to check if the screen is large(Tablet) you can try to catch the inches(diagonals) of the device:

public static Double getPolegadas(Activity activity) {

    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(metrics);

    final int largura = metrics.widthPixels;
    final int altura = metrics.heightPixels;
    final double diagonal = Math.sqrt((largura * largura) + (altura * altura));

    polegadas = diagonal / metrics.densityDpi;

    return polegadas;
}

Then for Tablet, you would check if it is bigger than 6.5, for example:

if (polegadas >= 6.5)

I hope I’ve helped!

  • Thanks for the help, I received a simpler answer and that worked very well, I published it here so I can help others.

  • @ribeirojpn Do not forget to accept the best answer. Hug!

2


I received the reply in another forum where I also asked and I received as an answer a great solution, I will copy here because it can help someone else in the future. I applied it to my app and it worked.

Suppose the XML containing the ListView or the GridView be called layout.xml. So:

  • in the briefcase layout, there must be a layout.xml with ListView.
  • in the briefcase layout-land and layout-large should there be a layout.xml with GridView.

With this done, Android will already inflate the layout.xml correct in doing:

setContentView(R.layout.layout);

As both Listview and Gridview are children of Abslistview, do the following:

AbsListView listOrGrid = 
    (AbsListView) findViewById(R.id.id_do_grid_e_list); 

Written by Lucas at GUJ(I hope there are no problems in quoting another forum)

Browser other questions tagged

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