Reputation: 3
Result.java:
Fragment f = new GridViewFragement();
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.grid, f).commit();
Activity_result.xml :
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/grid"
android:orientation="vertical"
>
</LinearLayout>
Row_grid.xml: defines a grid item
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="@+id/item_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:src="@drawable/home" >
</ImageView>
<TextView
android:id="@+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="15sp" >
</TextView>
</LinearLayout>
grid.xml:
<GridView
android:id="@+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="4dp"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:fadeScrollbars="false"
/>
On create in grid view fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
// on create for activity
View view = inflater.inflate(R.layout.grid, container, false);
Bitmap homeIcon = BitmapFactory.decodeResource
(this.getResources(), R.drawable.home);
Bitmap userIcon = BitmapFactory.decodeResource
(this.getResources(), R.drawable.personal);
//
gridArray.add(new Item(homeIcon,"Home"));
gridArray.add(new Item(userIcon,"Personal"));
gridArray.add(new Item(homeIcon,"Home"));
gridArray.add(new Item(userIcon,"User"));
gridArray.add(new Item(homeIcon,"Building"));
gridArray.add(new Item(userIcon,"User"));
gridArray.add(new Item(homeIcon,"Home"));
gridArray.add(new Item(userIcon,"xyz"));
gridArray.add(new Item(homeIcon,"Home"));
gridArray.add(new Item(userIcon,"User"));
gridArray.add(new Item(homeIcon,"House"));
gridArray.add(new Item(userIcon,"Friend"));
gridView = (GridView) view.findViewById(R.id.gridView1);
customGridAdapter =newCustomGridViewAdapter
(view.getContext(),R.layout.row_grid,gridArray);
gridView.setAdapter(customGridAdapter);
return view;
}
Having issues trying to show all data of my gridview. it cuts halfway only the first three items are showing in a row. sorry, I'm unable to post pictures because I'm only rep 1. I have tried putting scrollviews and making the scroll bar appear but it just won't scroll or show all of its data
Upvotes: 0
Views: 4829
Reputation: 4297
I think you are concerned about the number of rows and columns displayed. If you are then you can use numColumns
attribute to set the number of columns and the number of rows rely on the amount of data and number of columns. see this for a better insight
Upvotes: 0
Reputation: 459
If you want to see all of the rows you need to set layout_height=fill_parent not layout_width=fill_parent and layout_height=wrap_content. It won't be able to show all the rows though unless you make them all really small anyway depending on the number of cells you have. If you have a lot of cells then it is pointless to even try to get them to show up on one screen.
My suggestion is to go for list view.
Upvotes: 1