Cdacians

Cdacians
Cdacians

Tuesday, 10 July 2012

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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   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">
    </FrameLayout>
    <Button android:id="@+id/new_fragment"
       android:layout_width="wrap_content" android:layout_height="wrap_content"
       android:layout_weight="0" 
       android:text="New fragment">
        <requestFocus />
    </Button>
</LinearLayout>
3.)  Create and write following into fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/text"
   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



Thanks
akm
www.cdacians.com

No comments:

Post a Comment