0

I want to set Imageview and Textview on same line in LinearLayout but the Imageview is always higher than the Textview.

Here is my code:

String b[] = cacBuocThucHien.split("#");
for (int j = 0; j < b.length; j++) {
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(30,30);
    lp.setMargins(50, 0, 0, 0);
    ImageView img = new ImageView(RecipesDetailActivity.this);
    TextView tv = new TextView(RecipesDetailActivity.this);
    img.setLayoutParams(lp2);
    img.setImageResource(R.mipmap.testic);
    tv.setLayoutParams(lp);
    tv.setText(b[j] + "\n");
    layoutHuongDan.addView(img);
    layoutHuongDan.addView(tv);
}
jon snow
  • 3,062
  • 1
  • 19
  • 31
naji kun
  • 167
  • 3
  • 10
  • You should add gravity to the views - http://stackoverflow.com/questions/3775705/android-set-the-gravity-for-a-textview-programmatically – Vaiden Apr 26 '15 at 07:17
  • You might be looking for [How to display image in Android's TextView?](http://stackoverflow.com/questions/5561981/how-to-display-image-in-androids-textview) – Sufian Apr 26 '15 at 07:35

4 Answers4

0

This You can use to put image in one line.. and it will work in all kind of layouts. Hope this will help you

  <LinerLayout>     
   <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
  <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
   ....... // Put how many TextViews you want

 <TextView
        android:id="@+id/textn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 </LinerLayout>

You can set visibility gone initially if you wanna show these textviews only after setting text. Now in your activity make an array of ids of textviews like this...

public static int[] textRows = {R.id.text1, R.id.text2, ........ R.id.textn};

then use for loop for initializing them and setting text and images like this

   TextView[] allTexts = new TextView[n];
   for (int i = 0; i < n; i++) {
        allTexts[i] = (TextView) findViewById(textRows[i]);
        allTexts[i].setText("your text");
        allTexts[i].setCompoundDrawables(left_image, null, null, null); 
    }

It will work. Try it out

Ankit Kumar
  • 3,663
  • 2
  • 26
  • 38
  • Thank for your help but I tried your method but with this way the imageview disappear and I can't add multi imageview and textview and either. – naji kun Apr 26 '15 at 17:22
  • I want to add one text with one image in front of it for per line, so I have to create texts and images with "for" loop and use linear layout. – naji kun Apr 27 '15 at 13:36
  • see my edits, and check it.. It should work as per requirement. – Ankit Kumar Apr 27 '15 at 14:17
  • I learn a new thing from you,thank you for your help. But this is not actually what I want, I just want a small icon in front of the first letter of the textview. How can I do that? – naji kun Apr 28 '15 at 14:38
  • Here. I want something like this http://s16.postimg.org/m423udgmd/Untitled.png But follow your instruction, it will be like this http://s11.postimg.org/g7al92eub/Untitled.jpg – naji kun Apr 29 '15 at 11:06
  • okay... Do one thing, create a custom listview, item of listview will hold an image and text as per ur snap, then populate listview using adapter. – Ankit Kumar Apr 29 '15 at 11:16
  • Ok, so there is no way we can do this with linear layout, right? My teammates don't want to use listview but I will ask them again to see if they change their mind. Thank you for your help ;) – naji kun Apr 29 '15 at 11:28
0

set textview drawable left no need of extra imageview

android:drawableLeft="@drawable/img"
amodkanthe
  • 4,345
  • 6
  • 36
  • 77
0

If you only want to show an icon beside that TextView, in my opinion you should consider using drawable icon feature of TextView

tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);

But if you would insist to using two views beside each other, using layout gravity would be useful in such circumstances.

lp.gravity= Gravity.CENTER_VERTICAL;
Community
  • 1
  • 1
frogatto
  • 28,539
  • 11
  • 83
  • 129
  • When I try 1st method, the layout will be blank. And when I try 2nd method, the icon will be on top of the textview. – naji kun Apr 26 '15 at 17:04
0

If you want to display only TextView and ImageView.. go for setting CompounDrawable in TextView itself..

But, for having two views side-by-side in LinearLayout programmatically try setting the orientation=horizontal, check out this link.

Community
  • 1
  • 1
Ankit Bansal
  • 1,801
  • 17
  • 34