Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change LayoutManager depending on device format

I have a RecyclerView with a list of Cards. I want to know whether it's possible to change the RecyclerView's LayoutManager to Linear, when using a phone, and StaggeredGrid, when using a tablet, programmatically. My initial idea was to have the same code on the Activity, and only change the layout.xml, but given Android uses different LayoutManagers, it seems note complicated than that. I also tried using Cardslib library, but got reeeaally confused by the documentation, since there is no complete example with custom cards. Any ideas?

like image 450
Hugo M. Zuleta Avatar asked Jun 27 '26 22:06

Hugo M. Zuleta


2 Answers

Yes it is possible. One solution is to define a boolean resource in your values folder. For example, you can define:

<bool name="is_phone">true</bool>

in your values folder and in your values-sw720dp and values-sw600dp add the same resource with a false.

<bool name="is_phone">false</bool>

Then, in your Activity's onCreate(), you can do this:

    boolean isPhone = getResources().getBoolean(R.bool.is_phone);

    if (isPhone) {
        // Set linearlayoutmanager for your recyclerview.
    } else {
        // Set staggeredgridlayoutmanager for your recyclerview.
    }
like image 156
androholic Avatar answered Jun 29 '26 11:06

androholic


So, as I told @androholic, what I was trying to figure out is how to have the layout change depending on the devices format. This way, whenever the app was loaded on a tablet, a Grid was shown, and a List on phones. However, in order to do this with a RecyclerView, two LayouManagers would be needed: LinearLayoutManager for the list, and Staggered/GridLayoutManager, making the code a bit more complicated.

What I did: I used a GridLayoutManager for the general case. What I would change according to the screen size would be only the number of columns. This way, a list would be a RecyclerView with a GridLayoutManager with 1 column, and a grid would have more than one. In my case, I use only 2 columns.

My code is as follows.

public class AppListActivity extends AppCompatActivity {

private ArrayList<App> apps;
private int columns;


private String root = Environment.getExternalStorageDirectory().toString();

private boolean isTablet;
private RecyclerViewAdapter rvadapter;

public static Context context;
private SwipeRefreshLayout swipeContainer;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = getApplicationContext();
    //CHECK WHETHER THE DEVICE IS A TABLET OR A PHONE
    isTablet = getResources().getBoolean(R.bool.isTablet);
    if (isTablet()) { //it's a tablet
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        columns = 2;
    } else { //it's a phone, not a tablet
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        columns = 1;
    }
 //SwipeContainer SETUP
 //ArrayList and RecyclerView initialization
    apps = new ArrayList<App>();

    RecyclerView rv = (RecyclerView) findViewById(R.id.recycler_view);

    rv.setHasFixedSize(true);
    GridLayoutManager gridlm = new GridLayoutManager(getApplicationContext(),columns);
    rv.setLayoutManager(gridlm);
    rvadapter = new RecyclerViewAdapter(apps);
    rv.setAdapter(rvadapter);
    }
    public boolean isTablet() {
       return isTablet;
    }

The method isTablet is pretty much the same one on @androholic's answer. Hopefully, this will clear up any doubts on what my question was (I realize my wording wasn't the best), and what I accomplished.

like image 36
Hugo M. Zuleta Avatar answered Jun 29 '26 13:06

Hugo M. Zuleta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!