Saturday, 6 January 2018

Android Work with Fragments (Basic) -Tutorial

In Android apps start working with activities. To learn how these process works you should have to understand the life cycle of a Activity. To learn about life cycle check this pic




Apps start working with the onCreate method when you create an activity the method is created by default to check this have a look at this

when the activity starts working it set the view by setContentView method by passing the arguments like R.layout.activity_main.
But while navigating to other screens you have to call other activities which have to affect the performance of your app, to tackle this problem we have fragments by using fragments we can minimize the work of activity or decrease the load of the activity.
Fragments are light weight they work same as your  activity .
Lets have a look how to implement fragments in your app.
1.Start a new Android Project with blank Activity
 Choose a name according to you
2.Choose the target SDK version:-
3.Then choose blank activity and then click next
after click next your code looks like this
package com.example.hp.newapp1;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
then right click on


the folder like this which is highlighted and create a class name it like Login_frag
and create a layout for this layout folder in the res folder same as class creation in this you have to choose the "layout xml file" option name it like login.xml
and then create a one more class and one more layout file for that class name them Reg_lay_frag and reg_lay resp. then add following code to your files.



Login_frag.java
public class LoginFragment extends Fragment {
    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.login,container,false);
    }
}
you have to extend the classes with Fragment class to use the methods of this class 
when u add onCreateView method you have to delete its  inner statements and write the return statement as stated above 
which set the view for the class.
same as in  the Reg_lay_frag.java file
 
 
 
 
 

Reg_lay_frag.java

public class Reg_Lay_Frag extends Fragment {
    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.reg_lay,container,false);
    }
}


then open the xml files and edit them in text mode add the following code to your xml file 
 

 

login.xml

<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="255dp" 
 android:orientation="vertical">
    <EditText 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:layout_marginTop="80dp"  
 android:layout_marginLeft="20dp" 
 android:layout_marginRight="20dp" 
 android:hint="User name"/>
<EditText    android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:layout_marginRight="20dp" 
 android:layout_marginLeft="20dp" 
 android:layout_marginTop="20dp" 
 android:hint="Password" 
 android:inputType="textPassword"    />
 <Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:id="@+id/Frag1_Button" 
 android:layout_gravity="center" 
 android:text="Login" 
 android:layout_marginTop="20dp"/>
</LinearLayout>
 
 
 
 
make the layout linear otherwise it is your choice 
 

 Reg_lay.xml

<LinearLayout    android:orientation="vertical" 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent"    android:layout_height="340dp">


    <EditText         
android:layout_width="match_parent"         
android:layout_height="wrap_content" 
 android:layout_marginTop="60dp" 
 android:layout_marginLeft="20dp" 
 android:layout_marginRight="20dp" 
 android:hint="User name"/>
    <EditText         
android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:layout_marginRight="20dp" 
 android:layout_marginLeft="20dp" 
 android:layout_marginTop="20dp" 
 android:hint="Password"        />
 
 
 <EditText 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:layout_marginRight="20dp" 
 android:layout_marginLeft="20dp" 
 android:layout_marginTop="20dp"  
 android:hint="Comfirm Password"        />
 
 
 <Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:id="@+id/Frag1_Button" 
 android:layout_gravity="center" 
 android:text="Register" 
 android:layout_marginTop="20dp"/>

</LinearLayout>
 
 
and then edit your main activity.xml 
you have to create a space for the fragments to show in the main activity
 
 
 
<?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.ConstraintLayout
 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="com.example.hp.newapp1.MainActivity">
<LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 android:background="#694d2b">
 <TextView 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:text="Fragment one" 
 android:textAppearance="?android:textAppearanceLarge" 
 android:gravity="center" 
 android:textColor="#ffee"/>

    <RelativeLayout 
 android:layout_width="match_parent" 
 android:layout_height="340dp" 
 android:id="@+id/frag_con" 
 android:layout_marginTop="30dp">

    </RelativeLayout>
    <TextView 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:id="@+id/reg_user" 
 android:text="New User" 
 android:textAppearance="?android:textAppearanceLarge" 
 android:layout_marginTop="20dp" 
 android:gravity="center"/>
</LinearLayout>

</android.support.constraint.ConstraintLayout>
 

 then add onClick on the edit text in the main activity .java file 

add the following code 

 

 

 

package com.example.hp.newapp1;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
TextView textView;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=(TextView)findViewById(R.id.reg_user);
        FragmentManager fm=getFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
        LoginFragment loginFragment=new LoginFragment();
        ft.add(R.id.frag_con,loginFragment);
        ft.commit();
        textView.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View view) {
                FragmentManager fragmentManager= getFragmentManager();
                FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                Reg_Lay_Frag reg_lay_frag=new Reg_Lay_Frag();
                fragmentTransaction.replace(R.id.frag_con,reg_lay_frag).addToBackStack("1");
                fragmentTransaction.commit();
            }
        });
    }
}




Then Run your app and test it .
You can do various experiments on the fragments .
Almost every app uses the fragments to use them again you have to understand the life cycle  of the Fragments

 

 

 


No comments:

Post a Comment