Android Fragment Example | Whatsapp Model

ANDROID

Android Fragment Example

 

Android Fragment Example with Source code: Fragment Activity

Android Program of fragment sample code example

       

 

Java Code

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class FragAct extends Activity {

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

FragmentManager fm = getFragmentManager();
FragmentTransaction fgtr = fm.beginTransaction();
fgtr.add(R.id.ll1,new MyFragment2());
fgtr.commit();

}

public void openOne(View v)
{
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.ll1,new MyFragment1());
ft.commit();
}

public void openTwo(View v)
{
FragmentManager fm = getFragmentManager();
FragmentTransaction fgtr = fm.beginTransaction();
fgtr.replace(R.id.ll1,new MyFragment2());
fgtr.commit();
}

public void openThree(View v)
{
FragmentManager fm = getFragmentManager();
FragmentTransaction fgtr = fm.beginTransaction();
fgtr.replace(R.id.ll1,new MyFragment3());
fgtr.commit();
}
}

 

Java Code(MyFragment1)

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

public class MyFragment1 extends Fragment
{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_layout1,container,false);
return v;
}
}

Java Code(MyFragment2)

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

public class MyFragment2 extends Fragment
{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_layout2,container,false);
return v;
}
}

Java Code(MyFragment3)

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

public class MyFragment3 extends Fragment
{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_layout3,container,false);
return v;
}
}

 

Layout XML Code(activity_frag)

<?xml version=”1.0″ encoding=”utf-8″?>
<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:orientation=”vertical”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”com.andro.tech.androidproject.FragAct”>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textSize=”@dimen/textsize”
android:layout_gravity=”center”
android:textStyle=”bold”
android:text=”FRAGMENTS”
android:layout_marginBottom=”10dp” />

<TableLayout
android:background=”#0080ff”
android:layout_margin=”5dp”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”>

<TableRow>

<Button
android:layout_margin=”5dp”
android:layout_weight=”1″
android:background=”#ff00dd”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”One”
android:onClick=”openOne”/>
<Button
android:layout_margin=”5dp”
android:layout_weight=”1″
android:background=”#ff3c00″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Two”
android:onClick=”openTwo”/>

<Button
android:layout_margin=”5dp”
android:layout_weight=”1″
android:background=”#08ff00″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Three”
android:onClick=”openThree”/>

</TableRow>

</TableLayout>

<LinearLayout
android:id=”@+id/ll1″
android:background=”#08ff00″
android:orientation=”vertical”
android:layout_width=”match_parent”
android:layout_height=”320dp”
android:layout_marginBottom=”20dp”>

</LinearLayout>

<Button
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”@string/sourcecode”
android:background=”@color/bgcolor”
android:textStyle=”bold”
android:textColor=”@color/text”/>

</LinearLayout>

Layout XML Code(fragment_layout1)

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:gravity=”center”
android:background=”#ff00dd”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”I am One”
android:textColor=”#FFF”
android:textStyle=”bold”
android:textSize=”30sp”/>

</LinearLayout>

Layout XML Code(fragment_layout2)

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:gravity=”center”
android:background=”#ff3c00″
android:layout_width=”match_parent”
android:layout_height=”match_parent”>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”I am Two”
android:textColor=”#FFF”
android:textStyle=”bold”
android:textSize=”30sp”/>

</LinearLayout>

Layout XML Code(fragment_layout3)

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:gravity=”center”
android:background=”#08ff00″
android:layout_width=”match_parent”
android:layout_height=”match_parent”>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”I am Three”
android:textColor=”#FFF”
android:textStyle=”bold”
android:textSize=”30sp”/>

</LinearLayout>

 

Output :

 

Download Source Code for Android Fragment example

 

Learn Android

Link1 – Complete Android Programs with Source Code Download

Link 2- Android Interview Questions for Freshers and Beginners

Link 3- MNC’s Jobs Vacancies 2018 

Link 4- Java Interview Questions for Freshers and Experience

Android fragment with android fragment example source code | Whatsapp Model

 

Related Search

activity lifecycle, android developer fragment code example download, android fragment source code example download, android fragment example download, android fragment example source code, android fragment tutorial with source code, android lifecycle example, android list fragment, android multiple fragments in one activity example, android studio fragment example download, android studio fragment example download, android tablet layout example, fragment, fragment activity android for beginners, fragment android studio for beginners, fragment app, fragment example in android studio, fragment examples with simple example, fragment layout, fragment lifecycle, android fragment example source code download

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

    Comments