Android Fragment example

activity_main.xml

<LinearLayout 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="#FF00BB"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <!-- Static fragment lod here -->
    <fragment
        android:id="@+id/my_fragment"
        android:name="com.example.bikesh.tutorfragments.MyFragments"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_show_fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click Me" />

    <!-- Dynamic fragment lod here (no need to create FrameLayout we can create *any layout)-->
    <FrameLayout
        android:id="@+id/my_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java

package com.example.myapp.tutorfragments;

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

public class MainActivity extends AppCompatActivity {

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

        findViewById(R.id.btn_show_fragment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                MyFragments frag = new MyFragments();
                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.add(R.id.my_layout, frag, "view fragment tag");
                transaction.commit();

            }
        });
    }
}

MyFragments.java

package com.example.myapp.tutorfragments;

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

/**
 * Created by bikesh on 3/12/2015.
 */
public class MyFragments extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //return super.onCreateView(inflater, container, savedInstanceState);

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

my_fragments_layout.xml

<?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:background="#FFBB00" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text From fragment"
        android:id="@+id/textView" />
</LinearLayout>

 

Author: bm on June 8, 2016
Category: Android