Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Wednesday, 11 March 2015

Simple replace(Transaction) fragment to fragment, fragment to Activity, Activity to fragment

Firstly you create a new AndroidProject.


MainActivity.java


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.sample.Fragment1;


public class Main_Activity extends FragmentActivity
{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);           //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 0);
                   // Above two line code hide your Actionbar                    
setContentView(R.layout.activity_home_);

Fragment1 fragment1 = new Fragment1);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fragment1);
                transaction.addToBackStack(null); //This line of code prevent when  you press backbutton                   your application not close.
fragmentTransaction.commit();


}

activity_main.xml

<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"
    android:background="@color/White_White"
    tools:context="com.example.sample.MainActivity" >

     <fragment
        android:name="com.example.sample.Fragment1"
                android:id="@+id/fragment_place"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</RelativeLayout>


Now the second step you create a new class Fragment1 and exteds Fragment now this class make Fragment

Fragment1.java

com.example.sample;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {
   Button btn1,btn2;
   @Override
   public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {
      View view= inflater.inflate(R.layout.fragment_one, container, false);
    btn1=(Button)view. findViewById(R.id.btnchange);
    btn1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

    Fragment fragment=new Fragment2();
    FragmentManager fragmentManager=getFragmentManager();
    FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_place, fragment);
  transaction.commit();

   // Above code transaction of fragment to fragment

}
});

   btn2=(Button)view. findViewById(R.id.btnchangeactivity);
    btn2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent=new Intent(getActivity(),MainActivity.class);
                         startActivity(intent);
   

   // Above code transaction of fragment to Activity

}
});

      return view;
   }
}


Now you create another Fragment name Fragment2

Fragment2.java

com.example.sample;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

   public class Fragment2 extends Fragment {
   Button btn1,btn2;
   @Override
   public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {
      View view= inflater.inflate(R.layout.fragment_two, container, false);
      btn1=(Button)view. findViewById(R.id.btnchange);
      btn1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

    Fragment fragment=new Fragment1();
    FragmentManager fragmentManager=getFragmentManager();
    FragmentTransaction transaction=fragmentManager.beginTransaction();
 transaction.replace(R.id.fragment_place, fragment);
  transaction.commit();

   // Above code transaction of fragment to fragment

}
});

    btn2=(Button)view. findViewById(R.id.btnchangeactivity);
    btn2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent=new Intent(getActivity(),MainActivity.class);
                         startActivity(intent);
   

                       // Above code transaction of fragment to Activity

}
});

      return view;
   }
}


Now you create fragment_one.xml

fragment_one.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
    android:background="#e2e3e5">

       <TextView
           android:id="@+id/textView1"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:text="This is fragment No.1"
           android:textStyle="bold" />

       <Button
           android:id="@+id/btnchange"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Change fragment"
           android:textStyle="bold" />

       <Button
           android:id="@+id/btnchangeactivity"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Go Home"
           android:textStyle="bold" />

</RelativeLayout>


fragment_two.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
    android:background="#e2e3e5">
   
       <TextView
           android:id="@+id/textView1"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:text="This is fragment No.2"
           android:textStyle="bold" />

       <Button
           android:id="@+id/btnchange"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Change fragment"
           android:textStyle="bold" />

<Button
           android:id="@+id/btnchangeactivity"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Go Home"
           android:textStyle="bold" />

</RelativeLayout>


Now go to mainifest.xml

mainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sample;"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.sample.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



No comments:

Post a Comment