android - How to scroll two-columns buttons in Activity -
i can include 6 buttons , displays correctly .
now want include more 6 buttons in scrollable view, can't handle matching available space, creating 2 rows (or columns if portrait).
can provide way achieve this?
references:
i have composed button created using following code:
public class imagebuttontext extends relativelayout { imagebutton button; textview label; holder holder; private void init() { layoutinflater li = (layoutinflater) getcontext().getsystemservice(context.layout_inflater_service); li.inflate(r.layout.big_button, this, true); button = (imagebutton) findviewbyid(r.id.button); label = (textview) findviewbyid(r.id.label); /*initialise button , label*/ } }
and following xml:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="@dimen/component_margin"> <imagebutton android:id="@+id/button" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/text_box" android:scaletype="fitcenter"/> <textview android:id="@+id/label" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_gravity="bottom" android:layout_margin="@dimen/component_margin" android:gravity="center" android:text="sample" android:textsize="@dimen/text_size"/> </relativelayout>
finally add main layout using:
<linearlayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center"> <imagebuttontext android:id="@+id/button1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" custom:buttonbackground="@drawable/states_green" android:src="@drawable/ic_safe_call" android:contentdescription="@string/btn1_info" android:text="@string/btn1text" android:textcolor="@color/text_green" /> <imagebuttontext android:id="@+id/button2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" custom:buttonbackground="@drawable/states_red" android:src="@drawable/ic_private_call" android:contentdescription="@string/btn2_info" android:text="@string/btn2text" android:textcolor="@color/text_red" /> </linearlayout>
notes
may different approach also. final goal display multiple buttons (each containing stretched image , bottom aligned text) in 2 rows if portrait or 3 columns if landscape. wrapped in scrollable view.
update: solved using recyclerview :d everyone
an easy , dynamic approach create custom list of buttons , put gridview 2 colums resp. rows.
this adapter did navigation has icon , text, maybe helps you. icon on left side can read in line: holder.textview.setcompounddrawableswithintrinsicbounds(item.get_icon(), 0, 0, 0); te second 1 icon above text.
anyway, think need custom layout, wich can create doing new xml file relative or linearlayout , put imageview , textview , give parameter in constructor layoutresourceid.
the height of listitem can define in layout xml file.
the gridview can configure different landscape , portrait
- in landscape mode layout layout-land/ used
in portrait mode layout layout-port/ used
public class navigationadapter extends arrayadapter<navitem> { list<navitem> data; context context; int layoutresourceid; @override public view getview(int position, view convertview, viewgroup parent) { view row = convertview; navholder holder; if(row == null) { layoutinflater inflater = ((activity)context).getlayoutinflater(); row = inflater.inflate(layoutresourceid, parent, false); holder = new navholder(); holder.textview = (textview)row.findviewbyid(android.r.id.text1); row.settag(holder); } else { holder = (navholder) row.gettag(); } navitem item = data.get(position); holder.textview.settext(item.get_title()); holder.textview.setcompounddrawableswithintrinsicbounds(item.get_icon(), 0, 0, 0); //holder.textview.setcompounddrawablepadding(10); return row; } public navigationadapter(context context, int layoutresourceid, list<navitem> data) { super(context, layoutresourceid, data); this.layoutresourceid = layoutresourceid; this.context = context; this.data = data; } static class navholder { textview textview; }
}
Comments
Post a Comment