Reputation: 249
I have a RelativeLayout
in my app. How to add a new TextView
to the my layout?
I want to add a TextView
programatically, when the Activity
starts.
Now layout looks like this:
<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:background="@drawable/common_background" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".News" >
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I need result:
<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:background="@drawable/common_background" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="24dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".News" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Title" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Date" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:text="Content" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
My "News.java" code:
public class News extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.news);
}
}
Upvotes: 0
Views: 3134
Reputation: 4348
do something like that :- make an object and then add it to parent layout.
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout2);
// add text view
TextView tv = new TextView(this);
tv.setText("Dynamic Text!");
tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
ll.addView(tv);
Here is a simple tutorial for it. visit this link
Upvotes: 1
Reputation: 5731
First of all put an id to the RelativeLayout.
android:id="@+id/mylayout1"
Now, you can do it as,
TextView myText= new TextView(News.this);
myText.setText("This is inside Relative layout");
myText.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
((ViewGroup)findViewById(R.id.mylayout1)).addView(myText);
But I'll suggest you to use Custom list view instead. Which is more flexible and we can have more control on it. Here is a nice tutorial- Custom adapter
Upvotes: 0