Showing posts with label Fragments. Show all posts
Showing posts with label Fragments. Show all posts

Monday, 7 November 2016

AndroidFragmentTransaction

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 :-

<?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
AndroidFragmentTransaction fig:1




















AndroidFragmentTransaction
AndroidFragmentTransaction fig:2




















AndroidFragmentTransaction
AndroidFragmentTransaction fig:3



Friday, 21 October 2016

Fragments

 Fragment :-
Android piece lifecycle is influenced by movement lifecycle on the grounds that sections are incorporated into action. Every part has its own life cycle strategies that is influenced by action life cycle since pieces are installed in action. The FragmentManager class is mindful to make cooperation between piece objects.
How to Creating a Fragment :-
To make a part, you should make a subclass of Fragment (or a current subclass of it). The Fragment class has code that looks a great deal like an Activity. It contains callback strategies like a movement, for example, onCreate(), onStart(), onPause(), and onStop(). Indeed, in case you're changing over a current Android application to utilize pieces, you may just move code from your action's callback strategies into the separate callback techniques for your part.
 How Fragment are in single screen or multiple screen as shown figure below :

Android Fragment fig :1