0

I'm trying to have a dynamic textview based on the xml file..... Please help me with my code

CODE

MessageViewPage.java

package com.dpn.pack;

public class MessageViewPage extends Activity {

TextView tv1=new TextView(this);
ScrollView sv;
String nickname,body;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.message_view_page);

    Bundle b = getIntent().getExtras();

    nickname= b.getString("nick");
    body=b.getString("body");

    LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = vi.inflate(R.layout.message_view_page, null);

    // fill in any details dynamically here
    TextView textView = (TextView) v.findViewById(R.id.textViewSender);
    textView.setText("Sender : "+nickname);

    // insert into main view
    View insertPoint = findViewById(R.id.textViewSender);
    insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

}

}

my XML file as follows

message_view_page.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"
android:padding="5dp" 
android:background="@drawable/view">

<TextView
    android:id="@+id/textViewSender"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="18dp"
    android:layout_marginTop="35dp"
    android:textColor="#FFFFFF"
    android:textSize="20dp" />

<TextView
    android:id="@+id/textViewBody"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textViewSender"
    android:layout_below="@+id/textViewSender"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="40dp"
    android:textSize="15dp"
    android:textColor="#F69DD1" />

<Button
    android:id="@+id/buttonReply"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="45dp"
    android:layout_marginBottom="50dp"
    android:text="Reply" />

<Button
    android:id="@+id/buttonListen"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginRight="45dp"
    android:layout_marginBottom="50dp"
    android:text="LIsten" />

</RelativeLayout>

I am having an error in this code

insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

like

enter image description here Please any one tell me the solution for my problem.

my target output demo like

enter image description here

Dipin.K.G
  • 127
  • 10
  • one thing take care whenever you ask question for getting error,`tell us what kind of error you getting rather then code`. Please paste logcat here. You didnt even mention which "error" in your detail. No one is god here – mayank_droid Jul 06 '12 at 06:07
  • check my updated image?i can't know .addView having an error?????? – Dipin.K.G Jul 06 '12 at 06:11
  • Take linear layout and then addview in that linearlayout. `Views can be added in layout, not views inside view.` – mayank_droid Jul 06 '12 at 06:17

2 Answers2

1

The View class doesn't have an addView method, you can't add a View to another View. The addView method belongs to the ViewGroup class.

user
  • 86,916
  • 18
  • 197
  • 190
  • @Dipin.K.G I would give you some code but I don't understand what you're trying to do. Right now you set a layout as the content view, then inflate the same layout again, you search for a `TextView` from that inflated layout, set the text and then try to add the inflated view to that `TextView`. It doesn't make any sense at all. – user Jul 06 '12 at 06:19
0

The View class doesn't have an addView method.Add dynamic TextView as:

first give layout id in 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:id="@+id/layout"
android:orientation="vertical"
android:padding="5dp" 
android:background="@drawable/view">

and in code :

final TextView tv1 = new TextView(this);
 LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
 LinearLayout.LayoutParams.FILL_PARENT,
  LinearLayout.LayoutParams.WRAP_CONTENT
   );
   tv1.setText("TextView");
 RelativeLayout layout = (RelativeLayout ) findViewById(R.id.layout);
 layout.addView(tv1, p);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • but my target is in my question...i'm update my question now...check...is it possible this way?????? Because of difficulty i prefer LayoutInflater – Dipin.K.G Jul 06 '12 at 06:21
  • @Dipin.K.G : if you want to use LayoutInflater then integrate my code in you current code and try to add textview using `v.addView(tv1)` instead of `RelativeLayout layout = (RelativeLayout ) findViewById(R.id.layout); layout.addView(tv1, p);` – ρяσѕρєя K Jul 06 '12 at 06:31