Android programmatically add buttons to layout from string array
Posted
by
Luke Batley
on Stack Overflow
See other posts from Stack Overflow
or by Luke Batley
Published on 2013-10-29T15:33:45Z
Indexed on
2013/10/29
15:54 UTC
Read the original article
Hit count: 356
Does anyone know how to programatically add buttons to the layout from an array? I have an array of titles and icons that i have used for a list view and now i'm wanting to reuse the same arrays to create a layout as displayed here
so the layout adds a button for each item in the array and arranges them in 3's according to device width. is this possible to do? I have tried using a custom Array Adapter to generate the Grid but getting a null pointer error at the point
menuGrid = (GridView) findViewById(R.id.gridView1);
MenuItemsSetup menuData[] = new MenuItemsSetup[]
{
new MenuItemsSetup(R.drawable.ic_menu_icon1, "menuItem1"),
new MenuItemsSetup(R.drawable.ic_menu_icon2, "menuItem2"),
new MenuItemsSetup(R.drawable.ic_menu_icon3, "menuItem3"),
new MenuItemsSetup(R.drawable.ic_menu_icon4, "menuItem4"),
new MenuItemsSetup(R.drawable.ic_menu_icon5, "menuItem5"),
new MenuItemsSetup(R.drawable.ic_menu_icon6, "menuItem6"),
new MenuItemsSetup(R.drawable.ic_menu_icon7, "menuItem7"),
new MenuItemsSetup(R.drawable.ic_menu_icon8, "menuItem8")
};
String[] menuBgColours = {"#FFFFFF","#FFFFFF", ,"#888888", "#FFFFBB", "#BBFFFF","#000000", "#666666"};
CustomArrayAdapter adapter = new CustomArrayAdapter(this,
R.layout.drawer_list_item, menuData, menuBgColours);
menuList.setAdapter(adapter);
menuGrid.setAdapter(adapter);
© Stack Overflow or respective owner