android layout - Best way to create buttons dynamically -
i want create button dynamically in application. buttons need created based on items fetched database. best way achieve this. should go grid layout or linear layout. layout simple max 3 buttons per row. once first row complete buttons should placed in second row.
i scanned lot of similar questions(some had grid layout other using linear layout) unable decide optimum way implement this.
i complete newbie in android application, code snippets helpful. apologies if feels duplicate question (i searched lot before posting didn't find appropriate answer layout used.)
thanks.
please try use gridview same bellow code.
// in xml write code <gridview android:id="@+id/calendar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:numcolumns="3" /> // grid adapter public class gridadapter extends baseadapter { private final context _context; private final list<string> list; public gridadapter(context context, arraylist<string> list) { super(); this._context = context; this.list = list; } public string getitem(int position) { return list.get(position); } @override public int getcount() { return list.size(); } public view getview(int position, view convertview, viewgroup parent) { button button = new button(_context); button.settext("button" + list.get(position)); return button; } @override public long getitemid(int position) { // todo auto-generated method stub return 0; } } /// in oncreate gridview.setadapter(new gridadapter(getapplicationcontext(),list);
Comments
Post a Comment