Search code examples
androidlistviewtextviewlistadapter

How to add two TextView one after the other in a ListView?


I am creating a ListView dynamically and want to add two TextViews in a single ListItem (one Below the other). If I add both TextView they are overlapping each other (The reason I m adding two TextView is because both have to have different text size and styles.)

here is the code... inside the custom ListAdapter getView Method

FrameLayout v = new FrameLayout(AppStarter.this);
TextView title = new TextView(AppStarter.this);
TextView date = new TextView(AppStarter.this);
title.setTextSize(16);
title.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
title.setText(values[position]);
date.setText("Date : " + AppStarter.this.date[position]);
date.setTextSize(14);
v.addView(title);
v.addView(date, 1);
return v;

both title and date are overlapping each other I even tried addView(date,1);it didnt workout.


Solution

  • Either use LinearLayout or RelativeLayout.

        LinearLayout linear = new LinearLayout(this);
        linear.setOrientation(LinearLayout.VERTICAL);
        TextView tv1 = new TextView(this);
        tv1.setText("First Text!");
        TextView tv2 = new TextView(this);
        tv2.setText("Second Text!");
        linear.addView(tv1);
        linear.addView(tv2);
    

    pass the layout in adapter of listview. or Add RelativeLayout and place views relatively on same