Add Fragments

 


First Fragment -


package com.example.fragments;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;


public class FirstFragment extends Fragment {


    public FirstFragment() {

        // Required empty public constructor

    }



    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.fragment_first, container, false);

    }


}


-----------------------------------------------------------------------------------------------


Second Fragment - 


package com.example.fragments;



import android.os.Bundle;


import androidx.fragment.app.Fragment;


import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;



/**

 * A simple {@link Fragment} subclass.

 */

public class SecondFragment extends Fragment {



    public SecondFragment() {

        // Required empty public constructor

    }



    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.fragment_second, container, false);

    }


}


--------------------------------------------------------------------------------------------


Thierd Fragment -


package com.example.fragments;



import android.os.Bundle;


import androidx.fragment.app.Fragment;


import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;



/**

 * A simple {@link Fragment} subclass.

 */

public class ThierdFragment extends Fragment {



    public ThierdFragment() {

        // Required empty public constructor

    }



    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.fragment_thierd, container, false);

    }


}


-----------------------------------------------------------------------------------------------


MainActivity -


package com.example.fragments;


import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.FragmentManager;


import android.os.Bundle;

import android.view.View;

import android.widget.Button;


public class MainActivity extends AppCompatActivity {

    Button btn1,btn2,btn3;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        btn1=findViewById(R.id.firstFragment);

        btn2=findViewById(R.id.secondFragment);

        btn3=findViewById(R.id.thirdFragment);


        btn1.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                FirstFragment firstFragment = new FirstFragment();

                FragmentManager fragmentManager = getSupportFragmentManager();

                fragmentManager.beginTransaction().replace(R.id.linearLayout, firstFragment, firstFragment.getTag()).commit();

            }

        });

        btn2.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                SecondFragment secondFragment = new SecondFragment();

                FragmentManager fragmentManager = getSupportFragmentManager();

                fragmentManager.beginTransaction().replace(R.id.linearLayout,secondFragment,secondFragment.getTag()).commit();

            }

        });

        btn3.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                ThierdFragment thierdFragment = new ThierdFragment();

                FragmentManager fragmentManager = getSupportFragmentManager();

                fragmentManager.beginTransaction().replace(R.id.linearLayout,thierdFragment,thierdFragment.getTag()).commit();

            }

        });

    }

}


---------------------------------------------------------------------------------------------------------------------


Fragment_First


<?xml version="1.0" encoding="utf-8"?>

<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"

    android:background="#ff0"

    tools:context=".FirstFragment">


    <!-- TODO: Update blank fragment layout -->

    <TextView

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:text="First Fragment"

        android:textColor="#f00"

        android:textSize="35sp"

        android:textStyle="bold" />



</FrameLayout>


--------------------------------------------------------------------------------------------------


Fragment_Second


<?xml version="1.0" encoding="utf-8"?>

<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"

    android:background="#ff0"

    tools:context=".FirstFragment">


    <!-- TODO: Update blank fragment layout -->

    <TextView

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:text="Second Fragment"

        android:textColor="#f00"

        android:textSize="35sp"

        android:textStyle="bold" />



</FrameLayout>


-------------------------------------------------------------------------------------------------


Fragment_Thierd


<?xml version="1.0" encoding="utf-8"?>

<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"

    android:background="#ff0"

    tools:context=".FirstFragment">


    <!-- TODO: Update blank fragment layout -->

    <TextView

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:text="Thierd Fragment"

        android:textColor="#f00"

        android:textSize="35sp"

        android:textStyle="bold" />



</FrameLayout>


------------------------------------------------------------------------------------------------------


activity_main


 <?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=".MainActivity">


    <HorizontalScrollView

        android:layout_width="match_parent"

        android:layout_height="wrap_content">


        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:orientation="horizontal">


            <Button

                android:id="@+id/firstFragment"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_marginRight="15dp"

                android:text="Open First Fragment"

                android:textStyle="bold" />


            <Button

                android:id="@+id/secondFragment"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_marginRight="15dp"

                android:text="Open Second Fragment"

                android:textStyle="bold" />


            <Button

                android:id="@+id/thirdFragment"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="Open Third Fragment"

                android:textStyle="bold" />


        </LinearLayout>

    </HorizontalScrollView>


    <LinearLayout

        android:id="@+id/linearLayout"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="vertical"></LinearLayout>



</LinearLayout>


-----------------------------------------------------------------------------------------------


AndroidManifest -



  

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.fragments">


    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:roundIcon="@mipmap/ic_launcher_round"

        android:supportsRtl="true"

        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>


</manifest>

Comments

Popular posts from this blog

Generate Signing Release keystore

🚀 COMPLETE CI/CD FROM SCRATCH

Camera Application