Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Implementing Fragment in Android

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 249
    Comment on it

    In this example we are using two fragments Fragment1 and Fragment2 where Fragment1 consist of a button named click, this button will show the Fragment2 on clicking the Click button.

    To implement Fragment in Android follow the steps mentioned below:-

    1) Use fragment tag in your new project's activity_main.xml file

    activity_main.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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    
        <fragment
            android:name="com.example.manjeet.frameapp.Fragment1"
            android:id="@+id/fragment1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">
        </fragment>
        <!--Linearlayout containing fragment2-->
        <LinearLayout
            android:id="@+id/llFrame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone">
            <fragment
                android:name="com.example.manjeet.frameapp.Fragment2"
                android:id="@+id/fragment2"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </fragment>
        </LinearLayout>
    
    </LinearLayout>
    

    2)Now Create two fragment file in your project and name them Fragment1 and Fragment2 .

    Fragment1.java

    public class Fragment1 extends Fragment{
        private Button click;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.fragment1,container, false);
    
            click = (Button) view.findViewById(R.id.btClick);
            click.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    ((MainActivity)getActivity()).showfragment();
    
                }
    
            });
    
            return view;
    
        }
    }
    

    fragment1.xml

     <?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">
    
            <Button
                android:id="@+id/btClick"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Click"
                android:layout_marginTop="20dp"/>
    
        </LinearLayout>
    

    Fragment2.java

    public class Fregment2 extends Fragment {
        EditText username,email,password;
        Button register;
        CheckBox agree;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.fragment2,container, false);
    
            username=(EditText)view.findViewById(R.id.etUserName);
            email=(EditText)view.findViewById(R.id.etEmail);
            password=(EditText)view.findViewById(R.id.etPassword);
            register=(Button)view.findViewById(R.id.btRegister);
            agree=(CheckBox)view.findViewById(R.id.cbAgree);
    
    
            register.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String name=username.getText().toString();
                    String mail=email.getText().toString();
                    String pass=password.getText().toString();
                    boolean validationPassed = true;
                    if (name.isEmpty()) {
                        validationPassed = false;
                      //  username.setError(getResources().getString(R.string.errorusername));
                    }
                    if (mail.isEmpty()) {
                        validationPassed = false;
                        //email.setError(getResources().getString(R.string.errormail));
                    }
                    if (pass.isEmpty()) {
                        validationPassed = false;
                        //password.setError(getResources().getString(R.string.errorusername));
                    }
                    if (!(agree).isChecked())
                        Toast.makeText(getActivity(),"First Check",Toast.LENGTH_SHORT).show();
    
                    if (validationPassed) {
                        Toast.makeText(getActivity(),"Registered",Toast.LENGTH_SHORT).show();
                    }
                }
            });
    
            return view;
        }
    }
    

    fragment2.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent">
        <EditText
            android:id="@+id/etUserName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:hint="User-Name" />
    
        <EditText
            android:id="@+id/etEmail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_below="@+id/etUserName"
            android:hint="m@il-id"/>
    
        <EditText
            android:id="@+id/etPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_below="@+id/etEmail"
            android:hint="password"/>
    
        <CheckBox
            android:id="@+id/cbAgree"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_below="@+id/etPassword"
            android:text="I Agree "/>
    
        <Button
            android:id="@+id/btRegister"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_below="@+id/cbAgree"
            android:text="Submit"/>
    

    3)In MainActivity we have defined a function showfragment through which visibility of Fragment2 is set true.

    MainActivity.java

    public class MainActivity extends Activity {
        private LinearLayout llFrame;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            llFrame=(LinearLayout)findViewById(R.id.llFrame);
        }
        public void showfragment(){
            llFrame.setVisibility(LinearLayout.VISIBLE);
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: