Example usage for android.widget GridView GridView

List of usage examples for android.widget GridView GridView

Introduction

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

Prototype

public GridView(Context context) 

Source Link

Usage

From source file:MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GridView gridView = new GridView(this);
    setContentView(gridView);/*from w w  w . j  ava  2 s  . c o m*/
    String[] countries = new String[] { "China", "France", "Germany", "India", "Russia", "United Kingdom",
            "United States" };
    ListAdapter countryAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);
    gridView.setAdapter(countryAdapter);
    gridView.setNumColumns(2);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String s = ((TextView) view).getText() + " " + position;
            Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
        }
    });
}

From source file:com.bilibili.socialize.share.utils.selector.BaseSharePlatformSelector.java

protected static GridView createShareGridView(final Context context,
        AdapterView.OnItemClickListener onItemClickListener) {
    GridView grid = new GridView(context);
    ListAdapter adapter = new ArrayAdapter<ShareTarget>(context, 0, shareTargets) {
        // no need scroll
        @Override/*from  w  ww  .java2s . c  om*/
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.bili_socialize_shareboard_item, parent, false);
            view.setBackgroundDrawable(null);
            ImageView image = (ImageView) view.findViewById(R.id.bili_socialize_shareboard_image);
            TextView platform = (TextView) view.findViewById(R.id.bili_socialize_shareboard_pltform_name);

            ShareTarget target = getItem(position);
            image.setImageResource(target.iconId);
            platform.setText(target.titleId);
            return view;
        }
    };
    grid.setNumColumns(-1);
    grid.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
    grid.setColumnWidth(context.getResources().getDimensionPixelSize(R.dimen.bili_socialize_shareboard_size));
    grid.setLayoutParams(new ViewGroup.LayoutParams(-1, -2));
    grid.setSelector(R.drawable.bili_socialize_selector_item_background);
    grid.setAdapter(adapter);
    grid.setOnItemClickListener(onItemClickListener);
    return grid;
}

From source file:com.javielinux.dialogs.SelectIconDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    int title = R.string.select_icon;

    final ArrayList<Entity> icons = DataFramework.getInstance().getEntityList("icons");
    ArrayList<Integer> resources = new ArrayList<Integer>();

    for (Entity icon : icons) {
        resources.add(getResources().getIdentifier(
                DataFramework.getInstance().getPackage() + ":drawable/" + icon.getString("icon"), null, null));
    }/*from  w w  w .j a  v a 2  s.co m*/

    ImageResourcesAdapter adapter = new ImageResourcesAdapter(getActivity(), resources);

    GridView gridView = new GridView(getActivity());
    gridView.setNumColumns(3);
    gridView.setPadding(5, 5, 5, 5);
    gridView.setVerticalSpacing(5);
    gridView.setGravity(Gravity.CENTER);
    gridView.setAdapter(adapter);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
            if (selectIconListener != null) {
                selectIconListener.onSelectIcon(icons.get(pos).getId());
            }
            dismiss();
        }
    });

    return new AlertDialog.Builder(getActivity()).setTitle(title).setView(gridView).create();
}

From source file:org.weishe.weichat.emoji.EmojiPageFragment.java

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    sGrid = new GridView(getActivity());
    sGrid.setNumColumns(KJEmojiConfig.COLUMNS);
    adapter = new EmojiGridAdapter(getActivity(), datas);
    sGrid.setAdapter(adapter);//w  ww .  java 2  s .  c  o  m
    sGrid.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            EditText editText = (EditText) getActivity().findViewById(R.id.emoji_titile_input);
            if (listener != null) {
                listener.onEmojiClick((Emojicon) parent.getAdapter().getItem(position));
            }
            InputHelper.input2OSC(editText, (Emojicon) parent.getAdapter().getItem(position));
        }
    });
    sGrid.setSelector(new ColorDrawable(android.R.color.transparent));
    return sGrid;
}

From source file:com.example.giggle.oschina2.emoji.EmojiPageFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    sGrid = new GridView(getActivity());
    sGrid.setNumColumns(KJEmojiConfig.COLUMNS);
    adapter = new EmojiGridAdapter(getActivity(), datas);
    sGrid.setAdapter(adapter);/*from w w  w.jav a 2  s  .c o  m*/
    sGrid.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            EditText editText = (EditText) getActivity().findViewById(R.id.emoji_titile_input);
            if (listener != null) {
                listener.onEmojiClick((Emojicon) parent.getAdapter().getItem(position));
            }
            InputHelper.input2OSC(editText, (Emojicon) parent.getAdapter().getItem(position));
        }
    });
    sGrid.setSelector(new ColorDrawable(android.R.color.transparent));
    return sGrid;
}

From source file:com.example.ibesteeth.git_keybordtest.emoji.EmojiPageFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    sGrid = new GridView(getActivity());
    sGrid.setNumColumns(KJEmojiConfig.COLUMNS);
    adapter = new EmojiGridAdapter(getActivity(), datas);
    sGrid.setAdapter(adapter);//from   ww  w  . ja  va2s .  c om
    sGrid.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            EditText editText = (EditText) getActivity().findViewById(R.id.emoji_titile_input);
            if (listener != null) {
                listener.onEmojiClick((Emojicon) parent.getAdapter().getItem(position));
            }
            InputHelper.input2OSC(editText, (Emojicon) parent.getAdapter().getItem(position));
        }
    });
    sGrid.setSelector(new ColorDrawable(getActivity().getResources().getColor(R.color.transparet)));
    return sGrid;
}

