0

enter image description here

I am new with android. I have add textview dynamically in .java file with text apple but I want to add it on the top. How can I do that.

This is my code......in .jave file

  TextView lblname;
  LinearLayout linearlayout;
  lblname = new TextView(this);
  linearlayout = (LinearLayout) findViewById(R.id. linearlayout);
  linearlayout.addView(lblname);

Thank You in advance..

Vandana Rao
  • 133
  • 3
  • 13
  • possible duplicate of [how to generate dynamic textview in android?](http://stackoverflow.com/questions/5727771/how-to-generate-dynamic-textview-in-android) – Janak Nirmal Apr 29 '13 at 05:02
  • in your .xml file you get ImageView in Linear layout? and you add textView in in Linear Layout is common.? – Mr.Sandy Apr 29 '13 at 06:01
  • 1
    where is your linearlayout actually?? is it above the apple image or below it?? place a LinearLayout above the apple image and add the textview to that layout.. – Deepzz Apr 29 '13 at 06:03

3 Answers3

0

Simply specify the index where you want to add the child, as a second parameter for the addView.

For example, to add it at the top:

linearlayout.addView(lblname, 0);

See reference: http://developer.android.com/reference/android/view/ViewGroup.html#addView(android.view.View, int)

tbkn23
  • 5,205
  • 8
  • 26
  • 46
0

Try this

lblname = new TextView(this);
lblname.setText("APPLE");
lblname.setId(5); // id should be unique
lblname.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT));
linearlayout.addView(lblname,index);//specify your index
Linga
  • 10,379
  • 10
  • 52
  • 104
  • It is not working with my requirement...The textview is still display at the same place. Do you have other solution... – Vandana Rao Apr 29 '13 at 05:12
0

modify your last line to:-

linearlayout.addView(lblname, index); 

replace index with the position you want to add the view i.e, 0 if in the beginning or linearlayout.getChildCount()-1 if prior to the last view in you layout

d3m0li5h3r
  • 1,957
  • 17
  • 33