I'm trying to create something similar to what Facebook has done below:
I have created an empty RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fav_cake_rl">
</RelativeLayout>
Then I wrote the following code to dynamically create my views. As I would like 4 circles in one row going across the page (facebook only had 3), I obtained the DisplayMetrics which i placed into an object called "dm" in the code below and then divided the widthpixels by 4.
The layouts are created dynamically in a recyclerview.
RelativeLayout rl = (RelativeLayout) v.findViewById(R.id.fav_cake_rl);
CircularImageView circularImageView = new CircularImageView(context);
RelativeLayout.LayoutParams circlellp = new RelativeLayout.LayoutParams(dm.widthPixels/4, dm.widthPixels/4);
circularImageView.setLayoutParams(circlellp);
circularImageView.setId(1);
Drawable drawable = (Drawable) ContextCompat.getDrawable(context, R.drawable.cake);
circularImageView.setImageDrawable(drawable);
rl.addView(circularImageView);
final TextView groupname = new TextView(context);
RelativeLayout.LayoutParams textLp = new RelativeLayout.LayoutParams(dm.widthPixels/4, RelativeLayout.LayoutParams.WRAP_CONTENT);
textLp.addRule(RelativeLayout.BELOW, 1);
groupname.setLayoutParams(textLp);
groupname.setGravity(Gravity.CENTER_HORIZONTAL);
groupname.setText("StrawBerry Fields");
rl.addView(groupname);
The end result looks like this:
I don't really want the circles to be so big or so close together so then I added padding and margin to the imageview:
CircularImageView circularImageView = new CircularImageView(context);
RelativeLayout.LayoutParams circlellp = new RelativeLayout.LayoutParams(dm.widthPixels/4, dm.widthPixels/4);
**circlellp.setMargins(margin, margin, 0, 0);**
circularImageView.setLayoutParams(circlellp);
**circularImageView.setPadding(32, 32, 32, 0);**
circularImageView.setId(1);
Drawable drawable = (Drawable) ContextCompat.getDrawable(context, R.drawable.cake);
circularImageView.setImageDrawable(drawable);
rl.addView(circularImageView);
Now, it looks like this:
I do not want the text to be so far away from the imageview and it appears that while the padding has reduce the imageview, it has also added an extra space between the imageview and the text.
How can I get the textview and the imageview to be close to one another?
UPDATE:
I tried it with XML instead by doing the following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fav_group_rl">
<com.example.simon.customshapes.CircularImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/circlecake"
android:layout_centerHorizontal="true"
android:padding="24dp"
android:layout_margin="8dp"
android:src="@drawable/cake"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@id/groupname"
android:text="StrawBerry Fields"
android:layout_centerHorizontal="true"
android:layout_below="@+id/circlecake"/>
</RelativeLayout>
To get 4 of them to display properly, I tried this:
RelativeLayout rl = (RelativeLayout) v.findViewById(R.id.fav_group_rl);
RelativeLayout.LayoutParams llp = new RelativeLayout.LayoutParams(dm.widthPixels/4, RelativeLayout.LayoutParams.WRAP_CONTENT);
rl.setLayoutParams(llp);
When I ran this app, nothing was displayed.