0

I've looked on SO for a solution to this and I can't find one that works for my situation. I'm a beginner with Android development, so let me know if I'm doing something obviously wrong.

First a little background on the structure of my app. I use a navigation drawer with fragments for each of the pages, similar to the navigation drawer example in the Android documentation. On one of the pages I want to dynamically add TextViews to the LinearLayout of the fragment for that page when some event happens. The event isn't important in this case - it just calls a method in my activity.

So in my activity I have a method - addText(). Whenever addText() is called, I want to create a TextView, and then add this to the LinearLayout in the fragment. (the LinearLayout with id diary_layout)

How do I do this?

I have tried the following method. When this method is called, I do not see the text in the page.

public void textAdd(){
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View contentView = inflater.inflate(R.layout.fragment_diary, null);
    LinearLayout diaryLayout = (LinearLayout) contentView.findViewById(R.id.diary_layout);
    TextView newTextView = new TextView(this);
    newTextView.setText("Testing");
    diaryLayout.addView(newTextView);
}

This is the structure of the layout file for the fragment:

<LinearLayout
android:id="@+id/theLinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/diary_layout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MyActivity$PlaceholderFragment">
    </LinearLayout>
</ScrollView>

Activity Layout:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
<!-- The navigation drawer -->
<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="10dp"
    android:background="#FFFFFF"/>

Thanks in advance!

user2900772
  • 319
  • 1
  • 4
  • 8
  • Your `contentView` is unused. Post your activity layout. – Simas Nov 25 '14 at 23:26
  • I use it in the third line of the method. LinearLayout diaryLayout = (LinearLayout) contentView.findViewById(R.id.diary_layout); – user2900772 Nov 25 '14 at 23:27
  • You need to attach contentView to the fragment or activity. There's nothing on your code that is doing it. – dannyroa Nov 25 '14 at 23:27
  • Yeah you insert an item to it. But where **is** your `contentView`. Nowhere. It hasn't been added to your activity yet. – Simas Nov 25 '14 at 23:28
  • http://stackoverflow.com/questions/9457194/android-scrollview-layout-wrap-content-maximum-size – snachmsm Nov 25 '14 at 23:28
  • How do I attach contentView to the fragment or the activity? Which should I attach to? – user2900772 Nov 25 '14 at 23:30
  • `contentView` is your fragment's layout. So you want to attach it to your activity's layout. – Simas Nov 25 '14 at 23:31
  • Do I use setContentView for this? What will attaching accomplish? The fragment is already being displayed when I go to the page in the navigation drawer. – user2900772 Nov 25 '14 at 23:39

2 Answers2

0

You definitely don't want to inflate the content each time you add text. That would load the initial layout again, which is not what you want.

You didn't show how you construct your fragments, but based on your description, that you want to add a new TextView to a LinearLayout in one of the existing Fragments. I would expect you would first fetch the fragment, and then call some method in the fragment to add text. Something like:

public void textAdd(){
    FragmentManager fm = getFragmentManager();
    MyFragment myFragment = (MyFragment) fm.findFragmentById(R.id.my_fragment);
    if (myFragment != null) {
        LinearLayout diaryLayout = (LinearLayout) myFragment.findViewById(R.id.diary_layout);
        TextView newTextView = new TextView(this);
        newTextView.setText("Testing");
        diaryLayout.addView(newTextView);
    }
}
Bruce
  • 2,377
  • 1
  • 17
  • 11
0

I think you need to add layout params to your textview.. try it this way..

  TextView newTextView = new TextView(this);
    newTextView.setText("Testing");
    newTextView .setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

    diaryLayout.addView(newTextView);
iMDroid
  • 2,108
  • 1
  • 16
  • 29