Tuesday, 16 October 2018

Multiple fragments in one activity example

Create New project Multiple Fragment with empty activity

// activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.krishanandroid.multiplefragment.MainActivity">

    <FrameLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/container" >
    </FrameLayout>
</LinearLayout>
// fragment-login.xml
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.krishanandroid.multiplefragment.MainActivity">

    <EditText 
       android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="enter email"
        android:id="@+id/editText1"
        android:textSize="24sp"
        android:gravity="center_horizontal"
        android:textColor="@color/colorAccent"/>

    <EditText
        android:layout_width="match_parent" 
       android:layout_height="wrap_content"
        android:hint="enter password"
        android:id="@+id/editText2"
        android:textSize="24sp"
        android:gravity="center_horizontal"
        android:textColor="@color/colorAccent"/>


    <Button 
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:text="Login"
        android:layout_gravity="center"
        android:textSize="16sp"/>

</LinearLayout>
// fragment_welcome.xml
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.krishanandroid.multiplefragment.MainActivity">

    <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:text="Welcome user"
        android:textSize="24sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView2"
        android:text="Your name is: "
        android:textSize="24sp"/>

</LinearLayout>
//WelcomeFragment.java
package com.krishanandroid.multiplefragment;

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;

public class WelcomeFragment extends Fragment {

    TextView mTv1, mTv2;

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

       @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment        
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
        mTv1 = view.findViewById(R.id.textView1);
        mTv2 = view.findViewById(R.id.textView2);

        Bundle bundle = getArguments();
        if (bundle!=null)
        {
            String email = bundle.getString("email");
            String pwd = bundle.getString("pwd");

            mTv1.setText("Hi "+email);
            mTv2.setText("Pwd: "+pwd);
        }
        return view;
    }
}
// LoginFragment.java
package com.krishanandroid.multiplefragment;

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.Button;
import android.widget.EditText;

public class LoginFragment extends Fragment {

    EditText        mEmail, mPassword;
    Button          mButtonLogin;

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

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

        mEmail = view.findViewById(R.id.editText1);
        mPassword = view.findViewById(R.id.editText2);
        mButtonLogin = view.findViewById(R.id.button1);

        mButtonLogin.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                String email = mEmail.getText().toString();
                String pwd = mPassword.getText().toString();

                MainActivity m = (MainActivity) getActivity();
                m.loadWelcome(email, pwd);
            }
        });
        return view;
    }

}

// MainActivity.java
package com.krishanandroid.multiplefragment;

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

public class MainActivity extends AppCompatActivity {

    FragmentManager                 fragmentManager;
    FragmentTransaction             fragmentTransaction;

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

        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();

        LoginFragment loginFragment = new LoginFragment();
        Bundle bundle = new Bundle();
        loginFragment.setArguments(bundle);

        fragmentTransaction.add(R.id.container,loginFragment);
        fragmentTransaction.commit();

    }

    public void loadWelcome(String email, String pwd) {

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction  fragmentTransaction = manager.beginTransaction();

        WelcomeFragment welcomeFragment = new WelcomeFragment();

        Bundle bundle = new Bundle();
        bundle.putString("email",email);
        bundle.putString("pwd",pwd);
        welcomeFragment.setArguments(bundle);
        fragmentTransaction.replace(R.id.container,welcomeFragment);
        fragmentTransaction.addToBackStack(null); // Back button correctly        fragmentTransaction.commit();
    }
}

Live Demo: https://www.youtube.com/watch?v=vgReN-zmXnQ


No comments:

Post a Comment