Fragment Example In Android

This example shows Arc effects of graphics in android.

 

Algorithm:

 

1.)  Create a new project by File-> New -> Android Project name it FragmentExample.

2.)  Write following into main.xml:

 

<!--?xml version="1.0" encoding="utf-8"?-->
android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">

<FrameLayout
android:id="@+id/simple_fragment"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1">

<button> android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="0"
android:text="New fragment">

</button>

 

3.)  Create and write following into fragment.xml:

 

<!--?xml version="1.0" encoding="utf-8"?-->
android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="FragmentExample"/>

 

4.)  Create animator folder and write fragment left,right, entry and exit animation.

5.)  Run for output.

 

Steps:

 

1.) Create a project named FragmentExample and set the information as stated in the image.

 

Build Target: Android 4.0

Application Name: FragmentExample

Package Name: com. example. FragmentExample

Activity Name: FragmentExampleActivity

Min SDK Version: 14

 

 

2.) Open FragmentExampleActivity.java file and write following code there:

 

 

package com.example.FragmentExample;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class FragmentExampleActivity extends Activity {
/** Called when the activity is first created. */
int mStackLevel = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Watch for button clicks.
Button button = (Button)findViewById(R.id.new_fragment);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
addFragmentToStack();
}
});

if (savedInstanceState == null) {
Fragment newFragment = CountingFragment.newInstance(mStackLevel);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.simple_fragment, newFragment).commit();
} else {
mStackLevel = savedInstanceState.getInt("level");
}
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("level", mStackLevel);
}

void addFragmentToStack() {
mStackLevel++;
Fragment newFragment = CountingFragment.newInstance(mStackLevel);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.animator.fragment_slide_left_enter,
R.animator.fragment_slide_left_exit,
R.animator.fragment_slide_right_enter,
R.animator.fragment_slide_right_exit);
ft.replace(R.id.simple_fragment, newFragment);
ft.addToBackStack(null);
ft.commit();
}

public static class CountingFragment extends Fragment {
int mNum;

static CountingFragment newInstance(int num) {
CountingFragment f = new CountingFragment();
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);

return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText("Hello. This is fragment example #" + mNum);
tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
return v;
}
}
}

 

 

3.) Compile and build the project.

 

 

Output

 

 

 

 

Leave a Comment:

6 comments
sushant patekar says January 17, 2013

Thank You!!!
Very helpful

Reply
Arhtur says March 4, 2013

I kinda lost it at this step:
Create animator folder and write fragment left,right, entry and exit animation.

What animator folder?

Reply
Rodney Bluming says March 5, 2013

Thanks for sharing this great tutorial!

Reply
Oussama says May 16, 2013

Hi,

I didn’t understand the fourth step

4.) Create animator folder and write fragment left,right, entry and exit animation.

I would be thankfull if you explain it more.

Thanks in advance.

Reply
ggmax says June 29, 2013

hi thank you for creating this fine example. can you put your source project folder to download.

Reply
film senza limiti says October 23, 2013

The overall look of your website is great, let neatly as the content material!

Reply
Add Your Reply