Example usage for android.widget GridView AUTO_FIT

List of usage examples for android.widget GridView AUTO_FIT

Introduction

In this page you can find the example usage for android.widget GridView AUTO_FIT.

Prototype

int AUTO_FIT

To view the source code for android.widget GridView AUTO_FIT.

Click Source Link

Document

Creates as many columns as can fit on screen.

Usage

From source file:net.peterkuterna.android.apps.devoxxfrsched.ui.SpeakersGridFragment.java

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    final GridView gridView = getGridView();
    float sf = getResources().getDisplayMetrics().density;
    final int padding = (int) (5.0 * sf);
    final int spacing = (int) (20.0 * sf);

    gridView.setNumColumns(GridView.AUTO_FIT);
    gridView.setColumnWidth((int) (120 * sf));
    gridView.setStretchMode(GridView.STRETCH_SPACING);
    gridView.setVerticalFadingEdgeEnabled(true);
    gridView.setPadding(padding, padding, padding, padding);
    gridView.setHorizontalSpacing(spacing);
    gridView.setVerticalSpacing(spacing);
}

From source file:com.example.android.popularmovies.app.PopularMoviesFragment.java

private void updateMovies(int page) {

    // check the movie view mode and change the grid columns numbers in one row
    if (moviesAdapter.MOVIES_VIEW.equalsIgnoreCase(getString(R.string.pref_movies_view_grid))) {
        gridView.setNumColumns(GridView.AUTO_FIT);
    } else if (moviesAdapter.MOVIES_VIEW.equalsIgnoreCase(getString(R.string.pref_movies_view_list))) {
        gridView.setNumColumns(1);/*  w  w  w  .  j ava 2 s .c  o m*/
    }

    // run background task and fetch from api data
    FetchMoviesTask moviesTask = new FetchMoviesTask(getContext(), moviesAdapter);

    moviesTask.execute(String.valueOf(page));
}

From source file:ch.arnab.simplelauncher.GridFragment.java

