Monday, 3 July 2017

Android Fragment Example

A Fragment is a piece of an application's user interface or
 behavior that can be placed in an Activity.
 Interaction with fragments is done through
 FragmentManager, which can be obtained via
 Activity.getFragmentManager() and Fragment.getFragmentManager().


The Fragment class can be used many ways to achieve a wide variety 
of results. In its core, it represents a particular operation or i-
nterface that is running within a larger Activity. A Fragment is 
closely tied to the Activity it is in, and can not be used apart
from one.Though Fragment defines  its own lifecycle, that 
lifecycle  is dependent on its activity: if the activity is 
stopped, no fragments inside of it can be started; 
when the activity is destroyed, all fragments will be destroyed.
   

 
Source Code : 

MainActivity.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="sabziapp.ibridge.com.fregment.MainActivity">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fregment 1"
            android:id="@+id/btn_freg1"
            android:textAllCaps="false"
            android:layout_marginLeft="45dp"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fregment 2"
            android:id="@+id/btn_freg2"
            android:textAllCaps="false"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fregment 3"
            android:id="@+id/btn_freg3"
            android:textAllCaps="false"/>
    </LinearLayout>

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/change_frame"
       android:orientation="vertical"/>

</LinearLayout>


MainActivity.java



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

import layout.FragmentOne;
import layout.FragmentThree;
import layout.FragmentTwo;

public class MainActivity extends AppCompatActivity {
    Button btn_freg1, btn_freg2, btn_freg3;

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

        // get the reference of Button's        btn_freg1 = (Button) findViewById(R.id.btn_freg1);
        btn_freg2 = (Button) findViewById(R.id.btn_freg2);
        btn_freg3 = (Button) findViewById(R.id.btn_freg3);

        btn_freg1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadFragment(new FragmentOne());
            }
        });

        btn_freg2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadFragment(new FragmentTwo());
            }
        });

        btn_freg3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadFragment(new FragmentThree());
            }
        });


    }

    // loadFragment Function for Load fragment activity

    private void loadFragment(Fragment fr) {

        // create a FragmentManager        FragmentManager fm = getFragmentManager();

        // create a FragmentTransaction to begin the transaction and replace the Fragment       
       android.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();

        // replace the FrameLayout with new Fragment        
        fragmentTransaction.replace(R.id.change_frame, fr);
        fragmentTransaction.commit(); // save the changes    }
}


FragmentOne.xml



<FrameLayout 
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"    
tools:context="layout.FragmentOne">

    <!-- TODO: This Is  fragment layout -->   
 <ImageView        
android:layout_width="match_parent"        
android:layout_height="match_parent"       
android:background="@drawable/your_Iamge_Path"/>


</FrameLayout>



FragmentOne.java



import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import sabziapp.ibridge.com.fregment.R;

public class FragmentOne extends Fragment {

private OnFragmentInteractionListener mListener;


  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
 {
  // Inflate the layout for this fragment      
  return inflater.inflate(R.layout.fragment_fragment_one, container, false
 }


public interface OnFragmentInteractionListener {
   // TODO: Update argument type and name       
   void onFragmentInteraction(Uri uri);
 }
}


FragmentTwo And Three Are Same

Download Source Code :
https://drive.google.com/file/d/0B7p5TzrAq_QgQjZwUTJieUlZQUk/view?usp=sharing


No comments:

Android Studio - Get Current Latitude And Longitude

1. Add this dependencies in your gradle file     implementation 'com.google.android.gms:play-services-location:18.0.0' 2. Add this p...