Create a New Textview Programmatically Then Display It Below Another Textview

creating a new textview programmatically below an edittext

Use this layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/new_formulas"
>

<Button
android:textSize="30dp"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add"
android:id="@+id/add"
/>
<EditText
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/add_hint"
android:id="@+id/add_hint"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/add"
/>
<LinearLayout
android:id="@+id/hints"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/add_hint"
android:orientation="vertical"/>
</RelativeLayout>

Now every child TextView you can add to the hints LinearLayout just like you did above, no need to use rules.

Important: When you use LayoutParams always use it from the parent layout. For example here it should be LinearLayout.LayoutParams.

set textview below textview in android programmatically

you can addRule on LayoutParam for RelativeLayout

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.MATCH_PARENT);

rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

TextView tv = new TextView(getActivity());
tv.setText("Test");
tv.setCompoundDrawablesWithIntrinsicBounds(0,
0,
R.drawable.rsz_page_pic,
0);
tv.setCompoundDrawablePadding(20);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setLayoutParams(rlp);
relativeLayout.setBackgroundColor(Color.WHITE);
relativeLayout.addView(tv);

How to add a new Textview evertime Button is clicked

In Your button onClickListener You need to create new textview and then add it to Your LinearLayout. Look at this StackOverflow: response. Linked thread shows similar problem.

Be aware that if You want to have a lot of those TextViews, then You should consider using RecyclerView.

Creating multiple TextViews programmatically

In your 2nd activity, 1st you need a LinearLayout with attribute
android:orientation="vertical" defined in AndroidManifest file.
i.e:

<LinearLayout 
android:id="@+id/llMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>

Then you can write the code as shown below in the java file:

LinearLayout m_ll = (LinearLayout) findViewById(R.id.llMain);
for(int i=0;i<num;i++)
{
TextView text = new TextView(this);
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
text.setText(""+i);
m_ll.addView(text);
}

I still believe that the approach suggested by Frank D. would be optimal, but this is just for your reference, Hope it helps. :)

How to programmatically create textviews and show as grid

Building upon the linked question this is what will work for you

    String[] textArray = {"One", "Two", "Three", "Four","Five","Six"};
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
for( int i = 0; i < textArray.length; i+=2 )
{
LinearLayout linearLayout1 = new LinearLayout(this);
linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
TextView textView1 = new TextView(this);
textView1.setText(textArray[i]);
TextView textView2 = new TextView(this);
textView2.setText(textArray[i+1]);
linearLayout1.addView(textView1);
linearLayout1.addView(textView2);
linearLayout.addView(linearLayout1);
}
<mainLayout>.addView(linearLayout);

Further more you can add LinearLayout.LayoutParams to set the content however you like. And if you'd like to center it then you can use weights

Relative Layout adding views programmatically one below other not aligning correctly

I was using the index of the for loop as id for the textViews which I changed to make Android generate Id for it as below and it is working fine now as expected.

I changed the line

textView.setId(i)

to

textView.setId(ViewGroup.generateViewId())

Thanks.



Related Topics



Leave a reply



Submit