Reputation: 2828
i need to set a image dynamically before to text view on the below mentioned code , so how to do this.?my code is below..
if (attchStringArray.length > 0) {
LinearLayout attachemntLayout = new LinearLayout(mainContext);
LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
TextView attNameView = new TextView(mainContext);
ImageView Ivbackground= new ImageView(mainContext);
attNameView.setText(attchStringArray[2]);
attachemntLayout.addView(attNameView, layoutParam);
attachemntLayout.setBackgroundResource(R.drawable.bg_for_answers);
attachmentsection.addView(attachemntLayout);
}
Upvotes: 0
Views: 337
Reputation: 1531
I have added 2 lines in your original code. Check the lines that are preceded with comments.
EDIT: You should load the image view with some image using eg. ImageView.setImageResource()
or ImageView.setImageDrawable()
or ImageView.setImageBitmap()
.
if (attchStringArray.length > 0) {
LinearLayout attachemntLayout = new LinearLayout(mainContext);
// Changed width to WRAP_CONTENT
LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView attNameView = new TextView(mainContext);
ImageView Ivbackground= new ImageView(mainContext);
// You should put some content in the ImageView
// ...
attNameView.setText(attchStringArray[2]);
// I am adding the image view before the text view
attachemntLayout.addView(Ivbackground, layoutParam);
attachemntLayout.addView(attNameView, layoutParam);
attachemntLayout.setBackgroundResource(R.drawable.bg_for_answers);
attachmentsection.addView(attachemntLayout);
}
Upvotes: 1
Reputation: 2534
Try this setBackgroundDrawable()
for your TextView
yourTextView.setBackgroundDrawable(R.drwable.your_image_name);
Upvotes: 0