HorizontalListView In Android? No. Use A HorizontalScrollView
Posted by admin on
May 14, 2011
There is no HorizontalListView in Android but I have seen someone made a class for that. Problem is, you cannot add any other View in the XML. It simply will not appear, only the HorizontalListView will be there.
The Android API does have a HorizontalScrollView class and I recommend you use this instead since you can place other widgets in the XML file. This sample shows a number of Button objects inside the scroll view class and I added them programmatically since the code I was working on depended on dynamic data.
You can simply place these same Buttons inside the XML.
<HorizontalScrollView android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/horizontal" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </HorizontalScrollView>
The MyActivity class is my class that inherits from Activity and is the parameter that is needed by all views since it requires a Context class. This code adds 10 buttons to the HorizontalScrollView class and sets an OnClickListener to activate any code when you click on any of the buttons.
LinearLayout ll = (LinearLayout) a.findViewById(R.id.horizontal); int how_many_buttons = 10; for (int i=0; i<how_many_buttons; i++) { Button btn = new Button(MyActivity.this); btn.setHeight(20); btn.setText(String.valueOf(i)); btn.setOnClickListener(MyActivity.this); ll.addView(btn); } public void onClick(View v) { if (v instanceof Button) System.out.println("bt>" + ((Button) v).getText()); } }









