Example usage for android.widget GridView setStretchMode

List of usage examples for android.widget GridView setStretchMode

Introduction

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

Prototype

public void setStretchMode(@StretchMode int stretchMode) 

Source Link

Document

Control how items are stretched to fill their space.

Usage

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/>/*w w w . j a va2s.  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:de.baumann.thema.RequestActivity.java

@SuppressWarnings("unchecked")
private void populateView(ArrayList arrayListFinal) {
    ArrayList<AppInfo> local_arrayList;
    local_arrayList = arrayListFinal;/*w  w w .jav a2  s .c o  m*/

    GridView grid = (GridView) findViewById(R.id.appgrid);

    assert grid != null;
    grid.setVerticalSpacing(GridView.AUTO_FIT);
    grid.setHorizontalSpacing(GridView.AUTO_FIT);
    grid.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
    grid.setFastScrollEnabled(true);
    grid.setFastScrollAlwaysVisible(true);

    if (DEBUG)
        Log.v(TAG, "height: " + getDisplaySize("height") + "; width: " + getDisplaySize("width"));

    AppAdapter appInfoAdapter;
    if (isPortrait()) {
        grid.setNumColumns(numCol_Portrait);

        if (isTablet(context)) {
            grid.setNumColumns(numCol_Portrait); //Here you can change the number of columns for Tablets
            if (DEBUG)
                Log.v(TAG, "isTablet");
        }
        if (isXLargeTablet(context)) {
            grid.setNumColumns(numCol_Portrait); //Here you can change the number of columns for Large Tablets
            if (DEBUG)
                Log.v(TAG, "isXLargeTablet");
        }

        appInfoAdapter = new AppAdapter(this, R.layout.request_item_list, local_arrayList);
    }

    else {
        grid.setNumColumns(numCol_Landscape);

        if (isTablet(context)) {
            grid.setNumColumns(numCol_Landscape); //Here you can change the number of columns for Tablets
            if (DEBUG)
                Log.v(TAG, "isTablet");
        }
        if (isXLargeTablet(context)) {
            grid.setNumColumns(numCol_Landscape); //Here you can change the number of columns for Large Tablets
            if (DEBUG)
                Log.v(TAG, "isXLargeTablet");
        }

        appInfoAdapter = new AppAdapter(this, R.layout.request_item_grid, local_arrayList);
    }

    grid.setAdapter(appInfoAdapter);
    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> AdapterView, View view, int position, long row) {
            AppInfo appInfo = (AppInfo) AdapterView.getItemAtPosition(position);
            CheckBox checker = (CheckBox) view.findViewById(R.id.CBappSelect);
            ViewSwitcher icon = (ViewSwitcher) view.findViewById(R.id.viewSwitcherChecked);
            LinearLayout localBackground = (LinearLayout) view.findViewById(R.id.card_bg);
            Animation aniIn = AnimationUtils.loadAnimation(context, R.anim.request_flip_in_half_1);
            Animation aniOut = AnimationUtils.loadAnimation(context, R.anim.request_flip_in_half_2);

            checker.toggle();
            appInfo.setSelected(checker.isChecked());

            icon.setInAnimation(aniIn);
            icon.setOutAnimation(aniOut);

            if (appInfo.isSelected()) {
                if (DEBUG)
                    Log.v(TAG, "Selected App: " + appInfo.getName());
                localBackground
                        .setBackgroundColor(ContextCompat.getColor(context, R.color.request_card_pressed));
                if (icon.getDisplayedChild() == 0) {
                    icon.showNext();
                }
            } else {
                if (DEBUG)
                    Log.v(TAG, "Deselected App: " + appInfo.getName());
                localBackground
                        .setBackgroundColor(ContextCompat.getColor(context, R.color.request_card_unpressed));
                if (icon.getDisplayedChild() == 1) {
                    icon.showPrevious();
                }
            }
        }
    });
}

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;/*from  w  ww . j  a v a 2s  . co  m*/
    }

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