2

I wanted to add a button programmatically to my fragment in a tabbed activity. Somehow, the addView() method doesn't work. There are no errors, no exceptions, it just doesn't show the button.

That's the code of my MainActivity's onCreate() and onCreateView():

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    //Fragments erstellen
    MyFragment myfragment = new MyFragment();
    FragmentManager fragmentManager = getSupportFragmentManager();
    final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    fragmentTransaction.add(R.id.container, myfragment);

    fragmentTransaction.commit();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_my, container, false);
    return rootView;
}

And that's the code of my fragment's onCreateView():

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View rootview = inflater.inflate(R.layout.fragment_my, container, 
    false);
    LinearLayout l_layout = (LinearLayout) 
    rootview.findViewById(R.id.LinLay);
    Button b = new Button(getActivity());
    b.setText("test");
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    b.setLayoutParams(params);
    l_layout.addView(b);
    return rootview;
}

I guess, it's just a simple mistake, but I just can't find it...

Edit: AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.test2">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.user.test2.MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

fragment_my.xml:

<FrameLayout 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"
tools:context="com.example.user.test2.layout.MyFragment">

<!-- TODO: Update blank fragment layout -->

<LinearLayout
    android:id="@+id/LinLay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"></LinearLayout>
</FrameLayout>
Zimbaa
  • 45
  • 1
  • 1
  • 11

2 Answers2

0

Try this:

((LinearLayout)l_layout.addView(b);
n_r
  • 589
  • 7
  • 17
0

Is it not because you set you Fragment to your Viewpager fragmentTransaction.add(R.id.container, myfragment);?

To display the fragment you should either:

  • Add the fragment using the ViewPager adpater
  • Use another kind of container (such as a FrameLayout) to add the Fragment directly into it
Eselfar
  • 3,759
  • 3
  • 23
  • 43
  • 1
    Works, many thanks! :D - added the fragment using the adapter and the following instructions: https://stackoverflow.com/questions/18413309/how-to-implement-a-viewpager-with-different-fragments-layouts – Zimbaa Aug 17 '17 at 10:31