user2529011
user2529011

Reputation: 733

Fragment wont show in activity

I am very new to android development. I went through several tutorials on fragments but for some reason i cant seem to add a fragment from my main view. I'm not trying to do anything painfully complicated. All I want is to create my fragments and add them to my main activity. That's it. But every time I run my application all I get is a blank screen. I debug it and it is going to the fragment. This leads me to believe that the issue may be in the xml laout file (maybe).

Here is my MainActivity.java

package com.android.myCompany.nameOfApp;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

        // Begin the transaction
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.foo_frame_layout, new FooFragment());
        ft.commit();
    }
}

Here is my Fragment.cs

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * A simple {@link Fragment} subclass.
 */
public class FooFragment extends Fragment {

    public FooFragment () {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        TextView textView = new TextView(getActivity());
        textView.setText(R.string.hello_blank_fragment);
        return textView;
    }
}

Here is my layout activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@drawable/background_portrait">

    <FrameLayout
        android:id="@+id/copyright_frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
</RelativeLayout>

Here is my fragment_foo.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/copyright_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FooFragment">
<!-- TODO: Update blank fragment layout -->

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="wrap_content"
        android:layout_height="102dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="8dp"
        android:adjustViewBounds="false"
        android:cropToPadding="false"
        app:layout_constraintBottom_toTopOf="@+id/imageView6"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.504"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/logo" />

    <ImageView
</RelativeLayout>

Can anyone tell me what is wrong with my set up? Many thanks in advance.

Upvotes: 0

Views: 87

Answers (2)

Jeel Vankhede
Jeel Vankhede

Reputation: 12118

Change with this in your fragment class :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_foo, container, false); // This line will inflate your fragment_foo layout and return it's rootview for fragment inflation
}

and then find your fragment views in onViewCreated method of fragment.

Upvotes: 2

Amit Jangid
Amit Jangid

Reputation: 2889

Try adding this in your fragment class...

public static FooFragment newInstance()
{
    return new FooFragment();
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_layout, container, false);

    // work with your ui code here.
    return view;
}

And call this method in your activity...

// Begin the transaction
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.foo_frame_layout, FooFragment.newInstance());
ft.commit();

Upvotes: 0

Related Questions