PianomaR
PianomaR

Reputation: 53

Display and replace fragment from Activity

I am very new to Android development. I have an Activity (LoginActivity), which contains the background and the toolbar. Inside this activity I'd like to include two fragments.

The first one (login) is displayed when the activity starts, the second (sign-up) replaces the (login) fragment when a user clicks the register link.

My problem is that the (login) fragment isn't being shown when the activity starts. I am trying to do this by replacing a FrameLayout (contentFragment) with the login fragment, but it doesn't get replaced. No errors. It just seems as if the replacement code in onCreate() in LoginActivity isn't working

This is my attempt:

LoginActivity Activity:

package com.evently.pianomar.evently;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {

    private static final String TAG = "LoginActivity";
    private static final int REQUEST_SIGNUP = 0;

    private Button _loginButton, _test;
    private TextView _signupLink;
    private EditText _passwordText;
    private EditText _emailText;

    android.support.v7.widget.Toolbar mActionBarToolbar;

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

        LoginFragment fragment = new LoginFragment();
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.contentFragment, fragment);
        transaction.commit();
    }

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">
    <FrameLayout
        android:id="@+id/contentFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

LoginFragment

public class LoginFragment extends Fragment {

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

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

fragment_login.xml (stripped down)

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:paddingLeft="24dp"
    android:paddingRight="24dp"
    android:gravity="center"
    android:background="#e3e3e3"
    android:layout_weight="70">

 <ImageView
    android:layout_width="96dp"
    android:layout_height="96dp"
    android:src="@drawable/logo"
    android:layout_centerHorizontal="true" />
</LinearLayout>

What I've tried so far based on questions I read here without success:

Upvotes: 0

Views: 3458

Answers (2)

shadow chan
shadow chan

Reputation: 1

You can set the container as a null and it won't have a default container to replace.

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_login, ***null***, false);
}

Then, when you call the method replace you can set the container by yourself. Here a sample:

myFragmentTransaction.replace(R.id.myContainer, new MyFragment())

The "myContainer" part is the id for the view you have in your layout like a LinearLayout, RelativeLayout or FrameLayout.

Upvotes: 0

cycki
cycki

Reputation: 188

I guess your problem is in fragment_login.xml:

android:layout_width="0dp"

replace with:

android:layout_width="wrap_content"

LoginFragment was loaded to FrameLayout, but it is just invisible.

Upvotes: 1

Related Questions