/**
 * Provide default implementation to return a simple grid view.  Subclasses
 * can override to replace with their own layout.  If doing so, the
 * returned view hierarchy <em>must</em> have a GridView whose id
 * is {@link android.R.id#list android.R.id.list} and can optionally
 * have a sibling view id {@link android.R.id#empty android.R.id.empty}
 * that is to be shown when the grid is empty.
 *
 * <p>If you are overriding this method with your own custom content,
 * consider including the standard layout {@link android.R.layout#list_content}
 * in your layout file, so that you continue to retain all of the standard
 * behavior of ListFragment.  In particular, this is currently the only
 * way to have the built-in indeterminant progress state be shown.
 *//*from   ww  w  . j a  va2s  . c  o  m*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final Context context = getActivity();

    FrameLayout root = new FrameLayout(context);

    // ------------------------------------------------------------------

    LinearLayout pframe = new LinearLayout(context);
    pframe.setId(INTERNAL_PROGRESS_CONTAINER_ID);
    pframe.setOrientation(LinearLayout.VERTICAL);
    pframe.setVisibility(View.GONE);
    pframe.setGravity(Gravity.CENTER);

    ProgressBar progress = new ProgressBar(context, null, android.R.attr.progressBarStyleLarge);
    pframe.addView(progress, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    root.addView(pframe, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    // ------------------------------------------------------------------

    FrameLayout lframe = new FrameLayout(context);
    lframe.setId(INTERNAL_LIST_CONTAINER_ID);

    TextView tv = new TextView(getActivity());
    tv.setId(INTERNAL_EMPTY_ID);
    tv.setGravity(Gravity.CENTER);
    lframe.addView(tv, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    GridView lv = new GridView(getActivity());
    lv.setId(android.R.id.list);
    lv.setDrawSelectorOnTop(false);
    lv.setColumnWidth(convertDpToPixels(60, getActivity()));
    lv.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
    lv.setNumColumns(GridView.AUTO_FIT);
    lv.setHorizontalSpacing(convertDpToPixels(20, getActivity()));
    lv.setVerticalSpacing(convertDpToPixels(20, getActivity()));
    lv.setSmoothScrollbarEnabled(true);

    // disable overscroll
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        lv.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
    }

    lframe.addView(lv, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    root.addView(lframe, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    // ------------------------------------------------------------------

    root.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    return root;
}

From source file:com.imobile.mt8382hotkeydefine.GridFragment.java

/**
 * Provide default implementation to return a simple grid view.  Subclasses
 * can override to replace with their own layout.  If doing so, the
 * returned view hierarchy <em>must</em> have a GridView whose id
 * is {@link android.R.id#list android.R.id.list} and can optionally
 * have a sibling view id {@link android.R.id#empty android.R.id.empty}
 * that is to be shown when the grid is empty.
 *
 * <p>If you are overriding this method with your own custom content,
 * consider including the standard layout {@link android.R.layout#list_content}
 * in your layout file, so that you continue to retain all of the standard
 * behavior of ListFragment.  In particular, this is currently the only
 * way to have the built-in indeterminant progress state be shown.
 *///from   w  w  w.  j a  v a 2s.com
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final Context context = getActivity();

    FrameLayout root = new FrameLayout(context);

    // ------------------------------------------------------------------

    LinearLayout pframe = new LinearLayout(context);
    pframe.setId(INTERNAL_PROGRESS_CONTAINER_ID);
    pframe.setOrientation(LinearLayout.VERTICAL);
    pframe.setVisibility(View.GONE);
    pframe.setGravity(Gravity.CENTER);

    ProgressBar progress = new ProgressBar(context, null, android.R.attr.progressBarStyleLarge);

    pframe.addView(progress, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    root.addView(pframe, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    // ------------------------------------------------------------------

    FrameLayout lframe = new FrameLayout(context);
    lframe.setId(INTERNAL_LIST_CONTAINER_ID);

    TextView tv = new TextView(getActivity());
    tv.setId(INTERNAL_EMPTY_ID);
    tv.setGravity(Gravity.CENTER);
    lframe.addView(tv, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    GridView lv = new GridView(getActivity());
    lv.setId(android.R.id.list);
    lv.setDrawSelectorOnTop(false);
    lv.setColumnWidth(convertDpToPixels(60, getActivity()));
    lv.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
    lv.setNumColumns(GridView.AUTO_FIT);
    lv.setHorizontalSpacing(convertDpToPixels(20, getActivity()));
    lv.setVerticalSpacing(convertDpToPixels(20, getActivity()));
    lv.setSmoothScrollbarEnabled(true);

    // disable overscroll
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        lv.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
    }

    lframe.addView(lv, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    root.addView(lframe, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    // ------------------------------------------------------------------
    /*
            root.setLayoutParams(new FrameLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    */
    return root;
}

From source file:com.ckdroid.ilauncher.ShowGridFragment.java

/**
 * Provide default implementation to return a simple grid view.  Subclasses
 * can override to replace with their own layout.  If doing so, the
 * returned view hierarchy <em>must</em> have a GridView whose id
 * is {@link android.R.id#list android.R.id.list} and can optionally
 * have a sibling view id {@link android.R.id#empty android.R.id.empty}
 * that is to be shown when the grid is empty.
 * <p/>//from w w  w.j ava 2  s  .  co  m
 * <p>If you are overriding this method with your own custom content,
 * consider including the standard layout {@link android.R.layout#list_content}
 * in your layout file, so that you continue to retain all of the standard
 * behavior of ListFragment.  In particular, this is currently the only
 * way to have the built-in indeterminant progress state be shown.
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final Context context = getActivity();

    FrameLayout root = new FrameLayout(context);

    // ------------------------------------------------------------------

    LinearLayout pframe = new LinearLayout(context);
    pframe.setId(INTERNAL_PROGRESS_CONTAINER_ID);
    pframe.setOrientation(LinearLayout.VERTICAL);
    pframe.setVisibility(View.GONE);
    pframe.setGravity(Gravity.CENTER);

    ProgressBar progress = new ProgressBar(context, null, android.R.attr.progressBarStyleLarge);
    pframe.addView(progress, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    root.addView(pframe, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    // ------------------------------------------------------------------

    FrameLayout lframe = new FrameLayout(context);
    lframe.setId(INTERNAL_LIST_CONTAINER_ID);

    TextView tv = new TextView(getActivity());
    tv.setId(INTERNAL_EMPTY_ID);
    tv.setGravity(Gravity.CENTER);
    lframe.addView(tv, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    GridView lv = new GridView(getActivity());
    lv.setId(android.R.id.list);
    lv.setDrawSelectorOnTop(false);
    lv.setColumnWidth(convertDpToPixels(80, getActivity()));
    lv.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
    lv.setNumColumns(GridView.AUTO_FIT);
    lv.setNumColumns(4);
    lv.setHorizontalSpacing(convertDpToPixels(10, getActivity()));
    lv.setVerticalSpacing(convertDpToPixels(10, getActivity()));
    lv.setSmoothScrollbarEnabled(true);

    // disable overscroll
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        lv.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
    }

    lframe.addView(lv, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    root.addView(lframe, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    // ------------------------------------------------------------------

    root.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    return root;
}

From source file:com.ckdroid.ilauncher.AllGridFragment.java

/**
 * Provide default implementation to return a simple grid view.  Subclasses
 * can override to replace with their own layout.  If doing so, the
 * returned view hierarchy <em>must</em> have a GridView whose id
 * is {@link android.R.id#list android.R.id.list} and can optionally
 * have a sibling view id {@link android.R.id#empty android.R.id.empty}
 * that is to be shown when the grid is empty.
 * <p/>//ww  w  .  jav a  2s.c o  m
 * <p>If you are overriding this method with your own custom content,
 * consider including the standard layout {@link android.R.layout#list_content}
 * in your layout file, so that you continue to retain all of the standard
 * behavior of ListFragment.  In particular, this is currently the only
 * way to have the built-in indeterminant progress state be shown.
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final Context context = getActivity();

    FrameLayout root = new FrameLayout(context);

    // ------------------------------------------------------------------

    LinearLayout pframe = new LinearLayout(context);
    pframe.setId(INTERNAL_PROGRESS_CONTAINER_ID);
    pframe.setOrientation(LinearLayout.VERTICAL);
    pframe.setVisibility(View.GONE);
    pframe.setGravity(Gravity.CENTER);

    ProgressBar progress = new ProgressBar(context, null, android.R.attr.progressBarStyleLarge);
    pframe.addView(progress, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    root.addView(pframe, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    // ------------------------------------------------------------------

    FrameLayout lframe = new FrameLayout(context);
    lframe.setId(INTERNAL_LIST_CONTAINER_ID);

    TextView tv = new TextView(getActivity());
    tv.setId(INTERNAL_EMPTY_ID);
    tv.setGravity(Gravity.CENTER);
    lframe.addView(tv, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    GridView lv = new GridView(getActivity());
    lv.setId(android.R.id.list);
    lv.setDrawSelectorOnTop(false);
    lv.setColumnWidth(convertDpToPixels(60, getActivity()));
    lv.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
    lv.setNumColumns(GridView.AUTO_FIT);
    lv.setHorizontalSpacing(convertDpToPixels(20, getActivity()));
    lv.setVerticalSpacing(convertDpToPixels(20, getActivity()));
    lv.setSmoothScrollbarEnabled(true);

    // disable overscroll
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        lv.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
    }

    lframe.addView(lv, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    root.addView(lframe, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    // ------------------------------------------------------------------

    root.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    root.setPadding(24, 30, 24, 30);

    root.setBackgroundColor(getResources().getColor(R.color.bg_white));

    return root;
}

From source file:com.owncloud.android.ui.fragment.ExtendedListFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log_OC.d(TAG, "onCreateView");

    View v = inflater.inflate(R.layout.list_fragment, null);

    mListView = (ExtendedListView) (v.findViewById(R.id.list_root));
    mListView.setOnItemClickListener(this);
    mListFooterView = inflater.inflate(R.layout.list_footer, null, false);

    mGridView = (GridViewWithHeaderAndFooter) (v.findViewById(R.id.grid_root));
    mGridView.setNumColumns(GridView.AUTO_FIT);
    mGridView.setOnItemClickListener(this);
    mGridFooterView = inflater.inflate(R.layout.list_footer, null, false);

    if (savedInstanceState != null) {
        int referencePosition = savedInstanceState.getInt(KEY_SAVED_LIST_POSITION);
        if (mCurrentListView == mListView) {
            Log_OC.v(TAG, "Setting and centering around list position " + referencePosition);
            mListView.setAndCenterSelection(referencePosition);
        } else {//from  w ww  .  j  av a2 s  . c om
            Log_OC.v(TAG, "Setting grid position " + referencePosition);
            mGridView.setSelection(referencePosition);
        }
    }

    // Pull-down to refresh layout
    mRefreshListLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_containing_list);
    mRefreshGridLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_containing_grid);
    mRefreshEmptyLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_containing_empty);
    mEmptyListMessage = (TextView) v.findViewById(R.id.empty_list_view);

    onCreateSwipeToRefresh(mRefreshListLayout);
    onCreateSwipeToRefresh(mRefreshGridLayout);
    onCreateSwipeToRefresh(mRefreshEmptyLayout);

    mListView.setEmptyView(mRefreshEmptyLayout);
    mGridView.setEmptyView(mRefreshEmptyLayout);

    mCurrentListView = mListView; // list as default

    return v;
}

From source file:org.xbmc.kore.ui.AbstractListFragment.java

private void toggleAmountOfColumns(MenuItem item) {
    SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(getActivity()).edit();
    if (gridView.getNumColumns() == 1) {
        editor.putBoolean(Settings.KEY_PREF_SINGLE_COLUMN, false);
        item.setTitle(R.string.single_column);
        gridView.setNumColumns(GridView.AUTO_FIT);
    } else {//www  . j  a  v a 2  s  .  com
        editor.putBoolean(Settings.KEY_PREF_SINGLE_COLUMN, true);
        item.setTitle(R.string.multi_column);
        gridView.setNumColumns(1);
    }
    editor.apply();
    adapter.notifyDataSetChanged(); //force gridView to redraw
}

From source file:com.cerema.cloud2.ui.fragment.ExtendedListFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log_OC.d(TAG, "onCreateView");

    View v = inflater.inflate(R.layout.list_fragment, null);

    mListView = (ExtendedListView) (v.findViewById(R.id.list_root));
    mListView.setOnItemClickListener(this);
    mListFooterView = inflater.inflate(R.layout.list_footer, null, false);

    mGridView = (GridViewWithHeaderAndFooter) (v.findViewById(R.id.grid_root));
    mGridView.setNumColumns(GridView.AUTO_FIT);
    mGridView.setOnItemClickListener(this);
    mGridFooterView = inflater.inflate(R.layout.list_footer, null, false);

    if (savedInstanceState != null) {
        int referencePosition = savedInstanceState.getInt(KEY_SAVED_LIST_POSITION);
        if (mCurrentListView == mListView) {
            Log_OC.v(TAG, "Setting and centering around list position " + referencePosition);
            mListView.setAndCenterSelection(referencePosition);
        } else {//from w  ww. j a v  a  2 s.co  m
            Log_OC.v(TAG, "Setting grid position " + referencePosition);
            mGridView.setSelection(referencePosition);
        }
    }

    // Pull-down to refresh layout
    mRefreshListLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_containing_list);
    mRefreshGridLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_containing_grid);
    mRefreshEmptyLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe_containing_empty);
    mEmptyListMessage = (TextView) v.findViewById(R.id.empty_list_view);

    onCreateSwipeToRefresh(mRefreshListLayout);
    onCreateSwipeToRefresh(mRefreshGridLayout);
    onCreateSwipeToRefresh(mRefreshEmptyLayout);

    mListView.setEmptyView(mRefreshEmptyLayout);
    mGridView.setEmptyView(mRefreshEmptyLayout);

    mCurrentListView = mListView; // list as default

    mFabMain = (FloatingActionsMenu) v.findViewById(R.id.fab_main);
    mFabUpload = (FloatingActionButton) v.findViewById(R.id.fab_upload);
    mFabMkdir = (FloatingActionButton) v.findViewById(R.id.fab_mkdir);
    mFabUploadFromApp = (FloatingActionButton) v.findViewById(R.id.fab_upload_from_app);

    return v;
}

From source file:org.videolan.vlc.gui.video.VideoGridFragment.java

private void updateViewMode() {
    if (getView() == null || getActivity() == null) {
        Log.w(TAG, "Unable to setup the view");
        return;//  ww w.j a v a2 s.  com
    }

    GridView gv = (GridView) getView().findViewById(android.R.id.list);

    // Compute the left/right padding dynamically
    DisplayMetrics outMetrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
    int sidePadding = (int) (outMetrics.widthPixels / 100 * Math.pow(outMetrics.density, 3) / 2);
    sidePadding = Math.max(0, Math.min(100, sidePadding));
    gv.setPadding(sidePadding, gv.getPaddingTop(), sidePadding, gv.getPaddingBottom());

    // Select between grid or list
    if (hasSpaceForGrid(getView())) {
        Log.d(TAG, "Switching to grid mode");
        gv.setNumColumns(GridView.AUTO_FIT);
        gv.setStretchMode(GRID_STRETCH_MODE);
        gv.setHorizontalSpacing(Util.convertDpToPx(GRID_HORIZONTAL_SPACING_DP));
        gv.setVerticalSpacing(Util.convertDpToPx(GRID_VERTICAL_SPACING_DP));
        gv.setColumnWidth(Util.convertDpToPx(GRID_ITEM_WIDTH_DP));
        mVideoAdapter.setListMode(false);
    } else {
        Log.e(TAG, "Switching to list mode");
        gv.setNumColumns(1);
        gv.setStretchMode(LIST_STRETCH_MODE);
        gv.setHorizontalSpacing(LIST_HORIZONTAL_SPACING_DP);
        gv.setVerticalSpacing(Util.convertDpToPx(LIST_VERTICAL_SPACING_DP));
        mVideoAdapter.setListMode(true);
    }
}