Search code examples
javaandroidoverlapping

Can't start fragment from activity


I am trying to start a fragment from activity but it doesn't work, can somebody help?

This is the main.java

public class MainActivity extends FragmentActivity {

    Button clickButton;


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

        clickButton = (Button)findViewById(R.id.button1);
    click();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private void click()
    {
        clickButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Log.i("click","clickt");
             FragmentOne fragmentOne = new FragmentOne();
                 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                 transaction.replace(R.id.fragmentcontainer, fragmentOne);
                 transaction.addToBackStack(null);
                 transaction.commit();
            }

        });
    }

This is the main layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/container"
    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="com.example.testfragment.MainActivity" >



     <FrameLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2" 
    android:id="@+id/fragmentcontainer">
</FrameLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="57dp"
        android:text="Button" />

</RelativeLayout>

This is the Fragment

public class FragmentOne extends Fragment  {

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

And this is the fragment layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="TextView" />

</LinearLayout>

Solution

    • The problem is FrameLayout has width 0.

    Change this:

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" 
        android:id="@+id/fragmentcontainer">
    </FrameLayout>
    

    To this:

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

    Explanation: the weight attribute is a layout parameter of LinearLayout and not of RelativeLayout. The layout parameters are referred to the container of the View/ViewGroup (in this case the View/ViewGroup is your FrameLayout). So basically here the weight is completely ignored and the width set to 0, making the FrameLayout with the Fragment views effectively invisible