Reputation: 23
How do I dynamically generate a RelativeLayout based on a xml layout (posted below)? The relative layouts are being put in a linear layout that is the child of a scroll view.
<RelativeLayout
android:id="@+id/post_0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<TextView
android:id="@+id/op_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:text="Username" />
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="@+id/op_username"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="31dp"
android:layout_marginLeft="31dp"
android:layout_marginTop="15dp"
app:srcCompat="@android:drawable/btn_star_big_on"
tools:srcCompat="@android:drawable/btn_star_big_on" />
</RelativeLayout>
Upvotes: 1
Views: 50
Reputation: 344
Let's suppose that you've all the Images and their TextView's texts in your Database, then create a RecyclerView
(if there are a relatively huge number of items) or a simple ListView
, and then create a CustomAdapter class to add your RelativeLayout to your main ListView
/ ScrollView
/ RecyclerView
.
To completely implement this first you need to have an Item class which can store a user's Username and Image ( please note that you need to pass Image URI as a string) in an Object.
public class Items {
private final int id;
private final String image;
public Items(int id,String image){
this.name=name;
this.image=image;
}
//Include all the Getters and Setters here
}
Then assuming you've all your elements into an ArrayList<Items>
, then you need to implement an Adapter class, called CustomAdaper which extends ArrayAdapter and need to write a function for getView
to get a particular view to fill in your ScrollView
/RecyclerView
/ListView
I've found a link to help you implement a CustomAdapter here.
And finally in the MainActivity, you've to add few lines,
final ListView listView = (ListView) findViewById(R.id.list_view); //name of list view file
final ArrayList<Items> arrayList = dbHelper.retrieveFromDB(); // Name of the Database class and the function to retrieve all the elements into an ArrayList in that class
CustomAdapter adapter = new CustomAdapter(this /* The current Context */, R.layout.list_item, arrayList); // Create an object of custom adapter initialize it with the desired data (in this case the arrayList) and the layout required ( layout mentioned in the question)
listView.setAdapter(adapter);
Upvotes: 1
Reputation: 1506
If it is a dynamic list that can have a large number of items use a recyclerview and place that relative layout inside a cardview. If the dynamic list has a maximum potential number of items that are also few in number, you could add the relative layout and hide them, then populate and then show them as needed, but i advice you lean toward the former.
Upvotes: 0