How to instantiate a GridView or a ListView for the same Activity?

17

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

  • Smartphone (portrait) - ListView
  • Smartphone (landscape) and Tablet - GridView
  • Question

    How can 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.

        
    asked by anonymous 09.01.2014 / 02:54

    3 answers

    2

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

      

    Suppose XML containing ListView or GridView is called layout.xml . Then:

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

    With that done, Android will already inflate the layout.xml correct when doing:

    setContentView(R.layout.layout);
    
         

    Since both ListView and GridView are children of AbsListView, do the following:

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

    Sent by Lucas in the GUJ (I hope there are no problems in quoting another forum)

        
    09.01.2014 / 18:23
    5

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

    First, you must create a values folder for each qualifier that you have to 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 a bool.xml file with the following contents inside each of the values folders:

    <?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 the gridView, if there is a gridView you should set true, otherwise false.

    Now in the activity that calls the gridView or the 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 should have a code with the following:

    if(this.isGrid()){
        //crie uma gridView
    }
    else{
        //crie uma listView
    }
    
        
    09.01.2014 / 17:29
    4

    If you need to check orientation, you can use:

    getResources().getConfiguration().orientation
    

    which can be one of the following constants:

    ORIENTATION_LANDSCAPE 
    ORIENTATION_PORTRAIT
    

    To check if the screen is large (Tablet) you can try to pick up the device's (diagonal) inches:

    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;
    }
    

    There for Tablet, you would check if it is larger than 6.5, for example:

    if (polegadas >= 6.5)
    

    I hope I have helped!

        
    09.01.2014 / 16:36