Walter
Walter

Reputation: 189

How to add an Imageview near to a TextView dynamically from an existing layout?

I have textview with imageview. Now textview will be generated dynamically. But how to add imageview along with that?

xml:

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true">

      <LinearLayout
                        android:id="@+id/ll_rl_block4_1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:layout_marginBottom="@dimen/growth_rl_block_title_table"
                        android:gravity="center_vertical"
                        android:orientation="horizontal">

                        <ImageView
                            android:id="@+id/iv_block4_1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginRight="@dimen/growth_rl_block_title_table"
                            android:background="@drawable/dot_green" />

                        <TextView
                            android:id="@+id/tv_block4_body1_1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:gravity="center_vertical"
                            android:text="@string/growth_not1" />
                    </LinearLayout>
    </RelativeLayout>


    </RelativeLayout>

In MainActivity:

public class MainActivity extends BaseActivity {

 String[] word = {"This is text1.So it should be single line", "This is text2", "This is text3"};

TextView broadcastMessage;
ImageView greenDot;

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

broadcastMessage = (TextView) findViewById(R.id.tv_block4_body1_1);
 greenDot = (ImageView) findViewById(R.id.iv_block4_1);
        for (int i = 0; i < word.length; i++) {
            broadcastMessage.append(word[i]);
            broadcastMessage.append("\n");
        }
        }

If you see the xml, there is an imageview too. So now textview is generated dynamically but the imageview is not proper corresponding to textview. Imageview will be in left side and corresponding near to that is TextView. But the dot is missing near to the textview when generating dynamically from xml.

Upvotes: 0

Views: 43

Answers (1)

Aj 27
Aj 27

Reputation: 2427

you can do it by adding a compound drawable (left, top, right, bottom) as follows:

broadcastMessage.setCompoundDrawablesWithIntrinsicBounds( getContext().getResources().getDrawable( R.drawable.smiley ), null, null, null);

Upvotes: 1

Related Questions