Wednesday, 11 January 2017

Android Fragment Example

Hello friends Today i am going to share an example of fragment please follow me step by step .


Step 1: Create a Project with package name com.ritesh.demo

Step 2 :Create MainActivity java file and activity_main.xml
Step 3: Write this code in your build.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.ritesh.demo"
minSdkVersion 10
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
}


Step 4 : Write this code in your MainActivity

package com.ritesh.demo;

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

public class MainActivity extends AppCompatActivity {
public static FragmentManager fragmentmanager;

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

private void initFragment(Fragment fragment) {
fragmentmanager=getSupportFragmentManager();
FragmentTransaction ft=fragmentmanager.beginTransaction();
ft.add(R.id.frame_container,fragment,null);
ft.commit();
}
public static void gotoFragment(Fragment fragment) {
FragmentTransaction ft=fragmentmanager.beginTransaction();
ft.replace(R.id.frame_container,fragment,null);
//for allow backpress in fragment
ft.addToBackStack(null);
ft.commit();
}
}


Step 5 : Write this code in your layout activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame_container"
/>
</RelativeLayout>


Step 6 : Now Create a FragmentFirst and write this code

package com.ritesh.demo;

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;
import android.widget.TextView;

/**
* Created by riteshsingh on 11/01/17.
*/

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

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//object creation
TextView textView1=(TextView)getActivity().findViewById(R.id.textView1);
textView1.setText("First Fragment,Click me");
textView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainActivity.gotoFragment(new FragmentSecond());
}
});

}
}



Step 7 : Now Create a fragment1.xml and write this code

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

<TextView
android:text="Fragment 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:id="@+id/textView1"
android:layout_weight="1" />
</LinearLayout>




Step 8 : Now Create a FragmentFirst and write this code

package com.ritesh.demo;

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;
import android.widget.TextView;

/**
* Created by riteshsingh on 11/01/17.
*/

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

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//object creation
TextView textView2=(TextView)getActivity().findViewById(R.id.textView2);
textView2.setText("Second Fragment,Click me");

textView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainActivity.gotoFragment(new FragmentThird());
}
});

}
}



Step 9 : Now Create a fragment2.xml and write this code

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

<TextView
android:text="Fragment 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:id="@+id/textView2"
android:layout_weight="1" />
</LinearLayout>



Step 10 : Now Create a FragmentThird and write this code

package com.ritesh.demo;

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;
import android.widget.TextView;

/**
* Created by riteshsingh on 11/01/17.
*/

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

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

}
}



Step 11 : Now Create a fragment3.xml and write this code

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

<TextView
android:text="Fragment 3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:id="@+id/textView3"
android:layout_weight="1" />
</LinearLayout>

Step 12: Write this code in your manifest file


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ritesh.demo">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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>

Step 13 : styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

</resources>

Step 14 : colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>


Step 15 : Now run your fragment and Understand about fragment

No comments:

Post a Comment