How to use fragment in android with Example - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
How to use fragment in android with Example

How to use fragment in android with Example

 .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:weightSum="4"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:weightSum="3"
        android:orientation="horizontal">
        <Button
            android:id="@+id/button"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="event"
            android:text="btn 1"/>
        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="event"
            android:text="btn 1"/>
        <Button
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="event"
            android:text="btn 1"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/space1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:weightSum="1"
        android:orientation="vertical">
    </LinearLayout>
    <LinearLayout
        android:id="@+id/space2"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:weightSum="1"
        android:orientation="vertical">
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:weightSum="1"
        android:id="@+id/space"
        android:orientation="vertical">
    </LinearLayout>

</LinearLayout>

fragment1.xml

<?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"
    android:background="@color/colorPrimaryDark">

</android.support.constraint.ConstraintLayout>


fragment2.xml

<?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="#F59418"
    tools:context=".FragmentTwo">

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

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="30sp"
        android:text="@string/hello_blank_fragment" />

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</FrameLayout>


  fragmentthird.xml
<?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="#0D57F1"
    tools:context=".FragmentThird">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

MainActivity.java

package com.example.fragmentinandroid;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public  void event(View view)
    {
        switch(view.getId())
        {
            case R.id.button:
                FragmentManager manager=getSupportFragmentManager();
                FragmentTransaction transaction=manager.beginTransaction();
                fragmentOne one =new fragmentOne();
                transaction.add(R.id.space1,one);
                transaction.commit();
                break;
            case R.id.button2:
                FragmentManager manager1 =getSupportFragmentManager();
                FragmentTransaction transaction1 =manager1.beginTransaction();
                FragmentTwo two =new FragmentTwo();
               transaction1.add(R.id.space2,two);
                transaction1.commit();
              break;

            case R.id.button3:
                FragmentManager manager2 =getSupportFragmentManager();
                FragmentTransaction transaction2 =manager2.beginTransaction();
                FragmentThird thrid =new FragmentThird();
                transaction2 .add(R.id.space,thrid);
                transaction2.commit();
                break;

        }
    }
}
fragment1.java


package com.example.fragmentinandroid;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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



fragment2.java
package com.example.fragmentinandroid;

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


public class FragmentTwo extends Fragment {


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

    }



fragment3.java
package com.example.fragmentinandroid;

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


public class FragmentThird extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragment_third, container, false);
    }
    }
                                              
click on the first button

        


click on the Second buttonclick on the third button




About EasyToCode

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.