0

I am making an app similar to facebook where i have a news feed which shows all the posts. However, the number of post in this feed varies with each user, depending on the amount of friends they have and how many posts they have made. I want to create a CardView for every post item which will show the name of the user who posted it, the time it was posted and the actual post itself. The layout will be split horizontally once with the post in the bottom half and the name of the person with the time it was posted on the top half.

Currently i can only find ways to do this by altering the xml text, but i want to create these CardViews as and when i need them, in my Java code.

I know i can alter TextViews by using such functions as setText() & setTextSize(). Is there a way for me to use similar functions on a CardView which can alter the base layout.

for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

   CardView TVcard = new CardView(Home.this);

   TextView TVposts = new TextView(Home.this);
   TextView test = new TextView(Home.this);

   TVcard.addView(TVposts);
   TVcard.addView(test);

   test.setText("test");
   test.setBackgroundColor(Color.GREEN);


   TVposts.setText(snapshot.getValue().toString());
   TVposts.setTextColor(Color.WHITE);
   TVposts.setTextSize(25);
   TVposts.setBackgroundColor(Color.BLACK);
   TVposts.setHeight(200);

   linearLayout.addView(TVcard);
   LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) TVcard.getLayoutParams();
   params.setMargins(0, 20, 0, 0);
   TVcard.setLayoutParams(params);
}

Thanks

Boken
  • 4,825
  • 10
  • 32
  • 42
  • This does not sound correct. Read about RecyclerView (or ListView). – Henry Mar 11 '19 at 12:04
  • @Henry, are the purpose of these to house multiple cards? I have just had a look but am still unsure how to create this purely using java. Could you give an example please? Sorry i am quite new to this space – Jedd Hopkins Mar 11 '19 at 12:10
  • What is base layout?, and what attribute of CardView do you want to change. – Guy Luz Mar 11 '19 at 12:11
  • I am using a vertical linear layout. At the moment my CardView has two TextViews in it. However they show on top of each other. I would like to separate them horizontally with one TextView above the other – Jedd Hopkins Mar 11 '19 at 12:14
  • You say "altering the xml text" and than you say "create these CardViews". You can use TextView directly, you don't need CardView to show text. To clarify CardView dont have text attribute, you put TextView inside if you want to show text. – Guy Luz Mar 11 '19 at 12:14
  • I don't see why generating the view with Java would be necessary. With a RecyclerView you would create one view that is used by each item (i.e. the individual cards). The RecyclerView is responsible to spawn as many instances as necessary. – Henry Mar 11 '19 at 12:14

0 Answers0