From source file:org.kymjs.emoji.EmojiPageFragment.java

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    sGrid = new GridView(getActivity());
    sGrid.setNumColumns(KJEmojiConfig.COLUMNS);
    adapter = new EmojiGridAdapter(getActivity(), datas);
    sGrid.setAdapter(adapter);//w w  w  . ja  v a 2 s. co  m
    sGrid.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            EditText editText = (EditText) getActivity().findViewById(R.id.emoji_titile_input);
            if (position + 1 != datas.size()) {
                InputHelper.input(editText, (Emojicon) parent.getAdapter().getItem(position));
            } else {
                InputHelper.backspace(editText);
            }
        }
    });
    sGrid.setSelector(new ColorDrawable(Color.TRANSPARENT));
    return sGrid;
}

From source file:edu.rowan.app.fragments.HomescreenFragment.java

/**
 * Construct the view for this fragment.
 * The core layout of this fragment is a CarouselView and grid of available buttons
 *///from  w ww. j a v  a 2s  .c  o  m
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // hide actionbar loading indicator TODO: move this elsewhere
    activity.showLoading(false);

    View view = inflater.inflate(R.layout.activity_main, container, false);
    RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.mainLayout);
    layout.setPadding(0, 20, 0, 0);

    CarouselView rowanFeatures = new CarouselView(inflater.getContext());
    // set view Id so that relativeLayout can position. Without an Id relativeLayout doesn't work
    rowanFeatures.setId(2342343);
    layout.addView(rowanFeatures);
    setTouchListener(rowanFeatures);

    GridView grid = new GridView(inflater.getContext());
    grid.setOnItemClickListener(this);
    grid.setNumColumns(3);
    grid.setVerticalSpacing(20);
    grid.setAdapter(new RowanAdapter());
    RelativeLayout.LayoutParams gridParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT);
    gridParams.addRule(RelativeLayout.BELOW, rowanFeatures.getId());
    gridParams.topMargin = 20;
    layout.addView(grid, gridParams);

    return view;
}

From source file:com.lm.im_huanxin.emoji.EmojiPageFragment.java

protected void initWidget(View rootView) {
    mPagerFace = (ViewPager) rootView.findViewById(R.id.frag_pager_face);
    pagePointLayout = (LinearLayout) rootView.findViewById(R.id.frag_point);

    int total = datas.size();
    int pages = total / ITEM_PAGE_COUNT + (total % ITEM_PAGE_COUNT == 0 ? 0 : 1);

    allPageViews = new GridView[pages];
    pointViews = new RadioButton[pages];

    for (int x = 0; x < pages; x++) {
        int start = x * ITEM_PAGE_COUNT;
        int end = (start + ITEM_PAGE_COUNT) > total ? total : (start + ITEM_PAGE_COUNT);
        final List<Emojicon> itemDatas = datas.subList(start, end);
        GridView view = new GridView(aty);
        EmojiAdapter faceAdapter = new EmojiAdapter(view, itemDatas);

        view.setNumColumns(7);/*from  w  ww. j a  v  a 2  s. c  o  m*/
        view.setHorizontalSpacing(1);
        view.setVerticalSpacing(1);
        view.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
        view.setCacheColorHint(0);
        view.setPadding(2, 0, 2, 0);
        view.setBackgroundResource(android.R.color.transparent);
        view.setSelector(android.R.color.transparent);
        view.setVerticalScrollBarEnabled(false);
        view.setGravity(Gravity.CENTER);
        view.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT));
        view.setAdapter(faceAdapter);

        view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (listener != null) {
                    Emojicon emoji = itemDatas.get(position);
                    if (DisplayRules.isDeleteEmojicon(emoji)) {
                        listener.selectedBackSpace(emoji);
                    } else {
                        listener.selectedEmoji(emoji);
                    }
                }
            }
        });
        allPageViews[x] = view;

        RadioButton tip = new RadioButton(aty);
        tip.setBackgroundResource(R.drawable.selector_bg_tip);
        RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(8, 8);
        layoutParams.leftMargin = 10;
        pagePointLayout.addView(tip, layoutParams);
        if (x == 0) {
            tip.setChecked(true);
        }
        pointViews[x] = tip;
    }

    PagerAdapter facePagerAdapter = new FacePagerAdapter(allPageViews);
    mPagerFace.setAdapter(facePagerAdapter);
    mPagerFace.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int index) {
            pointViews[index].setChecked(true);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

From source file:com.github.yuukis.businessmap.app.ContactsActionFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    mContact = (ContactsItem) getArguments().getSerializable(KEY_CONTACTS);

    MenuAdapter adapter = new MenuAdapter(getActivity(), R.layout.gridview_contents, ACTION_ITEMS);
    int columns = getResources().getInteger(R.integer.gridview_columns);
    GridView gridView = new GridView(getActivity());
    gridView.setNumColumns(columns);/*  w ww .j a  v  a2  s.co  m*/
    gridView.setAdapter(adapter);
    gridView.setOnItemClickListener(this);
    gridView.setBackgroundColor(Color.WHITE);
    String title = mContact.getName();

    return new AlertDialog.Builder(getActivity()).setTitle(title).setView(gridView)
            .setNegativeButton(android.R.string.cancel, null).create();
}