0

I am trying to create a TextView programmatically as shown in the image on the left side, but I'm out of luck. For some reason, the "SKIP" and ">" gets duplicated in two rows as shown in the image on the right side. I attach my relative code below for reference. Note that the left image is done with a static xml layout. The reason to create the TextView programmatically is because I need to replace the TextView with a button at the 4th slide. Please help!

Relative xml layout portion

<RelativeLayout
    android:id="@+id/view_pager_indicator"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_alignParentBottom="true"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="24dp"
    android:gravity="center">

    <LinearLayout
        android:orientation="vertical"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/pager_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center"
            android:layout_marginBottom="20dp">

<!--<TextView--> (Want to replace this part with programmatic creation)
                <!--android:layout_width="wrap_content"-->
                <!--android:layout_height="wrap_content"-->
                <!--android:gravity="center"-->
                <!--android:textSize="20dp"-->
                <!--android:text="Welcome to Central Park!" />-->

        </LinearLayout>

        <RelativeLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/btn_skip"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dip"
                android:gravity="left"
                android:text="SKIP"
                android:textSize="20dp" />

            <LinearLayout
                android:id="@+id/view_pager_countDots"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="horizontal" />

            <TextView
                android:id="@+id/btn_next"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="15dip"
                android:gravity="right"
                android:text=">"
                android:textSize="30dp" />
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

Java code

pager_info = (LinearLayout) view.findViewById(R.id.pager_info);
TextView msg = new TextView(MyActivity.this);
msg.setText(message[0]);
msg.setTextSize(16);
msg.setLayoutParams(new LinearLayout.LayoutParams(
    new LayoutParams(LayoutParams.MATCH_PARENT,
    LayoutParams.WRAP_CONTENT)));
msg.setGravity(Gravity.CENTER);
pager_info.addView(msg);
D. Wang
  • 165
  • 1
  • 9
  • 3
    Possible duplicate of [Create a new TextView programmatically then display it below another TextView](http://stackoverflow.com/questions/4394293/create-a-new-textview-programmatically-then-display-it-below-another-textview) – Preston Garno Mar 11 '17 at 17:19
  • Thanks for your prompt reply. No, I only called my Java code in the onCreate() method of the Activity once. – D. Wang Mar 11 '17 at 17:28
  • I've tested your code, SKIP" and ">" is showing only single row (as you expected). I think the problem is elsewhere. Please show your full code. – tahsinRupam Mar 11 '17 at 18:00
  • tahsinRupam, You are right. There was a problem in my frame layout. I've fixed the problem. Thanks a lot for your help! – D. Wang Mar 12 '17 at 01:49

1 Answers1

1

enter image description here

Hello, this is my example.

Get your layout, create a new TextView and create a "Drawable" with your image.

    RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.activity_form_result);

    TextView textView = new TextView(this);
    textView.setText("XPTO");
    textView.setPadding(10,10,10,10);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.setMargins(0, 15, 5, 0);
    params.alignWithParent = true;
    textView.setLayoutParams(params);
    textView.setGravity(Gravity.CENTER);

    Drawable img = getBaseContext().getResources().getDrawable(R.mipmap.ic_check_circle_black_24dp);
    img.setBounds( 0, 0, 60, 60 );
    textView.setCompoundDrawables( img, null, null, null );
    relativeLayout.addView(textView);
Luis Camargo
  • 91
  • 1
  • 4