Android Fragment exchange (or) "Fragment Transaction" to handle sections. Android Fragment are valuable when we need to bolster different screen measure. To oversee parts we require a FragmentManager that help us to handle Android piece trasaction between sections. With Android part exchange we mean a succession of ventures to include, supplant or evacuate pieces. In the last post we demonstrated to bolster numerous screen size and introduction utilizing sections.
code for FragmentTransactin shown below.
activity_main.xml :-
fraga.xml :-
fragb.xml :-
MainActivity.java :-
FragA.java :-
FragB.java :-
Output :-
code for FragmentTransactin shown below.
activity_main.xml :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
tools:context="com.fragmenttransaction.MainActivity"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragA"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragB"
android:id="@+id/button2"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true" /> <FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:id="@+id/myframe"
android:background="#decaca">
</FrameLayout> </LinearLayout>
fraga.xml :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#811313"> <TextView
android:layout_width="209dp"
android:layout_height="54dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="this fragment A"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" /> </LinearLayout>
fragb.xml :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#be6f6f"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="fragment B"
android:id="@+id/textView2"
android:layout_gravity="center_horizontal" /> </LinearLayout>
MainActivity.java :-
package com.fragmenttransaction; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity implements
View.OnClickListener { Button b1,b2; FragmentManager fragmentManager; FragmentTransaction fragmentTransaction,fragmentTransaction1; @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragmentManager=getFragmentManager(); b1=(Button)findViewById(R.id.button); b2=(Button)findViewById(R.id.button2); b1.setOnClickListener(this); b2.setOnClickListener(this); } @Override
public void onClick(View v) { switch (v.getId()) { case R.id.button: fragmentTransaction=fragmentManager.beginTransaction(); fragA fragA=new fragA(); fragmentTransaction.add(R.id.myframe,fragA); fragmentTransaction.commit(); break; case R.id.button2: fragmentTransaction1=fragmentManager.beginTransaction(); frabB frabB=new frabB(); fragmentTransaction1.add(R.id.myframe,frabB); fragmentTransaction1.commit(); break; } } }
FragA.java :-
package com.fragmenttransaction; import android.app.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by Dell on 7/11/2016. */
public class fragA extends Fragment { @Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { return inflater.inflate(R.layout.fraga,container,false); } }
FragB.java :-
package com.fragmenttransaction; import android.app.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by Dell on 7/11/2016. */
public class frabB extends Fragment { @Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { return inflater.inflate(R.layout.fragb,container,false); } }
Output :-
AndroidFragmentTransaction fig:1 |
AndroidFragmentTransaction fig:2 |
AndroidFragmentTransaction fig:3 |
No comments:
Post a Comment