Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create fragment In android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 84
    Comment on it

    In the below example I have created a simple fragment app code. Here, first I have created two new fragment xml layouts and neamed them fragmentone and fragmenttwo then in this I have added TextView and backgraund image. Now In activity_main.xml I have added two button and a fragment layout, in fragment layout I have added package name. Now in next step I have created two new class (FragmentOne and FragementTwo) both class exetend with Fragment class. In MainActivity I have used FragmentTransaction class and getFragmentManager() method. You can see below example code it clearly describe you "How to create fragment In android”.

    Step(1)Create a new fragmentone.xml layout-

    <?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"
        android:orientation="vertical"
        android:background="@drawable/one">
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Evon"
            android:textStyle="bold" />
    
    </LinearLayout>

    Step(2)Create a new fragmenttwo.xml layout-

    <?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"
        android:orientation="vertical"
        android:background="@drawable/two">
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Ericsson"
            android:textStyle="bold" />
    
    </LinearLayout>

    Step(3)activity_main.xml layout-

    <?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"
        android:orientation="vertical" >
        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Fragment No.1"
            android:onClick="selectFrag" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:onClick="selectFrag"
            android:text="Fragment No.2" />
        <fragment
            android:name="com.tiwari.rajshekhar.fragment.FragmentOne"
            android:id="@+id/fragment_place"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>


    Step(4) Created a new FragmentOne class-

    public class FragmentOne extends android.app.Fragment {
    
            @Override
            public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
                return inflater.inflate(R.layout.fragmentone,container,false);
            }
    }


    Step(5) Created a new FragmentTwo class-

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

    Step(6)MainActivity-
     

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
        }
        public void selectFrag(View view){
            Fragment fr;
            if(view ==findViewById(R.id.button2)){
                fr=new FragmentTwo();
            }else {
                fr = new FragmentOne();
            }
            FragmentManager fm=getFragmentManager();
            FragmentTransaction fragmentTransaction=fm.beginTransaction();
            fragmentTransaction.replace(R.id.fragment_place, fr);
            fragmentTransaction.commit();
    
        }
    
    }
    
    

     

 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: