Tuesday 3 September 2013

Fragments android support for lower android versions

1.Download a jar file "android-support-v4.jar" and add it to libs folder and set the path in project
then create a MainActivity

 public class MainActivity extends FragmentActivity  {

    Button b1,b2,b3;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        b1 = (Button)findViewById(R.id.button1);
        b2 = (Button)findViewById(R.id.button2);
        b3 = (Button)findViewById(R.id.button3);
       
        b1.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
           
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager
                        .beginTransaction();
                First first_act = new First();

                fragmentTransaction.replace(R.id.fragment_container,
                        first_act);
                fragmentTransaction.addToBackStack("first_act");
                fragmentTransaction.commit();
               
            }
        });
        b2.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
           
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager
                        .beginTransaction();
                Second sec_act = new Second();

                fragmentTransaction.replace(R.id.fragment_container,
                        sec_act);
                fragmentTransaction.addToBackStack("sec_act");
                fragmentTransaction.commit();
               
            }
        });
 b3.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
           
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager
                        .beginTransaction();
                Third third_act = new Third();

                fragmentTransaction.replace(R.id.fragment_container,
                        third_act);
                fragmentTransaction.addToBackStack("third_act");
                fragmentTransaction.commit();
               
            }
        });
    }

}
2.Create a layout  activity_main

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

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="100dp"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/button1"
            android:layout_marginTop="14dp"
            android:text="Button" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/button2"
            android:layout_marginTop="22dp"
            android:text="Button" />
    </RelativeLayout>



    <RelativeLayout
        android:id="@+id/fragment_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/relativeLayout1" >

    </RelativeLayout>

</RelativeLayout>

3.Create a Class First and layout

public class First extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        /** Inflating the layout for this fragment **/
        View v = inflater.inflate(R.layout.first, null);
        return v;
    }
}

first.xml
<?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" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

4.Create a Class Second and layout

public class Second extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        /** Inflating the layout for this fragment **/
        View v = inflater.inflate(R.layout.second, null);
        return v;
    }
}

second.xml
<?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" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

5. Create a Class Third and layout

public class Third extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        /** Inflating the layout for this fragment **/
        View v = inflater.inflate(R.layout.third, null);
        return v;
    }
}

second.xml
<?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" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Third "
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

No comments:

Post a Comment