I need to center elements in each row so they will be like in this mockup. I\'ve been searching if there is any layout that works that way without look. items are centered i
you can add some layouts dynamically, for example:
1- create a LinearLayout in java code and customize it (gravity,...)
2- add some icons to linearLayout
3- add linearLayout to your adapter
4- repeat 1,2,3
// : declare new horizontal linearLayout
ImageView myIcons[nomOfIcons];
// : add all icons to myIcons
for(int i=1; i<=nomOfIcons; i++){
linearLayout.addView(myIcons[i - 1]);
if(i%numOfIconsInOneHorizontalLinearLayout==0) {
results.add(linearLayout); // add linearLayout to adapter dataSet
// : declare new horizontal linearLayout
}
}
if(n%numOfIconsInOneHorizontalLinearLayout!=0) // add last linearLayout if not added in the loop
results.add(linearLayout);
mAdapter.notifyDataSetChanged(); // update adapter
Take LinearLayout
in your RecyclerView
's item row layout then give android:layout_gravity="center"
to LinearLayout
.
For each row of images you have to take different LinearLayout
.
Here is example code:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/image1"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:src="@drawable/image1" />
<ImageView
android:id="@+id/image2"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:src="@drawable/image2" />
<ImageView
android:id="@+id/image3"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:src="@drawable/image3" />
</LinearLayout>