Saturday 22 October 2016

Fragment code

  In Fragments there are two types are adding for Fragment .

 1) Static Fragment :-

Syntax : -
  xml :-              
      <?xml version="1.0" encoding="utf-8"?>
      <fragment  
       android:id="@+id/fragment1" 
       android:name="com.kiranapp.dynamicfragment" 
       android:layout_width="0px" 
       android:layout_height="match_parent" 
       android:layout_weight="1" /> 
 
  java :-
               Fragment frag=getFragmentManager().findFragmentByID(R.id.static_fragment);

 2) Dynamic Fragment :-

Syntax :-
 xml : -       
     <FrameLayout 
      android:layout_width="match_parent"     
      android:layout_height="wrap_content" 
      android:layout_marginTop="20dp" 
      android:id="@+id/myframe" 
      android:layout_gravity="center_horizontal" 
      android:background="#8a99c3">
    </FrameLayout>

 java : -         
     fragmentManager=getFragmentManager();
     dynamicfragment dynamifrag=new dynamicfragment();
     fragmentTransaction=fragmentManager.beginTransaction();
     fragmentTransaction.add(R.id.myframe,dynamifrag);
     fragmentTransaction.commit();    
 
    DynamicFragment :-

               Below code is example for  dynamicfragment . And i have taken another one xml    
               and java.

 
 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.kiranapp.MainActivity" 
  android:background="#e6cbcb">
 <TextView 
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content" 
  android:textAppearance="?android:attr/textAppearanceLarge"  
  android:text="This is Activity" 
  android:id="@+id/textView" 
  android:layout_marginTop="10dp" 
  android:layout_gravity="center_horizontal" />
 <FrameLayout 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:layout_marginTop="20dp"  
  android:id="@+id/myframe" 
  android:layout_gravity="center_horizontal" 
  android:background="#8a99c3">
 </FrameLayout>
</LinearLayout>

dynamicfragment.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" 
  android:background="#7b1818">

<TextView  
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Dynamic Fragments" 
 android:id="@+id/textView" 
 android:layout_marginTop="10dp" 
 android:layout_gravity="center_horizontal" 
 android:background="#e9e5e5" />
<Button  
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="frag b" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java :-

package com.kiranapp;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;

@Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 fragmentManager=getFragmentManager();
 dynamicfragment dynamifrag=new dynamicfragment();
 fragmentTransaction=fragmentManager.beginTransaction();
 fragmentTransaction.add(R.id.myframe,dynamifrag);
 fragmentTransaction.commit();
 }
}

dynamicfragment.java :-

package com.kiranapp;

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;
import android.widget.Button;
import android.widget.Toast;

/** * Created by dell on 8/9/2016. */
public class dynamicfragment extends Fragment {
    Button b1;
@Nullable
@Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                                                      Bundle savedInstanceState) {
 return inflater.inflate(R.layout.dyanamicfragment,container,false);
 }
@Override 
 public void onActivityCreated(Bundle savedInstanceState) {
 super.onActivityCreated(savedInstanceState);
 b1=(Button)getActivity().findViewById(R.id.button);
 b1.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 Toast.makeText(getActivity(), "Button is Working", Toast.LENGTH_SHORT).show();
 }
 });
}
}


Output :-

Fragment code
DynamicFragment fig :1





















Fragment code
DynamicFragment fig :2








No comments:

Post a Comment