Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make Fragment app in android ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 289
    Comment on it

    Below I have created fragment app in android. In Fragment class we can insert a fragment into our activity layout by declaring the fragment in the activity's layout file.
    Fragment Features-
    1. Fragment has its own layout and its own behavior with its own life cycle.
    2. We can add or remove fragments in an activity while the activity is running.
    3. We can use multiple activities in fragment.
    Below I have described how to make Fragment app step by step-
     Step-(1) create layout/activity_main.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:text="Evon"
       android:textAppearance="?android:attr/textAppearanceMedium" />
    

     Step-(2)create second layout

    <?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:orientation="vertical" >
    
       <fragment
          android:id="@+id/fragment1"
          android:name="com.pavan.listfragmentdemo.MyListFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />
    
    </LinearLayout>
    

     Step-(3)create fragment_slide_left_enter.xml 

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
       <objectAnimator
          android:interpolator="@android:interpolator/decelerate_quint"
          android:valueFrom="100dp" android:valueTo="0dp"
          android:valueType="floatType"
          android:propertyName="translationX"
          android:duration="@android:integer/config_mediumAnimTime" />
    
       <objectAnimator
          android:interpolator="@android:interpolator/decelerate_quint"
          android:valueFrom="0.0" android:valueTo="1.0"
          android:valueType="floatType"
          android:propertyName="alpha"
          android:duration="@android:integer/config_mediumAnimTime" />
    </set>
    

     Step-(4)Create fragment_slide_right_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <objectAnimator
            android:interpolator="@android:interpolator/decelerate_quint"
            android:valueFrom="0dp" android:valueTo="100dp"
            android:valueType="floatType"
            android:propertyName="translationX"
            android:duration="@android:integer/config_mediumAnimTime" />
    
        <objectAnimator
            android:interpolator="@android:interpolator/decelerate_quint"
            android:valueFrom="1.0" android:valueTo="0.0"
            android:valueType="floatType"
            android:propertyName="Raj"
            android:duration="@android:integer/config_mediumAnimTime" />
    </set>
    

     Step-(5)MainActivity code.

    package com.fragment.rajshekhar;
    
    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.View.OnClickListener;
    import android.view.ViewGroup;
    
    import android.widget.Button;
    import android.widget.TextView;
    
    /**
     * Demonstrates the use of custom animations in a FragmentTransaction when
     * pushing and popping a stack.
     */
    public class FragmentCustomAnimations extends Activity {
       int mStackLevel = 1;
    
       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.fragment_stack);
    
          // 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) {
             // Do first time initialization -- add initial fragment.
             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++;
    
          // Instantiate a new fragment.
          Fragment newFragment = CountingFragment.newInstance(mStackLevel);
    
          // Add the fragment to the activity, pushing this transaction
          // on to the back stack.
          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;
          /**
          * Create a new instance of CountingFragment, providing "num"
          * as an argument.
          */
          static CountingFragment newInstance(int num) {
             CountingFragment f = new CountingFragment();
    
             // Supply num input as an argument.
             Bundle args = new Bundle();
             args.putInt("num", num);
             f.setArguments(args);
             return f;
          }
    
          /**
          * When creating, retrieve this instance's number from its arguments.
          */
          @Override
          public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             mNum = getArguments() != null ? getArguments().getInt("num") : 1;
          }
          /**
          * The Fragment's UI is just a simple text view showing its
          * instance number.
          */
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
             View v = inflater.inflate(R.layout.hello_world, container, false);
             View tv = v.findViewById(R.id.text);
             ((TextView)tv).setText("Fragment #" + mNum);
             tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
             return v;
          }
       }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: